打开Scala文件时,我试图让我的Vim安装程序自动折叠导入语句。这与打开文件时Intellij的操作类似。我目前.vimrc
在Wiki的文件中包含以下几行。
set foldmethod=syntax
set foldenable
syn region foldImports start=/(^\s*^import\) .\+/ end=/^\s*$/ transparent fold keepend
但是,当我打开.scala
文件时,它不会折叠导入内容,而是折叠对象的主体。我也在使用vim-scala插件。谢谢您的帮助!
您几乎可以使它正常工作。我们应该考虑一些时髦的因素。
foldmethod
为syntax
(顺便说一句,这在学习Vimscript the Hardway上没有记录。.so:help foldmethod
是弄清楚这一点的关键)SYNTAX fold-syntax
A fold is defined by syntax items that have the "fold" argument. |:syn-fold|
The fold level is defined by nesting folds. The nesting of folds is limited with 'foldnestmax'.
Be careful to specify proper syntax syncing. If this is not done right, folds may differ from the displayed highlighting. This is especially relevant when using patterns that match more than one line. In case of doubt, try using brute-force syncing:
:syn sync fromstart
The main thing to note is the sync fromstart
this is a useful helper if you have regex that would match throughout the file and only want to catch the header. In your case you should be able to ignore this but just something to be aware of.
由于import块是相当可预测的,因此我们可以简化start
和end
看起来像这样:
syn region foldImports start="import" end=/import.*\n^$/ fold keepend
由于region
只是在寻找一些字符串来开始匹配,因此我们可以使用"import"
(或/import/
),然后对于结束值,我们想使用一些经过精心设计的语句。关键是我们要使结尾为导入的最后一行,其后是空白行(/import.*\n^$/
)
希望这可以为您解决问题(我不使用scala,因此您可能需要根据需要调整正则表达式)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句