autozimu / LanguageClient-neovim

Language Server Protocol (LSP) support for vim and neovim.
MIT License
3.55k stars 273 forks source link

update the tag stack for jump to definition #1218

Open iamcodemaker opened 3 years ago

iamcodemaker commented 3 years ago

When jumping to a definition, updating the tag stack would be a useful feature. This allows navigating between definitions independently of other code navigation (ctrl-o/ctrl-i based jumping). Popping the stack is done using ctrl-t and works the same as jumping works with a tags file generated by ctags. The logic follows this example.

Closes #517.

Considerations

martskins commented 3 years ago

I'm a little confused, I was expecting this to enable both :tag and :pop to work, and while I seem to be able to jump back in the tag stack using :pop I don't seem to be able to use :tag to go forwards in the tag stack. Isn't that how it's supposed to work? (genuinely asking, not 100% sure about how this works)

iamcodemaker commented 3 years ago

@martskins I noticed that as well. I'm not sure why :tag doesn't work either.

Edit: one would think:tag would just walk up the tag stack, but it gives an error message saying the tag is not found. It may have something to do with how I'm setting the tag name. I might have to take a look at the vim source to see what's going on.

iamcodemaker commented 3 years ago

Ok, I think I know what's happening. Effectively, this is a limitation (or bug) in the way the tag stack is implemented in vim. When the :tag command is run, vim doesn't just jump to the next tag on the stack, it attempts to find that tag again in the tag file. Since there is no tag file the tag not found message is returned.

The tag stack contains the name of a tag and where we jumped from to get to that tag, but it doesn't contain the location of the tag, only the tag name. So it seems the only way to actually jump to the next tag on the tag stack would be to locate that tag again. In the case of settagstack the actual jump is done elsewhere and the location of the tag is not know to the tag stack.

martskins commented 3 years ago

The tagstack does contain the buf number and the position though, I would have expect it to use that to jump to the next tag. I'll try poking around some other implementations of this to see how they behave, I think vim-go has support for tags, so that'll be my first target.

iamcodemaker commented 3 years ago

The tagstack does contain the buf number and the position though

No, it doesn't. It only contains that info for where we jumped from (which could be anywhere). It stores the bufnum, matchnum, and tagname of the current tag, which is enough to look it up from the tags file, but there is no tags file if settagstack was used. That's how vim works at least (not sure about nvim).

martskins commented 3 years ago

Ooh ok, I misunderstood that part, yeah, I see now. Thanks for clarifying.

dimbleby commented 3 years ago

917 was a previous try at this - with much the same defects. There's some relevant discussion in that MR.