简体   繁体   English

VIM上红宝石的代码折叠区域?

[英]Code folding regions in ruby on VIM?

I know not everyone like code folding, but as a convert to Rails and a convert to VIM, I want one of the conveniences back that I had in Visual Studio.Net. 我知道不是每个人都喜欢代码折叠,但作为转换为Rails和转换为VIM,我想要的是Visual Studio.Net中的一个便利。 I want to be able to place arbitrary #region #endregion tags and have VIM fold them for me like everything else. 我希望能够放置任意#region #endregion标签,让VIM像其他一样折叠它们。

I have syntax code folding turned on and normal def..end folding works fine, I just want to augment that with #regions. 我打开了语法代码折叠,正常的def..end折叠工作正常,我只想用#regions来增强它。

I did some googling and thought that I could do this in a ruby.vim in my syntaxes folder, but it didn't work, and I don't know enough to debug whether it was bad code that I copied over or if it just "doesn't work that way." 我做了一些谷歌搜索,并认为我可以在我的语法文件夹中的ruby.vim中执行此操作,但它不起作用,我不知道调试是否是我复制的错误代码或者它是否只是“不这样做。”

let b:match_words = '\s*#\s*region.*$:\s*#\s*endregion'

This is my only line in syntax/ruby.vim. 这是我在syntax / ruby​​.vim中的唯一一行。 In fact, the file wasn't there before I started, even though ruby highlighting is working fine. 事实上,在我开始之前,文件并不存在,即使ruby突出显示工作正常。 (which also throws big red flags for me - I haven't a clue where the ruby/rails syntax, unless its in the rails.vim plugin, but I can't find it there either. (这也给我带来了大红旗 - 我不知道ruby / rails语法在哪里,除非它在rails.vim插件中,但我也找不到它。

Can someone lend me a thread so that I can follow it and figure this out? 有人可以借给我一个线程,以便我可以遵循它并想出来吗? Thanks 谢谢

UPDATE: 更新:

I found some other code snippets and tried this version instead, which almost gets me there: 我找到了一些其他的代码片段并尝试了这个版本,它几乎让我在那里:

syn region vimFoldRegion
  \ start="#region"
  \ end="#endregion"
  \ skip=+"\%(\\"\|[^"]\)\{-}\%("\|$\)\|'[^']\{-}'+
  \ transparent fold
  \ keepend extend
  \ containedin=ALLBUT,@vimNoFold

This lets me fold the region, but all of the code inside the region is shown like its a comment. 这让我可以折叠区域,但区域内的所有代码都显示为注释。 I've tried different variations of the parameters on the syn, but I'm not grokking it. 我已经在syn上尝试了不同的参数变化,但我并不是在研究它。

Vim can fold regions based on markers if you set foldmethod=marker . 如果设置foldmethod=marker Vim可以根据标记折叠区域。 The default markers are {{{ and }}} , though I'm pretty sure you could override these to be #region and #endregion . 默认标记为{{{}}} ,但我非常确定您可以将这些标记覆盖为#region#endregion The problem is you then lose the ability to fold based on syntax. 问题是你失去了根据语法折叠的能力。 There's another Stack Overflow question dealing with combining the two fold methods [1], but the conclusion there is that it's not possible to do both simultaneously. 还有另一个Stack Overflow问题涉及组合两种折叠方法[1],但结论是不可能同时进行这两种方法。

For what it's worth, I find that vim's syntax-based code-folding is horrendously slow for large Ruby files, so you might find yourself wanted to switch to a marker-only fold method if you run into the same problem. 对于它的价值,我发现vim的基于语法的代码折叠对于大型Ruby文件来说非常慢,所以如果你遇到同样的问题,你可能会发现自己想要切换到只有标记的折叠方法。

[1] Foldmethod=marker and syntax at the same time? [1] Foldmethod =标记和语法同时?

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

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