¿Cómo se habilita el plegado de bloques para los comentarios de Python en TextMate?

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

texto alternativo

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

preguntado el 09 de enero de 11 a las 02:01

Shouldn't that be a matter of defining foldingStartMarker and foldingStopMarker as """|'''? -

@delnan, I think you're on the right track. The current regex values are a little complex (I added them to the question above). -

2 Respuestas

Según 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.

Respondido el 03 de junio de 11 a las 00:06

Interesting. That explains why if you tab-in blank lines in comments, it'll still allow collapsing (it see's it as a whitespace and treats it like a normal character). - David

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

Respondido 03 Feb 11, 06:02

This is correct, but you failed to mention the important fact that your line 21 requires a tab that corresponds to line 18, or it will NOT work. - Alex Gray

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.