Search code examples
pythoncommentstextmateblockfolding

How do you enable block folding for Python comments in TextMate?


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*$';

Solution

  • 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.