preservim / tagbar

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

Feature Request: Highlight higher scope of unshown highlit items #882

Open jamesTSCoop opened 3 weeks ago

jamesTSCoop commented 3 weeks ago

It bugs me a bit that if my cursor rests on a variable assignment line too long the highlighting for the method I'm in disappears because the variable within its scope is highlit but folded from view. I'd love to be able to configure tagbar so that there are four options:

  1. Current behaviour, highlight based on the configured tagbar_highlight_method regardless of if the tag is folded out of view or not
  2. Highlight the deepest visible tag even if the actual nearest tag is folded from view
  3. Highlight all parent tags for the current nearest tag
  4. Automatically unfold folded tags to ensure the nearest tag is in view, then fold back up to the default level when no tag in such an unfolded list is nearest
  5. Both 4 & 3.

I'm not completely sure 4 isn't already possible, I scanned the help, wiki, and issues pretty carefully but not perfectly. I'm also not completely sure I can't already configure the highlighting colour/style based on tag type, but that's another thing I think would be cool.

I'm a software developer and I'd be happy to do the coding if someone who knows this codebase can confirm that it's possible and point me in approximately the right direction!

raven42 commented 3 weeks ago

Can you please share a screenshot or something? I'm not quite understanding the issue you are describing. I'm not understanding why a variable assignment line being too long would have any impact on the tags identified by universal-ctags or on tagbar. I just want to make sure I understand your request.

As for number 4, if I understand that request, I don't believe there is any option to do this currently. Tagbar only sets the fold levels for each line, but doesn't control showing/hiding the folds.

raven42 commented 3 weeks ago

If you want to take a look at the current implementation, the function that performs the highlight is here.

https://github.com/preservim/tagbar/blob/0815b5455c792a610d28ff3e7981642c9cf05d62/autoload/tagbar.vim#L2242-L2246

There is a bit specifically about highlighting the parent instead. Is this the issue to which you are referring?

https://github.com/preservim/tagbar/blob/0815b5455c792a610d28ff3e7981642c9cf05d62/autoload/tagbar.vim#L2304-L2308

jamesTSCoop commented 3 weeks ago

Yes, the code at 2307 looks like it should be doing what I am wanting/expecting, but for me it isn't, here are some screenshots.

The cursor is on an unfolded var, the var is highlit, makes sense:

cursor_on_unfolded_var

The cursor is on a folded var, the parent scope isn't highlit, and I wish it was :(

cursor_on_foded_var

The cursor is not on a var, the function is highlit, I'm happy for a moment :)

cursor_not_on_var

raven42 commented 3 weeks ago

Ok, I see what you mean now. Ya it looks like this might be a bug based on the comments in the code. It looks like it is supposed to set the highlight identifier to the parent tag if the fold is closed, but it looks like that might now be happening correctly.

I suspect something it wrong with this logic here: https://github.com/preservim/tagbar/blob/0815b5455c792a610d28ff3e7981642c9cf05d62/autoload/tagbar.vim#L2322-L2325

It is using the same tag.name that was returned from here: https://github.com/preservim/tagbar/blob/0815b5455c792a610d28ff3e7981642c9cf05d62/autoload/tagbar.vim#L2260-L2264

So maybe it is something to do with the GetNearbyTag() function?

Unfortunately I don't have time to really dig into it right now, but it is likely something in this area. I'm thinking if we do detect that the parent is folded, we may need to change the value of tag to be the parent instead of the nearest tag. Or maybe the GetNearbyTag() needs a different argument to ignore folded tags?

jamesTSCoop commented 3 weeks ago

Thanks for the pointers though, I'm sure I can figure it out from here!