Python 多行字符串破坏了 Vim 的缩进折叠
Python 的字符串-文字并置使多行字符串编写起来更加容易和美观,但是当我有四到五个缩进深度并且想要使用整行(前导空格并不重要)时,Vim 的 foldmethod=indent< /code> 崩溃了。
例如:
def getQuotation():
print "Fetching quotation from the absolutely useless function."
return ("Four score and seven years ago our fathers brought forth, "
"upon this continent, a new nation, conceived in liberty, and dedicated "
"to the proposition that \"all men are created equal\"")
应该这样折叠:
def getQuotation():
+-- 4 lines: print "Fetching quotation from the absolutely useless function."--
但我得到的是:
def getQuotation():
+-- 2 lines: print "Fetching quotation from the absolutely useless function."--
"upon this continent, a new nation, conceived in liberty, and dedicated "
"to the proposition that \"all men are created equal\"")
我尝试设置 foldignore=\"
,但无济于事。Vim 的 help Foldignore
提供了这个说明主题:
仅当“foldmethod”为“indent”时使用。行开头为 “foldignore”中的角色将从周围获得折叠级别 线。在检查此字符之前会跳过空格。
是否有一些明显的我遗漏的东西,或者我是否必须诉诸 foldmethod=expr
,将折叠级别基于缩进,并且自己排除极端情况?
编辑:我至少取得了一些进展;事实证明,如果我在字符串和之后添加一个非空行,并使用set fdm=indent
“刷新”缩进,则该块将按预期折叠。即使是空注释 (#
) 也足够了。
Python's string-literal juxtaposition makes multi-line strings much easier and prettier to write, but when I'm four or five indents deep and want to use the entire row (leading whitespace does not matter), Vim's foldmethod=indent
breaks down.
For example:
def getQuotation():
print "Fetching quotation from the absolutely useless function."
return ("Four score and seven years ago our fathers brought forth, "
"upon this continent, a new nation, conceived in liberty, and dedicated "
"to the proposition that \"all men are created equal\"")
should be folded as this:
def getQuotation():
+-- 4 lines: print "Fetching quotation from the absolutely useless function."--
but instead I get this:
def getQuotation():
+-- 2 lines: print "Fetching quotation from the absolutely useless function."--
"upon this continent, a new nation, conceived in liberty, and dedicated "
"to the proposition that \"all men are created equal\"")
I tried setting foldignore=\"
, but to no avail. Vim's help foldignore
offers this to say on the subject:
Used only when 'foldmethod' is "indent". Lines starting with
characters in 'foldignore' will get their fold level from surrounding
lines. White space is skipped before checking for this character.
Is there something obvious that I'm missing, or will I have to resort to foldmethod=expr
, base the foldlevel on the indent, and except the corner cases myself?
EDIT: I've made at least some headway; it turns out that if I add a non-empty line after the strings and "refresh" the indent with set fdm=indent
, then the block folds as it's supposed to. Even an empty comment (#
) is enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短回答:你不能使用
foldmethod=indent
来做到这一点,但我发现你可以使用foldmethod=expr
,所以不需要重新发明轮子。请参阅长答案。长答案
只是简单回顾一下
foldmethod=indent
的工作原理...shiftwidth
foldlevel
由于您拥有的文本在屏幕边缘对齐,因此任何涉及
shiftwidth
的内容最终都会被破坏,除非您将其破解为你做到了。在找到可行的配置之前,我查看了几种不同的
.vimrc
配置。要获得比foldmethod=indent
更简洁的解决方案,请使用foldmethod=expr
和下面的~/.vimrc
。我在 jneb 的 bitbucket python-fold repo 中找到了它作为测试,我构建了更多案例到您的示例中...
在我的底部使用 python-fold
~/.vimrc
产生:当我点击
zR
展开:仅供参考,我使用 Dmitry Vasiliev 的
python.vim
<~/.vim/syntax/python.vim
中的 /a> 用于 python 语法高亮显示。我复制了下面 jneb 的 vim 脚本,以防 bitbucket 存储库消失......
Short answer: you can't do this with
foldmethod=indent
, but I found something you can use withfoldmethod=expr
, so no need to reinvent the wheel. See the long answer.Long answer
Just a brief review of how
foldmethod=indent
works...shiftwidth
with whitespace from the edge of the pagefoldlevel
Since the text you have is justified at the edge of the screen, anything involving
shiftwidth
is eventually broken unless you hack it up as you did.I looked at several different
.vimrc
configurations before I found something that would work. For a cleaner solution thanfoldmethod=indent
, usefoldmethod=expr
with the~/.vimrc
I have below. I found it in jneb's bitbucket python-fold repoAs a test, I built a few more cases into your example...
Using python-fold at the bottom of my
~/.vimrc
yields:And when I hit
zR
to unfold:FYI, I use Dmitry Vasiliev's
python.vim
in~/.vim/syntax/python.vim
for python syntax highlights.I copied jneb's vim script below in case the bitbucket repo disappears...
您可以将
python-mode
与FastFold
一起使用。Python-mode
解决了很多折叠问题,而FastFold
提高了折叠速度。You can use
python-mode
along withFastFold
.Python-mode
has solved a lot of the problems with folding, andFastFold
improves folding speed.