Class & Function Names Highlighting in Vim

class & function names highlighting in Vim

Interestingly, the syntax highlighters in VIM don't support applying a syntax to identifiers or function names - at least not the syntax highlighters for C and C++. So, even if you do:

:hi Function guifg=red

or

:hi Identifier guifg=red

it doesn't give these a color. I just seems to be not much more than keywords and constants for these languages.

Here, someone has started extending the cpp syntax file to support method names. It's a start I guess.
http://vim.wikia.com/wiki/Highlighting_of_method_names_in_the_definition

Adding custom highlighting for project's API function names in Vim

Try putting this in ~/.vim/after/syntax/c.vim:

syn keyword Keyword func_name1 func_name2 func_name3

you can see the defined highlight groups with:

:highlight

if you want to pick your colors:

syn keyword Myfunctions func_name1 func_name2
highlight Myfunctions guifg=red

assuming you use the GUI version and you like red, check :help highlight for details.

If you want to keep this particular highlight local to a project instead of applying it to every C file, you can add this to your .vimrc

au BufNewFile,BufRead *my_project/* source ~/.vim/myproject_syntax.vim

of course the path and the name and location of the syntax file are totally free.

vim custom syntax, highlight class occurrences with different colors

Syntax highlighting associates a keyword, pattern match, or region with a syntax group. A corresponding highlight group then (directly or indirectly through linked groups) determines the color and formatting of the text.

In order to have different (background or otherwise) colors for each class name, you'd have to define different syntax groups, and assign different highlight groups, too. Your syntax file would not only have a fixed set of :syntax match commands, but also a loop that extracts matches from the current buffer and builds the corresponding :syntax match and :highlight command as the syntax loads (using :execute).

Then you have the problem of updating, if the user adds or changes class names. Normally, a syntax is static, so once it loads, it's done. In your case, you'd have to define :autocmds that periodically re-scan the buffer, and add new class names (and maybe even recycle unused highlight group names, so you don't run out of colors). The CursorHold event would be a good candidate for it, but there will be a delay until the colors show up. The available colors is another problem if you want to make this syntax available to other users. The number of colors can vary wildly, and coming up with background colors that work well with various colorschemes is difficult.

Summary

It is possible, but it would be unusual for a syntax, and have side effects like delays in updating or poor performance. (I've seen this used for highlighting function names from the tags file, though.) Some users definitely would want to turn this off.

Alternative

For small files with few (or very distinct) class names, this additional highlighting probably isn't necessary. For large files with many classes, having everything light up would make it appear like a Christmas tree, and all the colors could be more distracting than helpful. I'd rather leave it to the user to do such highlighting of some classes of interest, on demand. My Mark plugin provides the generic functionality for this, in a way that does not interfere with syntax highlighting, and it ships with color palettes that look like text marker highlightings. I use this often to have better orientation in log files or legacy code bases. (The plugin page has links to alternative plugins; there are a few.)

Your syntax

  • Group names typically have a common prefix that's identical to the name of your syntax. If that is cel, use celMethodCall instead of methodCall, and so on.
  • I would put the :hi commands all at the bottom; most syntax plugins do it like this.
  • Especially if you intend to later share the syntax, favor :hi linking to existing syntax groups (:help highlight-groups) over defining your own colors. Even for your personal use, defining your colors in your ~/.vimrc has the benefit of having a single place to adapt and reuse it, instead of hunting around various syntax scripts.
  • By using :hi def, users can customize the syntax, e.g. in their ~/.vimrc. :help 44.12 has more information on writing syntax plugins.

Excluding the pattern for vim syntax highlighting

When integrating with an existing syntax script (here: $VIMRUNTIME/syntax/rst.vim), you need to consider the existing syntax groups. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin. (I maintain an extended fork.)

On your example headings, I see that the .. item:: part is matched by rstExplicitMarkup, and the remainder (what you want to highlight) by rstExDirective.

Assuming that you want to integrate with (and not completely override) these, you need your syntax group to be contained inside the latter. This can be done via containedin=rstExDirective.

Another pitfall is that \zs limits the highlighting, but internally still matches the whole text. In combination with syntax highlighting, this means that the existing rstExplicitMarkup prevents a match of your pattern. If you use a positive lookbehind (:help /\@<=) instead, it'll work:

syn match rstHeading /\%([.+].*[:+] \)\@<=.*/ containedin=rstExDirective

Of course, to actually see any highlighting, you also need to define or link a highlight group to your new syntax group:

hi link rstHeading Title

vim syntax highlighting for python variables and functions?

There is not really a difference between variables and functions in python (both are first-class objects in python). So that's pretty much impossible without actually running the code and testing if callable(var) is true.

And there are always cases where such a behaviour would be confusing:

class Dummy(object):
pass
foo = Dummy()
if False:
foo()
foo.__call__ = lambda self: 'meow'
foo.x = 'y'
foo()

When would you highlight foo as a function now? It cannot be called until after the __call__ assignment but having different syntax highlighting for the same object would be pretty confusing. Of course the example is rather stupid. But it shows easily why it's not really possible to do what you want in python. You could make it even more complicated by using inheritance and metaclasses.



Related Topics



Leave a reply



Submit