preservim / tagbar

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

Is there a way to classify static functions and global functions? #766

Closed uudiin closed 3 years ago

uudiin commented 3 years ago

When there are many functions in a C language file, it is a very friendly function to display the global function and the local static function separately.

Thanks, Tianjia

alerque commented 3 years ago

Does ctags return annotations with that information?

raven42 commented 3 years ago

@uudiin The local/global information should already be present in the tagbar window. Static functions should be seen as local scope indicated in the tagbar window by a - indicator in front of the function. The same will be true for other variable types as well such as a static int.

Example (note how localFunction1() has a - in front of it): image

Unfortunately there is no way to separate these into a separate hierarchy though. At least not an easy way. From the tagbar parsing of the ctags output, it sees both local static functions and externally available functions as the same ctag kind.

Note the output for localFunction1() and function1() are the same tag kind f, only localFunction1() is noted as file: scope.

  Option: --language-force=c
  Option: --c-kinds=hdpgetsumvf
Initialize parser: C
Reading command line arguments
OPENING test.c as C language file [new]
Initialize parser: CPreProcessor
...
localFuntion1   test.c  /^static int localFuntion1(int c) {$/;" f   line:19 typeref:typename:int    file:   signature:(int c)   end:21
function1   test.c  /^int function1(int c) {$/;"    f   line:23 typeref:typename:int    signature:(int c)   end:25
...

One option would be to use the g:tagbar_sort = 0 in your .vimrc. This will sort according to appearance of the function in the file instead of sorting by name. I write in C primarily myself, and most code I use generally has all statically defined functions at the top, and global functions at the bottom (sometimes only 1 or 2 per file with the rest being static). So sorting by place in the file might work.

Currently global tags and local tags are all treated the same and populate under the same parent kind label (in this case functions). It should be possible to treat file scope tags differently, but that would require reworking how the tags are parsed and how they are categorized. It wouldn't be quite as simple as one might hope. If you want to take a stab at it I can help test it and might be able to provide pointers if I have time.

uudiin commented 3 years ago

@raven42 Thanks for your reply, it is very helpful, I was negligent before, the - mark is enough to me. Thanks a lot.

Best regards, Tianjia

uudiin commented 3 years ago

@alerque Looks like it already exists, you can refer to @raven42 's reply. Thanks.