突出显示vim中函数体中的参数 [英] Highlight arguments in function body in vim

查看:140
本文介绍了突出显示vim中函数体中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些可以从IDE借来的东西。因此,想法是在函数体内突出显示函数参数(以及可能的作用域变量名)。这是一些C的默认行为:





好吧,如果我把光标放在 func code> foo 和 bar 突出显示以更好地遵循算法逻辑。注意,在 func2 中类似命名的 foo 这个豪华可以省略虽然...





使用本地作用域变量,我也希望本地初始化变量highlit:





<最后再次展现奢华:





不要这么简单。我用C给了一个大致的想法。真的,我可以用于Scheme / Clojure编程更好:






这应该识别 let loop >, doseq



我的vimscript-我怀疑我们需要




  • 解析(非regexply?)光标下的函数定义的参数。这当然是语言特定的。

  • 定义语法区域以覆盖给定的函数/范围

  • 提供所需的语法匹配



作为一个函数,这可以映射到一个键(如果资源非常密集)或 CursorMoved if不太慢。



好吧,现在。有没有人写/发现这样的东西? vimscript大师对于如何真正开始编写这样的脚本有一个想法?



对于轻微的offtopicness和格式不好,抱歉。随意编辑/格式。

解决方案

这比你想象的要困难得多,而且与vimscript API无关,因为你不需要解析文件;如果您希望它工作正常,您需要递增地解析文件 。这就是为什么正常的语法文件限于使用正则表达式可以做的事情 - 当你改变几个字符时,vim可以找出语法高亮中的变化,而不必重做整个文件。



vim语法高亮工具仅限于处理正则表达式,但是如果你这样做,你可以在vimscript中自己解析器,并且生成一个缓冲区局部语法,引用文件中的标记按行和列,在正则表达式中使用\%l和\%c原子。这将必须在每次更改后重新运行。不幸的是,没有autocmd为文件更改,但有CursorHold autocmd,运行时,你已经闲置了一个可配置的时间。


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:

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...

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

Finally to redemonstrate the luxury:

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:

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.

解决方案

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.

这篇关于突出显示vim中函数体中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