reddit / emacs
How do you navigate 100+ lines of python code in lsp-mode?
omeow · 2022-06-16 09:04
Ideally
- I would like to quickly jump up/down to functions, class, possible get some type hints (if available) for variables.
- If possible fold/unfold blocks (function blocks probably) like in org mode.
​
I recently got \`lsp-mode\` to work. So, if something is available in this eco-system that will be great. Otherwise I am open to other suggestions. 'Elpy` has this functionality but it is very slow and I notice a lag -- which I cannot live with.
Thank you for your suggestions (and please feel free to describe/share your tools)!!!!
NoisyFrequency · 2022-06-16 10:07
For navigating definitions, you can use `lsp-ui-imenu` [see here](https://github.com/emacs-lsp/lsp-ui#lsp-ui-imenu), which will create an outline of the code, you need to install `lsp-ui` to use it. I personally prefer to use `xref-find-definitions` to lookup where a function was defined.
For code folding, there is `hs-minor-mode` (hideshow, which is included in Emacs) that lets you fold and unfold blocks.
diamondnbond · 2022-06-16 11:33
I like imenu-list and counsel imenu list :)
recencyeffect · 2022-06-16 11:43
Lsp-ui-menu was already mentioned, I'd like to add narrow to defun `C-x n d` that is sometimes useful in large files.
ExistingProgram3883 · 2022-06-16 13:05
I recommend using [consult-lsp](https://github.com/gagbo/consult-lsp) and its function consult-lsp-file-symbols.
konrad1977 · 2022-06-16 13:05
I am not using LSP but `dumb-jump` works great.
pathemata · 2022-06-16 13:15
outline-minor-mode works, but it is not perfect and sometimes annoying.
FrozenOnPluto · 2022-06-16 13:36
Random stream of consiousness pre-coffee :)
LSP enables python mode, which has things like C-M-a (jump to begin of block / def) IIRC, among many others.
I think the challenge is many options and methods, so which are they? which to use? which to bind?
I find myself using of course dozens and dozens of different keystrokes and options, but for lesser used I tend to make my own helpers.. I use hydra menus (pretty hydra iirc), and have set up a couple master keys that pop up top levcel hydras, and then have those hydras pop up sub hydras and/or make function calls; I commonly have some hydra entries that toggle things on and off (such as lsp-treemacs-symbols) to show a panel of vars and defs and such, and also to dismiss that; I so rarely use that sort of thing that I will never remember the keybind or function name, so I just bury it in one of my personal menus. So for me its <menu popup start key> -> c (coding subhydra) -> s (toggle symbol panel on and off) .. but thgat way after a fweq weeks, and I've forgotten a particular neat thing I can browse to discover the forgotten thing, in my menus, but also enable/disable .. and end up with my own keybind of a sort, via hydras.
LongjumpingAd7260 · 2022-06-16 21:53
By far, the best setup I've used is lsp-mode with pyright
omeow · 2022-06-17 10:41
Thank you. It looks like this can be very productive. In your workflow, do use any keybindings for `xref...` ? The other issue is if there are ways to navigate to blocks inside a function/previous/next function?
NoisyFrequency · 2022-06-17 14:19
Yeah, I think finding definitions is already bound to `M-.` but I have it bound to `M-ù` because I have a French speaking keyboard (BE_FR to be precise), it's a seldom used letter and there are no keybindings for it in Emacs originally.
I think if you want to navigate the blocks then it's definitely going to come down to things like `imenu-list` or `counsel` as other comments have mentioned.
cradlemann · 2022-06-17 16:42
THank you very much I was no aware of `beginning-of-defun` in Emacs. It will help me a lot
omeow · 2022-06-18 01:26
Great Thanks!
Every time, I look into a new emacs package I realize that I use such a small portion of the total functionality of all my installed packages. :(
NoisyFrequency · 2022-06-18 10:50
Somethings I just copy/paste a useful function into my `init` and add a comment to give credit, instead of loading a whole new function.
by_wicker · 2022-06-18 12:58
Have you been through the tutorial? It's limited but covers a lot of these fundamentals.
Also try `M-.` on a symbol, and `M-,` to return.
lstrang · 2022-06-29 09:52
I just started to appreciate C-M-n and C-M-p with source code. They go to the next and prior def respectively and are a quick way to flip through method definitions. I use them in Ruby, but they seem to work well with python as well. I have been using Emacs since the 90s and only recently discovered these as code naviagation tools.
omeow · 2022-06-29 21:56
In discovered that if you are using emacs's own python mode then it has a couple of navigation tools. like
\-beg/ end of block
\- beg/end of function
very useful.