PY-18928 fix multiline comments folding to handle custom folding regions correctly
authorliana.bakradze <liana.bakradze@jetbrains.com>
Fri, 25 Mar 2016 16:29:53 +0000 (19:29 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Fri, 25 Mar 2016 16:29:53 +0000 (19:29 +0300)
python/src/com/jetbrains/python/PythonFoldingBuilder.java
python/testData/folding/customFoldingWithComments.py [new file with mode: 0644]
python/testData/folding/nestedFolding.py [new file with mode: 0644]
python/testSrc/com/jetbrains/python/PyFoldingTest.java

index d78f3fded2d1f2380bfbca6ea4eb3303f608ef2e..71f22aa670e219cc8c5bdb53f6d5c120a57ee5e8 100644 (file)
@@ -90,10 +90,17 @@ public class PythonFoldingBuilder extends CustomFoldingBuilder implements DumbAw
   }
 
   private static void foldSequentialComments(ASTNode node, List<FoldingDescriptor> descriptors) {
+    //do not start folded comments from custom region
+    if (isCustomRegionElement(node.getPsi())) {
+      return;
+    }
     //need to skip previous comments in sequence
     ASTNode curNode = node.getTreePrev();
     while (curNode != null) {
       if (curNode.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
+        if (isCustomRegionElement(curNode.getPsi())) {
+          break;
+        }
         return;
       }
       curNode = curNode.getPsi() instanceof PsiWhiteSpace ? curNode.getTreePrev() : null;
@@ -104,6 +111,10 @@ public class PythonFoldingBuilder extends CustomFoldingBuilder implements DumbAw
     ASTNode lastCommentNode = node;
     while (curNode != null) {
       if (curNode.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
+        //do not end folded comments with custom region
+        if (isCustomRegionElement(curNode.getPsi())) {
+          break;
+        }
         lastCommentNode = curNode;
         curNode = curNode.getTreeNext();
         continue;
diff --git a/python/testData/folding/customFoldingWithComments.py b/python/testData/folding/customFoldingWithComments.py
new file mode 100644 (file)
index 0000000..3009428
--- /dev/null
@@ -0,0 +1,9 @@
+<fold text='Description'># region Description
+<fold text='...'># comment
+# comment</fold>
+print("test region")
+
+<fold text='...'>## comment
+#comment</fold>
+# endregion</fold>
+# comment
\ No newline at end of file
diff --git a/python/testData/folding/nestedFolding.py b/python/testData/folding/nestedFolding.py
new file mode 100644 (file)
index 0000000..d2c9002
--- /dev/null
@@ -0,0 +1,7 @@
+<fold text='# Some description'># region # Some description
+<fold text='...'>## print("first line of commented code")
+## print("second line of commented code")
+# pass
+# # comment
+# pass</fold>
+# endregion # end of custom block</fold>
\ No newline at end of file
index 77faeeb7ade13c572ff91446874fa8006d9a63ad..1839a0bc8fc3b72d22f47732e475bce7dae9bb8d 100644 (file)
@@ -57,4 +57,13 @@ public class PyFoldingTest extends PyTestCase {
     doTest();
   }
 
+  public void testNestedFolding() {
+    doTest();
+  }
+
+  //PY-18928
+  public void testCustomFoldingWithComments() {
+    doTest();
+  }
+
 }