From 3a4c1d457532f9a0a7feefeadda3f5652ee08f98 Mon Sep 17 00:00:00 2001 From: "liana.bakradze" Date: Fri, 25 Mar 2016 19:29:53 +0300 Subject: [PATCH] PY-18928 fix multiline comments folding to handle custom folding regions correctly --- .../com/jetbrains/python/PythonFoldingBuilder.java | 11 +++++++++++ python/testData/folding/customFoldingWithComments.py | 9 +++++++++ python/testData/folding/nestedFolding.py | 7 +++++++ .../testSrc/com/jetbrains/python/PyFoldingTest.java | 9 +++++++++ 4 files changed, 36 insertions(+) create mode 100644 python/testData/folding/customFoldingWithComments.py create mode 100644 python/testData/folding/nestedFolding.py diff --git a/python/src/com/jetbrains/python/PythonFoldingBuilder.java b/python/src/com/jetbrains/python/PythonFoldingBuilder.java index d78f3fded2d1..71f22aa670e2 100644 --- a/python/src/com/jetbrains/python/PythonFoldingBuilder.java +++ b/python/src/com/jetbrains/python/PythonFoldingBuilder.java @@ -90,10 +90,17 @@ public class PythonFoldingBuilder extends CustomFoldingBuilder implements DumbAw } private static void foldSequentialComments(ASTNode node, List 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 index 000000000000..3009428a9200 --- /dev/null +++ b/python/testData/folding/customFoldingWithComments.py @@ -0,0 +1,9 @@ +# region Description +# comment +# comment +print("test region") + +## comment +#comment +# endregion +# 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 index 000000000000..d2c900298d1c --- /dev/null +++ b/python/testData/folding/nestedFolding.py @@ -0,0 +1,7 @@ +# region # Some description +## print("first line of commented code") +## print("second line of commented code") +# pass +# # comment +# pass +# endregion # end of custom block \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyFoldingTest.java b/python/testSrc/com/jetbrains/python/PyFoldingTest.java index 77faeeb7ade1..1839a0bc8fc3 100644 --- a/python/testSrc/com/jetbrains/python/PyFoldingTest.java +++ b/python/testSrc/com/jetbrains/python/PyFoldingTest.java @@ -57,4 +57,13 @@ public class PyFoldingTest extends PyTestCase { doTest(); } + public void testNestedFolding() { + doTest(); + } + + //PY-18928 + public void testCustomFoldingWithComments() { + doTest(); + } + }