如何在 TextMate 中启用 Python 注释的块折叠?

发布于 2024-10-10 22:13:12 字数 1073 浏览 7 评论 0原文

在 TextMate 1.5.10 r1623 中,您会看到一些小箭头,允许您折叠方法块:

alt text

不幸的是,如果您有多行 Python 注释,它无法识别它,因此您无法折叠它:

def foo():
 """
 How do
 I fold
 these comments?
 """

 print "bar"

TextMate 在其网站上有关于如何自定义折叠的信息: http://manual.macromates.com/en/navigation_overview#customizing_foldings

...但我对正则表达式的熟练程度不足以对此做任何事情。 TextMate 使用 Oniguruma 正则表达式 API,我正在使用默认的 Python.tmbundle 更新为通过 GetBundles 获取最新版本。

有谁知道如何做到这一点?预先感谢您的帮助! :)


在 Bundle Editor 中的 Python 语言下添加 Python.tmbundle 的默认 foldingStartMarkerfoldingStopMarker 正则表达式值:

foldingStartMarker = '^\s*(def|class)\s+([.a-zA-Z0-9_ <]+)\s*(\((.*)\))?\s*:|\{\s*$|\(\s*$|\[\s*$|^\s*"""(?=.)(?!.*""")';
foldingStopMarker = '^\s*$|^\s*\}|^\s*\]|^\s*\)|^\s*"""\s*$';

In TextMate 1.5.10 r1623, you get little arrows that allow you to fold method blocks:

alt text

Unfortunately, if you have a multi-lined Python comment, it doesn't recognize it, so you can't fold it:

def foo():
 """
 How do
 I fold
 these comments?
 """

 print "bar"

TextMate has this on their site on how to customize folding: http://manual.macromates.com/en/navigation_overview#customizing_foldings

...but I'm not skilled in regex enough to do anything about it. TextMate uses the Oniguruma regex API, and I'm using the default Python.tmbundle updated to the newest version via GetBundles.

Does anyone have an idea of how to do this? Thanks in advance for your help! :)


Adding the default foldingStartMarker and foldingStopMarker regex values for Python.tmbundle under the Python Language in Bundle Editor:

foldingStartMarker = '^\s*(def|class)\s+([.a-zA-Z0-9_ <]+)\s*(\((.*)\))?\s*:|\{\s*$|\(\s*$|\[\s*$|^\s*"""(?=.)(?!.*""")';
foldingStopMarker = '^\s*$|^\s*\}|^\s*\]|^\s*\)|^\s*"""\s*
;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情魔剑神 2024-10-17 22:13:12

看来多行注释折叠在 TextMate 中确实有效,但您必须完全像这样排列引号:

""" Some sort of multi
    line comment, which needs quotes
    in just the right places to work. """

这似乎可以做到:

在此处输入图像描述

在此处输入图像描述

It appears that multi-line comment folding does work in TextMate, but your must line up your quotes exactly like so:

""" Some sort of multi
    line comment, which needs quotes
    in just the right places to work. """

That seems to do it:

enter image description here

enter image description here

鱼窥荷 2024-10-17 22:13:12

根据此 Textmate 邮件列表线程,如果您按照它到底,不支持 Python 的正确代码折叠。基本上,在foldingStartMarker 和foldingStopMarker 中实现的正则表达式不允许捕获,因此“结束折叠”开始处的间距量无法与“开始折叠”匹配。

Textmate 的创建者 Allan Odgaard 并未最终正式解决这个问题;然而,由于该线程来自 2005 年,我认为这是一个死问题,并且不会得到支持。

According to this Textmate Mailing list thread, if you follow it to the end, proper code folding for Python is not supported. Basically, regular expressions as implemented in the foldingStartMarker and foldingStopMarker do not allow for captures, thus the amount of spacing at the beginning of the "end fold" cannot be matched to the "begin fold".

The issue is not finally and officially addressed by Textmate's creator, Allan Odgaard; however since the thread is from 2005, I assume it is a dead issue, and not one that will be supported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文