54
votes

A little something that could be borrowed from IDEs. So the idea would be to highlight function arguments (and maybe scoped variable names) inside function bodies. This is the default behaviour for some C:

plain vim highlighting

Well, if I were to place the cursor inside func I would like to see the arguments foo and bar highlighted to follow the algorithm logic better. Notice that the similarly named foo in func2 wouldn't get highlit. This luxury could be omitted though...

func hilighted

Using locally scoped variables, I would also like have locally initialized variables highlit:

highlight <code>i</code> inside for

Finally to redemonstrate the luxury:

luxury again

Not so trivial to write this. I used the C to give a general idea. Really I could use this for Scheme/Clojure programming better:

for clojure tooinside let construct

This should recognize let, loop, for, doseq bindings for instance.

My vimscript-fu isn't that strong; I suspect we would need to

  • Parse (non-regexply?) the arguments from the function definition under the cursor. This would be language specific of course. My priority would be Clojure.
  • define a syntax region to cover the given function/scope only
  • give the required syntax matches

As a function this could be mapped to a key (if very resource intensive) or CursorMoved if not so slow.

Okay, now. Has anyone written/found something like this? Do the vimscript gurus have an idea on how to actually start writing such a script?

Sorry about slight offtopicness and bad formatting. Feel free to edit/format. Or vote to close.

2
supercool topic. this highlight really improve readability of algorithm. some news on that? - nkint
It is totally feasible for C/C++/Obj-C. I know that it is possible to use Python/Clang to get the scope of current cursor as well as signature of current function. Source: eli.thegreenplace.net/2011/07/03/parsing-c-in-python-with-clang. It should not be difficult to write a vim plugin with this. - Thanh DK
I was so close to downvoting you for posting images of code instead of text... but then I read the question. ;) - Bill the Lizard
this is actually suprisingly non-trivial to do in vim. The big problem is that it doesn't do semantic parsing of the language, so knowing what is "the argument list of a function" is hard, especially in a language agnostic way. ditto with scoping the highlights only to the function. in an ide its trivial, cause they usually have the full AST in memory, but at the same time, writing an ide is not comparable to a vim syntax file... - Matt Briggs
@MattBriggs: well. Libclang was proposed and it could be used for C/C++ using Vim-Python bindings. And for LISPs the AST parsing is easier. :) That's correct that we have to rely on language specific scripts but that's just something we have to live with... - mike3996

2 Answers

6
votes

This is much harder than it sounds, and borderline-impossible with the vimscript API as it stands, because you don't just need to parse the file; if you want it to work well, you need to parse the file incrementally. That's why regular syntax files are limited to what you can do with regexes - when you change a few characters, vim can figure out what's changed in the syntax highlighting, without redoing the whole file.

The vim syntax highlighter is limited to dealing with regexes, but if you're hellbent on doing this, you can roll your own parser in vimscript, and have it generate a buffer-local syntax that refers to tokens in the file by line and column, using the \%l and \%c atoms in a regex. This would have to be rerun after every change. Unfortunately there's no autocmd for "file changed", but there is the CursorHold autocmd, which runs when you've been idle for a configurable duration.

1
votes

One possible solution can be found here. Not the best way because it highlights every occurrence in the whole file and you have to give the command every time (probably the second one can be avoided, don't know about the first). Give it a look though.