Is it possible, using ctags with Vim, to extract all the enum values for a given enum?

For example, if I have the following enum type:

typedef enum fruit {
    APPLE,
    ORANGE,
    PEAR,
} fruit_t;

which generates the following lines in my tags file (using default --c-kinds):

APPLE   minex.c /^    APPLE,$/;"    e   enum:fruit  file:
ORANGE  minex.c /^    ORANGE,$/;"   e   enum:fruit  file:
PEAR    minex.c /^    PEAR,$/;" e   enum:fruit  file:
fruit   minex.c /^typedef enum fruit$/;"    g   file:
fruit_t minex.c /^    } fruit_t;$/;"    t   typeref:enum:fruit  file:

is there any way of specifying fruit or fruit_t and obtaining the values [ 'APPLE', 'ORANGE', 'PEAR' ]?

In Vim I had hoped that :tselect fruit would do the trick, but it doesn't.

Of course I could grep the tags file and process the results myself, but for a very large file this would be expensive, and I am hoping there is a built-in way of getting such basic information.

有帮助吗?

解决方案

Yes, you can, by filtering the results of the taglist() function, which give you a convenient programmatic access to the tags database.

:echo map(filter(taglist('.*'), 'has_key(v:val, "enum") && v:val.enum ==# "fruit"'), 'v:val.name')
['APPLE', 'ORANGE', 'PEAR']

What this does is:

  1. Get all tags (.* regular expression)
  2. Keep (filter()) all tags that have (has_key()) an enum attribute, and whose name is exactly (==#) fruit.
  3. From the resulting objects, select (map()) only the name attribute.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top