将定义的类型标记为类型

7

有没有办法在C语言中使用typedef语句定义新类型时添加语法高亮?

typedef struct {
    int a,b;
} MyStruct;

MyStruct *InitMyStruct(MyStruct *struct, int a, int b);
    ^         ^           ^               ^      ^
    +---------+-----------+               +------+
    Same Color                             Correct type color

如果原生不支持(我猜是这样),是否有任何插件可以使这个视觉提示起作用?

可能类似于https://dev59.com/GXRB5IYBdhLWcg3wCjrO。那里给出的链接是http://vim.wikia.com/wiki/Highlighting_of_method_names_in_the_definition,其中描述了如何对函数进行操作,应该可以修改它以识别结构体。 - joshhendo
感谢@joshhendo,虽然问题类似,但我正在寻找更多“阅读我的代码并突出显示”的解决方案。不是一个更普遍的解决方案。但我设法找到了解决方案。猜猜在哪里?是的,在Vim的帮助中。请查看我的答案。 - sidyll
1个回答

9

我在Vim的帮助文档中找到了准确的答案,并在此发布,以防将来有人需要。这正是我想要的:一种阅读代码并相应地进行高亮的方法。

syntax.txt

第15节:标记高亮

[...]
Only highlighting typedefs, unions and structs can be done too.  For this you
must use Exuberant ctags (found at http://ctags.sf.net).

Put these lines in your Makefile:

# Make a highlight file for types.  Requires Exuberant ctags and awk
types: types.vim
types.vim: *.[ch]
        ctags --c-kinds=gstu -o- *.[ch] |\
                awk 'BEGIN{printf("syntax keyword Type\t")}\
                        {printf("%s ", $$1)}END{print ""}' > $@

And put these lines in your .vimrc: >

   " load the types.vim highlighting file, if it exists
   autocmd BufRead,BufNewFile *.[ch] let fname = expand('<afile>:p:h') . '/types.vim'
   autocmd BufRead,BufNewFile *.[ch] if filereadable(fname)
   autocmd BufRead,BufNewFile *.[ch]   exe 'so ' . fname
   autocmd BufRead,BufNewFile *.[ch] endif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接