简体   繁体   English

如何调试 vim indentexpr 脚本?

[英]How do I debug vim indentexpr script?

I have downloaded a verilog/systemverilog indent file which looks really comprehensive.我已经下载了一个看起来非常全面的 verilog/systemverilog 缩进文件。 However, there is a problem: it doesn't work.但是,有一个问题:它不起作用。 I'm looking at the vimscript code and want to both fix it (I'm on vim 8.2, maybe there's a version mismatch) as well as enhance it.我正在查看 vimscript 代码并希望修复它(我在 vim 8.2 上,可能存在版本不匹配)以及增强它。 However, I'm running into issues debugging.但是,我遇到了调试问题。 Specifically, indentexpr scripts have a variable v:lnum which is set when indentkey is pressed and the indentexpr is evaluated.具体来说,indentexpr 脚本有一个变量 v:lnum,它在按下 indentkey 并评估 indentexr 时设置。

BUT, I don't know of a way to enter debug mode on just the indentexpr call.但是,我不知道仅在 indentexpr 调用上进入调试模式的方法。 I tried manually calling the function inside the vimscript but that leaves v:lnum as some garbage number (well, the last line to invoke indentexpr).我尝试在 vimscript 中手动调用 function ,但这将 v:lnum 作为一些垃圾编号(好吧,调用 indentexpr 的最后一行)。 Is there a way to enter debug mode when I actually hit a key that invokes indentexpr?当我实际按下调用 indentexpr 的键时,有没有办法进入调试模式?

The best way for v:lnum to be used is as an argument to the function.使用v:lnum的最佳方式是作为 function 的参数。 Here's an example: vim-ruby's indentexpr setting used to look like this:这是一个例子:vim-ruby 的 indentexpr 设置曾经看起来像这样:

setlocal indentexpr=GetRubyIndent()

Inside the function, the variable v:lnum was used to get the line number that it was called on.在 function 内部,变量v:lnum用于获取调用它的行号。 This, as you've discovered, is pretty inconvenient.正如您所发现的,这非常不方便。 So, the better way is:所以,更好的方法是:

setlocal indentexpr=GetRubyIndent(v:lnum)

So, find the place in the script where the indentexpr is set and change it to take the magic v:lnum variable as an argument.因此,在脚本中找到设置indentexpr的位置并将其更改为将魔术v:lnum变量作为参数。 You could then rewrite the function itself to take a single argument:然后,您可以重写 function 本身以采用单个参数:

function! GetRubyIndent(lnum)

Now, in that function, the a:lnum variable will be the line number, and you can call the function with that argument.现在,在 function 中, a:lnum变量将是行号,您可以使用该参数调用 function。 Search-and-replace v:lnum with a:lnum everywhere in the function.在 function 中的任何地方用a:lnum搜索并替换v:lnum The indent script should now work as it did before, and you'll be able to call the function manually with a line number.缩进脚本现在应该像以前一样工作,您将能够使用行号手动调用 function。

Here's the specific commit that does this in the vim-ruby repository, as an example这是在 vim-ruby 存储库中执行此操作的特定提交, 例如

Additionally, echomsg "foo" will print a message that you can read afterwards by running the :messages command.此外, echomsg "foo"将打印一条消息,之后您可以通过运行:messages命令阅读该消息。 And I can recommend the Decho plugin for easy-to-read debug messages.我可以推荐Decho插件以获得易于阅读的调试消息。

Use my systemverilog script.使用我的 systemverilog 脚本。 No issues with this one.这个没有问题。 https://github.com/nachumk/systemverilog.vim https://github.com/nachumk/systemverilog.vim

Over 13k downloads from https://www.vim.org/scripts/script.php?script_id=4743https://www.vim.org/scripts/script.php?script_id=4743下载超过 13k

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

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