preservim / tagbar

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

Remap Filetypes #721

Closed matheusfillipe closed 3 years ago

matheusfillipe commented 3 years ago

Is it possible to make it accept a filetype as if it was another? Like I have an extension that provides highlight and autocomplete for arduino files which I can even generate tags for but tagbar will be empty when i open if If i set ft to c tagbar works.

Can I remap those filetypes somehow?

alerque commented 3 years ago

Yes, of course you can setup your own customized setup. See the Wiki for numerous examples of how to add filetypes. You can start with the default mapping for C if that works for you, then customize it for your file type. There are also controls for running other tag generators besides ctags if that's what you need.

matheusfillipe commented 3 years ago

I just needed to make arduino file types to be interpreted as c or c++. Ill take a look, thanks!

matheusfillipe commented 3 years ago

So i tried adding this:

let g:tagbar_type_arduino = { 'ctagsbin': 'ctags', 'ctagsargs' : '--language-force=c++' }

But then I get this error now in any buffer:

Error detected while processing function airline#extensions#tagbar
#currenttag[14]..tagbar#currenttag[22]..<SNR>157_Init[10]..<SNR>15
7_InitTypes[107]..<SNR>157_LoadUserTypeDefs[23]..<SNR>160_createKi
nddict:
line    2:
E716: Key not present in Dictionary: kinds
Press ENTER or type command to continue
Error detected while processing function airline#extensions#tagbar
#currenttag[14]..tagbar#currenttag[22]..<SNR>157_Init[10]..<SNR>15
7_InitTypes[107]..<SNR>157_LoadUserTypeDefs[23]..<SNR>160_createKi
nddict:
line    2:
E15: Invalid expression: self.kinds

I have tried lazy loading: let g:airline#extensions#tagbar#enabled = 0

Not working still.

alerque commented 3 years ago

You probably want to copy the kinds array from the C++ type definition. You are only telling the plugin how to generate tags but not how to interpret them.

Also rather than just hacking this together, if Arduino files really are C++ equivalent syntax, I highly recommend contributing to the upstream ctags project. The project is active these days, and if what you are trying to do actually works it should be relatively trivial to add Arduino support, and that way everybody gets first class support without having to add hacks.

raven42 commented 3 years ago

This might be better to have a vim definition that treats arduino files as c file types (or c++ if you'd prefer). Rather than making this a specific tagbar configuration, it might be better as a global filetype setting for that extension from a vim perspective. That way you get all the other c filetype info like syntax, auto-indent, etc.

Try adding this into ~/.vim/syntax/ino.vim

if exists('b:current_syntax')
    finish
endif

let b:current_syntax = 'c'

This will change the filetype syntax to c. You can also define custom highlight definitions or syntax definitions if you want in this file if you want to differentiate between the normal c filetype syntax.

This will in effect cause vim to treat this file as a c filetype in all respects. That should include tagbar too I believe (though I haven't tried that).

matheusfillipe commented 3 years ago

@raven42 That would be a good solution if I wasn't using a arduino plugin that does syntax highlight for more specific arduino functions and helps me with compiling and sending to the board. I believe if i do that I am going to loose it so... not cool.

Any way arduino's syntax is exactly c++, if there was a way to set this "remap" just for the tagbar extension it would work. As i said if i change the filetype to c++ it works fine on tagbar but then I loose the arduino plugin.

@alerque Any idea where can I find the kinds array vim/tagbar is currently using for c++?

raven42 commented 3 years ago

ah ok... ya if there are other plugins using that file type, then ya that would be a problem.

I actually tried a couple things out and looks like vim already recognizes the .ino extension as a filetype arduino. So this gets pretty simple. With the following change in the tagbar code it should map all c++ definitions to arduino files as well.

diff --git a/autoload/tagbar/types/uctags.vim b/autoload/tagbar/types/uctags.vim
index 58fe65d..f9612bf 100644
--- a/autoload/tagbar/types/uctags.vim
+++ b/autoload/tagbar/types/uctags.vim
@@ -250,6 +250,7 @@ function! tagbar#types#uctags#init(supported_types) abort
     \ }
     let types.cpp = type_cpp
     let types.cuda = type_cpp
+    let types.arduino = type_cpp
     " C# {{{1
     let type_cs = tagbar#prototypes#typeinfo#new()
     let type_cs.ctagstype = 'c#'
alerque commented 3 years ago

https://github.com/preservim/tagbar/blob/b63e8cb83f08165d15474ea1291c51f6661f1f7e/autoload/tagbar/types/uctags.vim#L221-L235 has the C++ definition for kinds of tags.

matheusfillipe commented 3 years ago

@alerque Thanks! @raven42 Exactly the solution I thought but couldn't figure out how! Is working fine now! I hope that commit gets merged without problems ;)