Thursday, August 11, 2022

Re: vim9 exported functions not recognized by ctags

On 2022-08-10, Lifepillar <lifepillar@lifepillar.me> wrote:
> On 2022-08-10, N V <nivaemail@gmail.com> wrote:
>> Hi,
>>
>> Exported functions in New vim9 are not found by exubérant ctags, universal
>> ctags and not displayed by tagbar plugin
>> https://github.com/preservim/tagbar
>>
>> Is there a work around.

I'd like to elaborate a bit more on my previous reply, as I had just
played a bit more with the issue. In the following, "Ctags" means
Universal Ctags, because that is what I am using.

Ctags supports Vim out of the box. You may check which entities are
supported with `ctags --list-kinds=vim` from the command-line. That
currently gives me:

a autocommand groups
c user-defined commands
f function definitions
m maps
v variable definitions
n vimball filename
C constant definitions

Each "kind" (i.e., entity) is identified by a letter, and has a name
(not shown) and description. You may extend Ctags support for Vim by
creating a ~/.ctags.d/vim.ctags configuration file. In that file, you
define new "kinds" and corresponding regexes to recognize them. Each new
kind is also identified by a letter: be careful not to use the already
defined letters!

My current ~/.ctags.d/vim.ctags looks as follows:

--kinddef-vim=e,export,Vim 9 exported defs
--kinddef-vim=g,global,Vim 9 global variables
--kinddef-vim=K,const,Vim 9 constants
--regex-vim=/^\s*export\s+def\s+([^(]+)/\1/e,export/
--regex-vim=/^(\s*export\s+)?const\s+(\w+)/\2/K,const/
--regex-vim=/^(\s*export\s+)?final\s+(\w+)/\2/K,const/

The value of --kinddef is a triple <letter>,<name>,<description>. Each
regex item has three parts /A/B/C/:

- A is the pattern that recognizes an entity;
- B is the value that should be returned for that entity (what Tagbar
will display);
- C is the identifier of the entity, as defined in --kinddef.

So, the logic is: "if a line matches A, return the value B of kind C".

You may check that your configuration is correct by running `ctags
--list-kinds=vim` again. With the configuration file in place, you
should get:

a autocommand groups
c user-defined commands
f function definitions
m maps
v variable definitions
n vimball filename
C constant definitions
e Vim 9 exported defs
g Vim 9 global variables
K Vim 9 constants

Now, you need to configure Tagbar. Create
`~/.vim/after/ftplugin/vim.vim` and execute:

:TagbarGetTypeConfig vim

This will paste into the buffer Tagbar's current configuration, which
should look like this:

let g:tagbar_type_vim = {
\ 'kinds' : [
\ 'n:vimball filenames',
\ 'v:variables:1:0',
\ 'f:functions',
\ 'a:autocommand groups:1',
\ 'c:commands:0:0',
\ 'm:maps:1:0',
\ ],
\ }

Now, it's a matter of adding the new kinds, possibly removing those that
you don't need, and reordering the items the way you want them
displayed. I like to use vim9script, so my current script looks as
follows:

vim9script

if !exists('g:tagbar_type_vim')
g:tagbar_type_vim = {
'kinds': [
'e:exported defs',
'f:functions',
'v:variables:1:0',
'g:global variables',
'K:constants',
'c:commands:0:0',
'a:autocommand groups:1',
'm:maps:1:0',
],
}
endif

That's it! You may need to restart Vim for the changes to take effect.

You will notice that def's parameters are not shown for exported defs,
and that is intentional, because I don't find them useful. But you may
of course adapt Ctags's regexps to include those, and in general to fit
your taste.

Hope this helps,
Life.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/td29is%24153d%241%40ciao.gmane.io.

No comments: