ctags, jsctags/doctorjs, Tagbar step by step
Asked Answered
F

1

6

I need help on setting up ctags, jsctags, and tagbar so I can have a workable Javascript editing envrionment. I got everything installed but couldn't get an idea how ctags and jsctags work together so I don't know how to configure properly. I have done quite a bit Google around but the information is pretty broken and lacks consistency. I got an error similar to this post, ctags and tagbar configuration are out of sync. I'm on OS X mountain lion and iterm2.

Any help would be greatly appreciated. A step by step instructions would be excellent.

Thx.

Fredel answered 15/3, 2013 at 6:1 Comment(0)
G
6

First, you have to understand that jsctags and ctags will probably never be in sync. Even if you have only a simple function, both programs may output slightly different informations. Thus it is advised to use one or the other but not both.

Second, AFAIK, TagBar doesn't need to be configured to use jsctags instead of ctags. If you are doing JavaScript it will simply default to jsctags if it's available. TagBar shouldn't complain if you have a working jsctags.

Third, for its own tag-related functionalities (:tag, :tselect, <C-]>, etc.), Vim only relies on the presence of one or more physical tags files. Whether those files are generated by ctags, jsctags or whatevertags is not a concern as long as they are generated correctly.

And now we arrive at the fourth point which is where the pain really is: neither Tagbar nor its older cousin TagList actually use a physical tags file. They tap ctags or jsctags directly without even trying to use or update any existing tags file. Because of that, and the fact that TagBar only deals with the current buffer, your own physical tags file (and thus Vim's tag-related functionalities) and TagBar are almost guaranteed to be out of sync. Even if you use the same indexer.

So… I'd advise you to use either ctags or jsctags and forget anything about syncing TagBar's and Vim's tags-related stuff as both things are completely separate:

  • Use TagBar to understand/navigate in your current buffer.

  • Use Vim's tag-related functionalities to move around your project.


Random thoughts…

  • For Vim to locate your tags file(s) easily, you should put this line in your ~/.vimrc:

    set tags=./tags,tags;/
    

    ./tags means "look for a tags file in the directory of the current file", tags means "look for a tags file in the working directory", ;/ means "keep looking up and up until you reach /".

  • The TagBar wiki talks about a bug in jsctags, make sure that you are not concerned by it.

  • jsctags is better than ctags when you write crazy "modern" JavaScript with lots of callbacks and self-executing functions. If your JavaScript is more traditional, ctags may be enough.

Glasswork answered 15/3, 2013 at 6:50 Comment(4)
romainl, thanks for the explanation. I think I have got jsctags working but the tagbar only showed the variables, methods, closures, etc without breakdown/hierarchy, like an objects tree, or referencing stuff from external libraries (require(..)). Is it the way it's supposed to be? Maybe ctags can do a better job? The biggest value would be able to cursor on a variable and go to the source file that defines it. I hope I make sense.Fredel
You are supposed to have a scoped hierarchical view. Jsctags should do a better job at scoping than ctags so what you describe sounds like that bug in jsctags mentioned in the TagBar wiki. You should try the issue tracker on Github for a more focused help, I'm afraid. If you have an up-to-date tags file, hitting <C-]> with the cursor on a variable should jump you to the definition. See this for a more JS-friendly command.Glasswork
Thx again. The remap line is very helpful. How do you normally organize your tags file (or how do you call jsctags command)? The convoluted nature of dependency structure in JS makes things less intuitive. E.g., the core modules are sitting in the node but the project files are in different path...Btw, the scope of tags happens to me...The proposed fix, according to the wiki, isn't workable as it requires to downgrade node.js! Any other workaround? Thx again.Fredel
I don't use jsctags, it's too buggy/unstable. My JavaScript coding style is quite old school and I don't do any nodejs dev so ctags does a good enough job with my code. I've stopped using TagBar (and TagList before) as well because I only care about navigation and it sucks at it. I usually have a single tags file (auto-generated by EasyTags) at the root of my project and use CtrlP's :CtrlPBufTag to navigate in the current buffer and :CtrlPTag to navigate within the project. I also use those two mapping in my ~/.vimrc very often.Glasswork

© 2022 - 2024 — McMap. All rights reserved.