From f1aad514e3103fed2eed98c7b9dc3b64867ac985 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Thu, 5 Sep 2024 16:53:20 -0700 Subject: [PATCH 1/2] Folding for functions with parameters over multiple lines The default configuration fails to fold functions if the parameters are defined over multiple lines. Due to typing, this is more likely to happen these days than it used to be. Three things here: - a new regex for foldingIndentedBlockStart. The existing expression only matches when the final : is on the same line as the 'def'. We don't want to lose that requirement for the other keywords that start a block, so I added a totally separate expression that matches only functions definitions with a open-parenthesis as the final non-whitespace non-comment character on the line. `^\s*def\b.*\(\s*(#.*)?$` - adding a foldingIndentedBlockIgnore expression This is needed so that the closing parenthesis of the parameter declaration can be on a lower indent level, like black likes to do. It should match a closing parenthesis and colon with optional return type. `^\s*\)(\s*->\s*\w.*)?:\s*(#.*)?$` - modifying foldingStartMarker Still not done, because Textmate also matches `def foo(\n` as the start of marker-based folding due to the parenthesis, and then it gets confused. I added a negative lookbehind to prevent it from matching when we're making a def statement. `(?settings foldingIndentedBlockStart - ^\s*(class|def|async|for|while|if|elif|else|with|try|finally|except)\b.*(:|,|->|\\)\s*(#.*)?$ + ^\s*(class|def|async|for|while|if|elif|else|with|try|finally|except)\b.*(:|,|->|\\)\s*(#.*)?$|^\s*def\b.*\(\s*(#.*)?$ + foldingIndentedBlockIgnore + ^\s*\)(\s*->\s*\w.*)?:\s*(#.*)?$ uuid 4A5DB35F-D647-4357-9D9B-57313710B95B diff --git a/Preferences/Folding.tmPreferences b/Preferences/Folding.tmPreferences index e5ce1ef..87a1e3f 100644 --- a/Preferences/Folding.tmPreferences +++ b/Preferences/Folding.tmPreferences @@ -9,7 +9,7 @@ settings foldingStartMarker - ^\s*"""(?=.)(?!.*""")|(\{|\(|\[)\s*(#.*)?$ + ^\s*"""(?=.)(?!.*""")|(? foldingStopMarker ^\s*"""\s*$|^\s*(\}|\)|\]),?\s*(#.*)?$ From d3555a3d3992b21d2710ff1a3d9d974a7ca2b69c Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Thu, 5 Sep 2024 18:08:11 -0700 Subject: [PATCH 2/2] working version of foldingStartMarker Negative look-behinds with varaible lengths aren't supported, which I didn't realize. This fixes the issue using a negative-lookahead instead. --- Preferences/Folding.tmPreferences | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Preferences/Folding.tmPreferences b/Preferences/Folding.tmPreferences index 87a1e3f..34ae806 100644 --- a/Preferences/Folding.tmPreferences +++ b/Preferences/Folding.tmPreferences @@ -9,7 +9,7 @@ settings foldingStartMarker - ^\s*"""(?=.)(?!.*""")|(? + ^\s*"""(?=.)(?!.*""")|(\{|\[)\s*(#.*)?$|^(?!\s*def\s+\w+\s*).*\(\s*(#.*)?$ foldingStopMarker ^\s*"""\s*$|^\s*(\}|\)|\]),?\s*(#.*)?$