preservim / tagbar

Vim plugin that displays tags in a window, ordered by scope
https://preservim.github.io/tagbar
Other
6.1k stars 485 forks source link

Why can't I use TagbarJumpNext directly #817

Closed liangjunheng closed 2 years ago

liangjunheng commented 2 years ago

I need to use command TagbarToggle before I can use command TagbarJumpNext. I hope I can use the command TagbarJumpNext directly.

raven42 commented 2 years ago

There is an optimization in place so tagbar does not actually analyze the open file until the :TagbarToggle is issued and tagbar is opened for the first time. If the file isn't analyzed, then tagbar doesn't know about any of the tags and so it doesn't know what tag to jump to.

Statusline alternative

An alternative would be to make use of a plugin like lightline or similar and set a callback function for the status line to show the nearest tag. This would also trigger tagbar to analyze the file. For example, I have this in my configuration.

let g:lightline = {
    \ 'active': {
    \   'left': [['mode', 'paste', 'modified'], ['readonly', 'filename'], ['functionName']],
    \   'right': [['lineinfo'], ['percent'], ['fileformat', 'fileencoding', 'filetype']]
    \ }
    ...
    \ 'component_function': {
        ...
    \   'functionName': 'LightlineFunctionName',
    \ },
    \ }
\ }

" ---- LightlineFunctionName() {{{2
function! LightlineFunctionName()
    return tagbar#currenttag("%s", "", 'f', 'nearest-stl')
endfunction

So even if I don't have tagbar open at the start, the lineline plugin will populate the statusline and use the callback tagbar#currenttag() which will force analyzing the file.

ForceUpdate alternative

Another alternative might be to call tagbar#ForceUpdate() in your .vimrc after plugins are loaded. This should trigger an update as well, though I have not tested this method, so it may not work if the file has not been analyzed yet. Just looking at the code I am thinking this might be worth a shot.

liangjunheng commented 2 years ago

thank u