Skip to content

Conversation

@MaxGyver83
Copy link

@MaxGyver83 MaxGyver83 commented Jul 29, 2023

The new buffer-local settings b:gutentags_ctags_extra_args can be used to create separate tag files for different programming languages.

Example:

autocmd FileType python let b:gutentags_ctags_extra_args = ['--languages=Python', '-o', 'tags-' . &ft] | execute 'setl tags=tags-' . &ft
autocmd FileType cpp    let b:gutentags_ctags_extra_args = ['--languages=C++', '-o', 'tags-' . &ft]    | execute 'setl tags=tags-' . &ft
autocmd FileType vim    let b:gutentags_ctags_extra_args = ['--languages=Vim', '-o', 'tags-' . &ft]    | execute 'setl tags=tags-' . &ft
autocmd FileType sh     let b:gutentags_ctags_extra_args = ['--languages=Sh', '-o', 'tags-' . &ft]     | execute 'setl tags=tags-' . &ft
autocmd FileType c      let b:gutentags_ctags_extra_args = ['-h=.c.h', '-o', 'tags-' . &ft]            | execute 'setl tags=tags-' . &ft

Updated example configuration as suggested by @ludovicchabant:

autocmd FileType python let b:gutentags_ctags_extra_args = ['--languages=Python'] | let b:gutentags_ctags_tagfile = "tags-" . &ft
autocmd FileType cpp    let b:gutentags_ctags_extra_args = ['--languages=C++']    | let b:gutentags_ctags_tagfile = "tags-" . &ft
autocmd FileType vim    let b:gutentags_ctags_extra_args = ['--languages=Vim']    | let b:gutentags_ctags_tagfile = "tags-" . &ft
autocmd FileType sh     let b:gutentags_ctags_extra_args = ['--languages=Sh']     | let b:gutentags_ctags_tagfile = "tags-" . &ft
autocmd FileType c      let b:gutentags_ctags_extra_args = ['-h=.c.h']            | let b:gutentags_ctags_tagfile = "tags-" . &ft

Fixes #264!?

@MaxGyver83
Copy link
Author

Similar to #342 but this pull request doesn't depend on an additional plugin.

@MaxGyver83
Copy link
Author

I think this feature needs an update. I got this error in a fugitive buffer:

E121: Undefined variable: b:gutentags_ctags_extra_args

I suppose get(b:, 'gutentags_ctags_extra_args', []) is wrong here because it is meant to check for this variable for the fugitive buffer but buffer-local means vim in the gutentags plugin code.

This might fix it:

