Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Allow tags jumping with no need to open the tags panel first #845

Closed
pidgeon777 opened this issue Feb 2, 2023 · 8 comments · Fixed by #897
Closed

[Feature] Allow tags jumping with no need to open the tags panel first #845

pidgeon777 opened this issue Feb 2, 2023 · 8 comments · Fixed by #897

Comments

@pidgeon777
Copy link

Currently, to use the TagbarJumpNext and TagbarJumpPrev commands, you need to open the tagbar panel first.

It would be great to allow jumping to the next/previous tag without opening the tagbar panel first, eventually by setting a custom option to enable this behaviour.

@raven42
Copy link
Collaborator

raven42 commented Feb 23, 2023

This can be done a little bit indirectly. The reason it is not working for you without opening the tagbar window is because the file has not yet been scanned for tags. This is actually an optimization that is in the plugin to avoid using extra resources if not in use. I.E. there is no reason to load the tags for a file unless you open the tagbar window or are using the tags.

To trigger a call to scan the file for tags, you can add the following to your .vimrc somewhere after the file has loaded.

call tagbar#Update()

This should trigger scanning the file for tags, and thus the :TagbarJumpNext / :TagbarJumpPrev command should work.

@pidgeon777
Copy link
Author

call tagbar#Update() leads to an error:

Errore/i eseguendo function tagbar#Update[1]..<SNR>104_AutoUpdate[38]..<SNR>104_IsValidFile:
riga   29:
E121: Undefined variable: s:known_types
E116: Argomenti non validi per la funzione: has_key

@raven42
Copy link
Collaborator

raven42 commented Feb 23, 2023

Hmm... are you by chance using vim 9? I've seen a similar error on a test setup with vim 9 that I have that I haven't been able to really track down yet. From what I can tell it seems to be something with vim 9 doing the initial load a little differently than vim 8, where calling a tagbar function inside the .vimrc causes a similar error, but once vim is fully loaded, then calling the same function seems to work just fine.

@pidgeon777
Copy link
Author

pidgeon777 commented Feb 23, 2023

I'm using Neovim version v0.8.3 stable.

@raven42
Copy link
Collaborator

raven42 commented Feb 23, 2023

hm. Ok. could be something similar. I'll try to dig into it more and see if I can figure anything out. But as of now, that is about the only way I can think of to automatically have the :TagbarJump... commands work.

@shaobosong
Copy link

shaobosong commented Dec 27, 2024

This can be done a little bit indirectly. The reason it is not working for you without opening the tagbar window is because the file has not yet been scanned for tags. This is actually an optimization that is in the plugin to avoid using extra resources if not in use. I.E. there is no reason to load the tags for a file unless you open the tagbar window or are using the tags.

To trigger a call to scan the file for tags, you can add the following to your .vimrc somewhere after the file has loaded.

call tagbar#Update()

This should trigger scanning the file for tags, and thus the :TagbarJumpNext / :TagbarJumpPrev command should work.

" autoload/tagbar.vim
function! tagbar#Init() abort
    call s:Init(0)
endfunction
augroup MyAugroup
  autocmd BufEnter * call tagbar#Init()|call tagbar#Update()
augroup END
noremap <silent> [[ :<C-U>TagbarJumpPrev<CR>
noremap <silent> ]] :<C-U>TagbarJumpNext<CR>

It works well for me.

@raven42
Copy link
Collaborator

raven42 commented Dec 27, 2024

@shaobosong - Thanks for the tip. I was able to incorporate a similar functionality into the tagbar plugin directly. The jump should now trigger an init and update if not yet done.

@pidgeon777 give this latest a try and see how it works without any need for the workarounds. If you still have issues, feel free to re-open the issue.

@shaobosong
Copy link

Thanks for your fix, @raven42 .
But I found it important for me to call s:AutoUpdate(fnamemodify(expand('%'), ':p'), 0) in tagbar#Update() for tagbar#jumpToNearbyTag. I proposed a revision like this:

 function! tagbar#jumpToNearbyTag(direction, ...) abort
-    if s:init_done == 0
-        call tagbar#Update()
-    endif
+    call tagbar#Update()
     let search_method = a:0 >= 1 ? a:1 : 'nearest-stl'
     let flags = a:0 >= 2 ? a:2 : ''

In addition, I observed that my cursor exhibits strange behavior, including switching windows, jumping between tags, and then switching back when two windows share the same buffer. There might also be other unexpected behaviors affecting other users. To address this, I proposed a revision as follows:

@@ -2355,6 +2355,7 @@ endfunction
 function! s:JumpToTag(stay_in_tagbar, ...) abort
     let taginfo = a:0 > 0 ? a:1 : s:GetTagInfo(line('.'), 1)
     let force_lazy_scroll = a:0 > 1 ? a:2 : 0
+    let hold_window = a:0 > 2 ? a:3 : 0

     if empty(taginfo) || !taginfo.isNormalTag()
         " Cursor line not on a tag. Check if this is the start of a foldable
@@ -2384,7 +2385,9 @@ function! s:JumpToTag(stay_in_tagbar, ...) abort
         let autoclose = 0
     endif

-    call s:GotoFileWindow(taginfo.fileinfo)
+    if !hold_window
+        call s:GotoFileWindow(taginfo.fileinfo)
+    endif

     " Mark current position so it can be jumped back to
     mark '
@@ -3245,6 +3248,7 @@ function! s:JumpToNearbyTag(direction, request, flags) abort

     let lnum = a:direction > 0 ? line('.') + 1 : line('.') - 1
     let lazy_scroll = a:flags =~# 's' ? 0 : 1
+    let hold_window = 1

     let tag = s:GetNearbyTag(a:request, 1, lnum, a:direction, 1)

@@ -3258,7 +3262,7 @@ function! s:JumpToNearbyTag(direction, request, flags) abort
         return
     endif

-    call s:JumpToTag(1, tag, lazy_scroll)
+    call s:JumpToTag(1, tag, lazy_scroll, hold_window)
 endfunction

 " s:GetTagInfo() {{{2

This is an early-stage patch that only addresses the specific issue I encountered. I hope you can help improve and refine it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants