简体   繁体   English

在VIM中:如何在“ C”文件中突出显示“局部变量”

[英]In VIM: How to highlight “local variables” in a “C” file

I have highlighted all Symbols by using tags file & highlight option. 我已经使用标签文件和突出显示选项突出显示了所有符号。

But I could not able to highlight my local variables. 但是我无法突出显示我的局部变量。

I have an idea, that is, VIM already supports autocompletion of keywords for a current file, it does autocompletion of my local variable, so, if I get a list of keywords for my current file then I will highlight those keywords by using "highlight" vim command. 我有个主意,就是VIM已经支持当前文件的关键字自动补全,它可以自动完成我的本地变量,因此,如果我得到了当前文件的关键字列表,那么我将使用“突出显示”来突出显示这些关键字。 “ vim命令。

But problems is, I don't know, how to get a list of keywords for a current file. 但是问题是,我不知道如何获取当前文件的关键字列表。

You can highlight recognised names using the tags file as long as the tags file is generated with the --c-kinds=+l to ensure that it includes local variables. 只要标签文件是使用--c-kinds=+l生成的,以确保其中包含局部变量,就可以使用标签文件突出显示可识别的名称。 However, there is currently no realistic way to identify the scope of those variables (ctags does not provide much information), therefore Vim will not distinguish between variables in one function and another: 但是,目前还没有确定这些变量范围的现实方法(ctags没有提供太多信息),因此Vim不会区分一个函数和另一个函数中的变量:

void main(void)
{
    int MyVariable; // Highlighted

}

int MyFunction(void)
{
    int MyFunctionVariable; // Highlighted

    MyVariable = 1; // Syntax error, but still highlighted
}

It could be done by parsing the C file in a little more detail and creating syntax regions for each function, but it is far from easy (and it would be incompatible with plugins like rainbow.vim as Vim doesn't support overlapping regions). 可以通过更详细地解析C文件并为每个函数创建语法区域来完成此操作,但这远非易事(而且与Rainbow.vim之类的插件不兼容,因为Vim不支持重叠区域)。

On a related note, you may also be interested in my tag highlighting plugin available here . 在一个相关的说明,您还可能有兴趣在自己的广告代码高亮插件可在这里 It will highlight local variables (if b:TypesFileIncludeLocals is set to 1 in the buffer open when running :UpdateTypesFile ), but it doesn't deal with the scope of local variables. 它会突出显示局部变量(如果在运行:UpdateTypesFile时打开的缓冲区中b:TypesFileIncludeLocals设置为1 ),但它不会处理局部变量的范围。 It does, however offer a lot more highlighting colour variations than the highlighting suggested in :help tag-highlight . 但是,它确实提供了比:help tag-highlight建议的突出显示更多的突出显示颜色变化。 Note that your colour scheme will have to have highlights defined for lots of extra groups (eg GlobalVariable , LocalVariable , DefinedName etc) to take full advantage of it. 请注意,您的配色方案必须为许多额外的组(例如GlobalVariableLocalVariableDefinedName等)定义高光才能充分利用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM