From d80512f68a4b147ebc69b9e53a6579e73457c555 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Thu, 5 Sep 2013 14:50:43 +0400 Subject: [PATCH] Fix move when multi-line elements have shared lines --- .../upDownMover/AbstractJetUpDownMover.java | 27 +++++++++++++++++ .../upDownMover/JetDeclarationMover.java | 2 +- .../upDownMover/JetExpressionMover.java | 2 +- .../expressions/multilineComment1.kt | 13 ++++++++ .../expressions/multilineComment1.kt.after | 13 ++++++++ .../expressions/multilineComment2.kt | 13 ++++++++ .../expressions/multilineComment2.kt.after | 13 ++++++++ .../expressions/multilineComment3.kt | 13 ++++++++ .../expressions/multilineComment3.kt.after | 13 ++++++++ .../expressions/multilineComment4.kt | 13 ++++++++ .../expressions/multilineComment4.kt.after | 13 ++++++++ .../expressions/multilineComment5.kt | 13 ++++++++ .../expressions/multilineComment5.kt.after | 13 ++++++++ .../expressions/multilineComment6.kt | 13 ++++++++ .../expressions/multilineComment6.kt.after | 13 ++++++++ .../moveUpDown/CodeMoverTestGenerated.java | 30 +++++++++++++++++++ 16 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt.after diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/AbstractJetUpDownMover.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/AbstractJetUpDownMover.java index 1c620a48bbf..19485e963e6 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/AbstractJetUpDownMover.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/AbstractJetUpDownMover.java @@ -109,6 +109,33 @@ public abstract class AbstractJetUpDownMover extends LineMover { return endLine - startLine; } + protected static int getElementLine(PsiElement element, Editor editor, boolean first) { + if (element == null) return -1; + + Document doc = editor.getDocument(); + TextRange spaceRange = element.getTextRange(); + + return first ? doc.getLineNumber(spaceRange.getStartOffset()) : doc.getLineNumber(spaceRange.getEndOffset()); + } + + protected static PsiElement getLastNonWhiteSiblingInLine(@Nullable PsiElement element, @NotNull Editor editor, boolean down) { + if (element == null) return null; + + int line = getElementLine(element, editor, down); + + PsiElement lastElement = element; + while (true) { + if (lastElement == null) return null; + PsiElement sibling = firstNonWhiteSibling(lastElement, down); + if (getElementLine(sibling, editor, down) == line) { + lastElement = sibling; + } + else break; + } + + return lastElement; + } + private static boolean checkCommentAtBlockBound(PsiElement blockElement, PsiElement comment, JetBlockExpression block) { return PsiTreeUtil.isAncestor(block, blockElement, true) && comment instanceof PsiComment; } diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetDeclarationMover.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetDeclarationMover.java index 08915e34933..7d7a85a5115 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetDeclarationMover.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetDeclarationMover.java @@ -237,7 +237,7 @@ public class JetDeclarationMover extends AbstractJetUpDownMover { LineRange sourceRange = getSourceRange(firstDecl, lastDecl, editor, oldRange); if (sourceRange == null) return false; - PsiElement sibling = firstNonWhiteSibling(sourceRange, down); + PsiElement sibling = getLastNonWhiteSiblingInLine(firstNonWhiteSibling(sourceRange, down), editor, down); // Either reached last sibling, or jumped over multi-line whitespace if (sibling == null) { diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetExpressionMover.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetExpressionMover.java index 8645f8a801a..f766e699b91 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetExpressionMover.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetExpressionMover.java @@ -476,7 +476,7 @@ public class JetExpressionMover extends AbstractJetUpDownMover { LineRange sourceRange = getSourceRange(firstElement, lastElement, editor, oldRange); if (sourceRange == null) return false; - PsiElement sibling = adjustSibling(editor, sourceRange, info, down); + PsiElement sibling = getLastNonWhiteSiblingInLine(adjustSibling(editor, sourceRange, info, down), editor, down); // Either reached last sibling, or jumped over multi-line whitespace if (sibling == null) return true; diff --git a/idea/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt new file mode 100644 index 00000000000..909f3f2c623 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt @@ -0,0 +1,13 @@ +// MOVE: up + +fun foo() { + + /* + * + */ val foo = 1 + println("foo=") + println(foo) /* + * + */ + +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt.after new file mode 100644 index 00000000000..6c5a57b7695 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt.after @@ -0,0 +1,13 @@ +// MOVE: up + +fun foo() { + /* + * + */ val foo = 1 + + println("foo=") + println(foo) /* + * + */ + +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt new file mode 100644 index 00000000000..79d9890159d --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt @@ -0,0 +1,13 @@ +// MOVE: up + +fun foo() { + + /* + * + */ val foo = 1 + println("foo=") + println(foo) /* + * + */ + +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt.after new file mode 100644 index 00000000000..82ea2212902 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt.after @@ -0,0 +1,13 @@ +// MOVE: up + +fun foo() { + + println("foo=") + /* + * + */ val foo = 1 + println(foo) /* + * + */ + +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt new file mode 100644 index 00000000000..20e23f6cb12 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt @@ -0,0 +1,13 @@ +// MOVE: up + +fun foo() { + + /* + * + */ val foo = 1 + println("foo=") + println(foo) /* + * + */ + +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt.after new file mode 100644 index 00000000000..1522bac0ba5 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt.after @@ -0,0 +1,13 @@ +// MOVE: up + +fun foo() { + + /* + * + */ val foo = 1 + println(foo) /* + * + */ + println("foo=") + +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt new file mode 100644 index 00000000000..3e0ccc604a9 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt @@ -0,0 +1,13 @@ +// MOVE: down + +fun foo() { + + /* + * + */ val foo = 1 + println("foo=") + println(foo) /* + * + */ + +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt.after new file mode 100644 index 00000000000..c67e8e03c1f --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt.after @@ -0,0 +1,13 @@ +// MOVE: down + +fun foo() { + + println("foo=") + /* + * + */ val foo = 1 + println(foo) /* + * + */ + +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt new file mode 100644 index 00000000000..ac67b559119 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt @@ -0,0 +1,13 @@ +// MOVE: down + +fun foo() { + + /* + * + */ val foo = 1 + println("foo=") + println(foo) /* + * + */ + +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt.after new file mode 100644 index 00000000000..0a908386a59 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt.after @@ -0,0 +1,13 @@ +// MOVE: down + +fun foo() { + + /* + * + */ val foo = 1 + println(foo) /* + * + */ + println("foo=") + +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt new file mode 100644 index 00000000000..b115fcf32c8 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt @@ -0,0 +1,13 @@ +// MOVE: down + +fun foo() { + + /* + * + */ val foo = 1 + println("foo=") + println(foo) /* + * + */ + +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt.after new file mode 100644 index 00000000000..baa367bf0a2 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt.after @@ -0,0 +1,13 @@ +// MOVE: down + +fun foo() { + + /* + * + */ val foo = 1 + println("foo=") + + println(foo) /* + * + */ +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/moveUpDown/CodeMoverTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/moveUpDown/CodeMoverTestGenerated.java index 83e1fa16c04..9dfd432dc71 100644 --- a/idea/tests/org/jetbrains/jet/plugin/codeInsight/moveUpDown/CodeMoverTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/moveUpDown/CodeMoverTestGenerated.java @@ -912,6 +912,36 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { doTestExpression("idea/testData/codeInsight/moveUpDown/expressions/lambda3.kt"); } + @TestMetadata("multilineComment1.kt") + public void testMultilineComment1() throws Exception { + doTestExpression("idea/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt"); + } + + @TestMetadata("multilineComment2.kt") + public void testMultilineComment2() throws Exception { + doTestExpression("idea/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt"); + } + + @TestMetadata("multilineComment3.kt") + public void testMultilineComment3() throws Exception { + doTestExpression("idea/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt"); + } + + @TestMetadata("multilineComment4.kt") + public void testMultilineComment4() throws Exception { + doTestExpression("idea/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt"); + } + + @TestMetadata("multilineComment5.kt") + public void testMultilineComment5() throws Exception { + doTestExpression("idea/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt"); + } + + @TestMetadata("multilineComment6.kt") + public void testMultilineComment6() throws Exception { + doTestExpression("idea/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt"); + } + @TestMetadata("outOfClosure1.kt") public void testOutOfClosure1() throws Exception { doTestExpression("idea/testData/codeInsight/moveUpDown/expressions/outOfClosure1.kt");