diff --git a/autoload/gutentags/ctags.vim b/autoload/gutentags/ctags.vim
index 348e610..e6b417b 100644
--- a/autoload/gutentags/ctags.vim
+++ b/autoload/gutentags/ctags.vim
@@ -9,7 +9,7 @@ let g:gutentags_ctags_auto_set_tags = get(g:, 'gutentags_ctags_auto_set_tags', 1
 let g:gutentags_ctags_options_file = get(g:, 'gutentags_ctags_options_file', '.gutctags')
 let g:gutentags_ctags_check_tagfile = get(g:, 'gutentags_ctags_check_tagfile', 0)
 let g:gutentags_ctags_extra_args = get(g:, 'gutentags_ctags_extra_args', [])
-let b:gutentags_ctags_extra_args = get(b:, 'gutentags_ctags_extra_args', [])
+let s:gutentags_ctags_extra_args = get(b:, 'gutentags_ctags_extra_args', [])
 let g:gutentags_ctags_post_process_cmd = get(g:, 'gutentags_ctags_post_process_cmd', '')

 let g:gutentags_ctags_exclude = get(g:, 'gutentags_ctags_exclude', [])
@@ -173,7 +173,7 @@ function! gutentags#ctags#generate(proj_dir, tags_file, gen_opts) abort
     if l:use_tag_relative_opt
         let l:cmd += ['-O', shellescape("--tag-relative=yes")]
     endif
-    for extra_arg in g:gutentags_ctags_extra_args + b:gutentags_ctags_extra_args
+    for extra_arg in g:gutentags_ctags_extra_args + s:gutentags_ctags_extra_args
         let l:cmd += ['-O', shellescape(extra_arg)]
     endfor
     if !empty(g:gutentags_ctags_post_process_cmd)

@ludovicchabant
Copy link
Owner

ludovicchabant commented Aug 15, 2023

Hi! Thanks for this. I don't think the other PR #342 requires an additional plugin? If I understand correctly, the author just said that the change allows using another plugin they like because the tags are correctly generated for the given language.

That said, that other PR manipulates the global array in a way that can cause side-effects, whereas here we just combine the two arrays as needed, which is better IMHO.

I suppose the buffer-local variable is also more consistent with how other variables in gutentags have the global/local dichotomy too, so that's nice! However:

  1. Unless I'm mistaken, this will generate an error if b:gutentags_ctags_extra_args isn't set. You probably need to do instead getbufvar("", "gutentags_ctags_extra_args", []) or something.
  2. In your example usage, you set the -o parameter for ctags, and set the tags option in Vim. Why not set b:gutentags_ctags_tagfile instead? If it works, it's nice because it just sets two buffer-local variables for gutentags (notice how we fallback from the buffer-local to the global variable here). If it doesn't work, we might want to fix it.

@MaxGyver83
Copy link
Author

If I understand correctly, the author just said that the change allows using another plugin they like because the tags are correctly generated for the given language.

Oops, then I misunderstood him/her.

  1. Unless I'm mistaken, this will generate an error if b:gutentags_ctags_extra_args isn't set. You probably need to do instead getbufvar("", "gutentags_ctags_extra_args", []) or something.

Nice! This works. I have pushed this change.

  1. In your example usage, you set the -o parameter for ctags, and set the tags option in Vim. Why not set b:gutentags_ctags_tagfile instead? If it works, it's nice because it just sets two buffer-local variables for gutentags (notice how we fallback from the buffer-local to the global variable here). If it doesn't work, we might want to fix it.

Then I would use this in my .vimrc?

autocmd FileType python let b:gutentags_ctags_extra_args = ['--languages=Python', '-o'] | let b:gutentags_ctags_tagfile = "tags-" . &ft

This gives me:

gutentags: ctags job failed, returned: 1

tags-python isn't created.

@MaxGyver83
Copy link
Author

Some debug information:

:echo b:gutentags_ctags_tagfile
tags-python
:verbose GutentagsUpdate
gutentags: Wildignore options file is up to date.
gutentags: Running: ['/home/max/.dotfiles/vim/.vim/pack/plugins/start/vim-gutentags/plat/unix/update_tags.sh', '-e',
 'ctags', '-t', 'tags-python', '-p', '.', '-o', '/home/max/.dotfiles/vim/.vim/pack/plugins/start/vim-gutentags/res/c
tags_recursive.options', '-O', '--languages=Python', '-O', '-o', '-x', '@/tmp/nvim.max/8xyujs/0', '-x', 'tags', '-x'
, 'virtual_envs', '-x', '.ccls-cache', '-x', '.mypy_cache', '-x', '*.json', '-x', '*.rst', '-x', '*.md', '-x', '*.cs
s', '-x', '*.js', '-x', '*.html', '-x', '*.diff', '-x', '*.patch', '-x', '*.svg', '-x', '*.tex', '-x', '*.pb', '-l',
 'tags-python.log']
gutentags: In:      /home/max/.dotfiles

@MaxGyver83
Copy link
Author

OK, my bad 🙃. I forgot to delete the '-o' here:

let b:gutentags_ctags_extra_args = ['--languages=Python', '-o']

@MaxGyver83
Copy link
Author

When I opened first a .c file with b:gutentags_ctags_extra_args defined in my .vimrc and then opened a .1 file (b:gutentags_ctags_extra_args not defined), I got an error on writing the latter file: E121: Undefined variable: b:gutentags_ctags_extra_args. (This didn't happen when I opened the latter file first because in this case, b:gutentags_ctags_extra_args was initialized to an empty list.)

dae35b5 fixes this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

options support by {type} / buffer local options

2 participants