From d8d9f870987d2860a4ca21eeae761108bb0a71ce 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 GitOrigin-RevId: 06f825efeaba78ce2d4828949594e62f079b4b27 --- .../upDownMover/AbstractJetUpDownMover.java | 27 +++++++++++++++++ .../upDownMover/JetDeclarationMover.java | 2 +- .../upDownMover/JetExpressionMover.java | 2 +- .../moveUpDown/CodeMoverTestGenerated.java | 30 +++++++++++++++++++ .../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 ++++++++ 16 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt.after diff --git a/plugins/kotlin/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/AbstractJetUpDownMover.java b/plugins/kotlin/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/AbstractJetUpDownMover.java index 1c620a48bbfb..19485e963e68 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/AbstractJetUpDownMover.java +++ b/plugins/kotlin/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/plugins/kotlin/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetDeclarationMover.java b/plugins/kotlin/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetDeclarationMover.java index 08915e34933c..7d7a85a51155 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetDeclarationMover.java +++ b/plugins/kotlin/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/plugins/kotlin/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetExpressionMover.java b/plugins/kotlin/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetExpressionMover.java index 8645f8a801a2..f766e699b913 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetExpressionMover.java +++ b/plugins/kotlin/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/plugins/kotlin/idea/tests/test/org/jetbrains/jet/plugin/codeInsight/moveUpDown/CodeMoverTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/jet/plugin/codeInsight/moveUpDown/CodeMoverTestGenerated.java index 83e1fa16c044..9dfd432dc71b 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/jet/plugin/codeInsight/moveUpDown/CodeMoverTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/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"); diff --git a/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt b/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt new file mode 100644 index 000000000000..909f3f2c623a --- /dev/null +++ b/plugins/kotlin/idea/tests/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/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt.after b/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt.after new file mode 100644 index 000000000000..6c5a57b7695f --- /dev/null +++ b/plugins/kotlin/idea/tests/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/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt b/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt new file mode 100644 index 000000000000..79d9890159d1 --- /dev/null +++ b/plugins/kotlin/idea/tests/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/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt.after b/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment2.kt.after new file mode 100644 index 000000000000..82ea22129023 --- /dev/null +++ b/plugins/kotlin/idea/tests/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/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt b/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt new file mode 100644 index 000000000000..20e23f6cb128 --- /dev/null +++ b/plugins/kotlin/idea/tests/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/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt.after b/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment3.kt.after new file mode 100644 index 000000000000..1522bac0ba5d --- /dev/null +++ b/plugins/kotlin/idea/tests/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/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt b/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt new file mode 100644 index 000000000000..3e0ccc604a94 --- /dev/null +++ b/plugins/kotlin/idea/tests/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/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt.after b/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment4.kt.after new file mode 100644 index 000000000000..c67e8e03c1f1 --- /dev/null +++ b/plugins/kotlin/idea/tests/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/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt b/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt new file mode 100644 index 000000000000..ac67b559119e --- /dev/null +++ b/plugins/kotlin/idea/tests/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/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt.after b/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment5.kt.after new file mode 100644 index 000000000000..0a908386a59d --- /dev/null +++ b/plugins/kotlin/idea/tests/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/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt b/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt new file mode 100644 index 000000000000..b115fcf32c8b --- /dev/null +++ b/plugins/kotlin/idea/tests/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/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt.after b/plugins/kotlin/idea/tests/testData/codeInsight/moveUpDown/expressions/multilineComment6.kt.after new file mode 100644 index 000000000000..baa367bf0a27 --- /dev/null +++ b/plugins/kotlin/idea/tests/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