Python多行字符串打破了Vim的缩进折叠

2024-09-30 18:13:08 发布

您现在位置:Python中文网/ 问答频道 /正文

Python的string-literal并置使多行字符串更容易编写,也更漂亮,但是当我缩进了四到五个缩进并且想要使用整行时(前导空格无关紧要),Vim的foldmethod=indent就会崩溃。在

例如:

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\"")

应该这样折叠:

^{pr2}$

但我得到的却是:

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提供了这样一个主题:

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.

有没有什么明显的东西我遗漏了,或者我将不得不求助于foldmethod=expr,以缩进为基础,除了我自己的角盒?在

编辑:我至少取得了一些进展;事实证明,如果我在字符串后面添加一个非空行,用set fdm=indent刷新缩进,那么块就会按预期折叠。即使是一个空的注释(#)也足够了。在


Tags: andthe字符串infromdefvimthis
2条回答

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?

简而言之:你不能用foldmethod=indent来做这个,但是我发现了一些可以与foldmethod=expr一起使用的东西,所以不需要重新设计轮子。看长答案。在

长答案

只是简单回顾一下foldmethod=indent是如何工作的。。。在

  • 从页面边缘查找带有空白的文本shiftwidth
  • 插入新的foldlevel
  • 取消缩进将使您的折叠级别降低

由于文本在屏幕的边缘是对正的,任何涉及shiftwidth的内容最终都会被破坏,除非您像您那样对其进行修改。在

我研究了几种不同的.vimrc配置,然后找到了可以工作的方法。对于比foldmethod=indent更干净的解决方案,请将foldmethod=expr与下面的~/.vimrc一起使用。我在jneb's bitbucket python-fold repo找到的

作为测试,我在您的示例中构建了更多的案例。。。在

class testclass(object):
    def __init__(self):
        self.testit = None
    def __repr__(self):
        return "guacamole"

def foobarme():
    assert False
    return 42

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\"")

在我的~/.vimrc底部使用python-fold可以得到:

jneb_folded

当我点击zR展开时:

jneb_unfolded

仅供参考,我使用Dmitry Vasiliev的^{}in ~/.vim/syntax/python.vim中的python语法亮点。在

我在下面复制了jneb的vim脚本,以防bitbucket回购消失。。。在


^{pr2}$

您可以将python-modeFastFold一起使用。Python-mode解决了很多折叠问题,{}提高了折叠速度。在

相关问题 更多 >