Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight arguments in function body in vim

Tags:

vim

clojure

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.

like image 572
mike3996 Avatar asked Feb 19 '12 11:02

mike3996


2 Answers

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.

like image 92
jimrandomh Avatar answered Sep 21 '22 05:09

jimrandomh


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.

like image 44
George Karanikas Avatar answered Sep 20 '22 05:09

George Karanikas