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 ae95da053c8..08915e34933 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetDeclarationMover.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/upDownMover/JetDeclarationMover.java @@ -106,16 +106,36 @@ public class JetDeclarationMover extends AbstractJetUpDownMover { return element instanceof JetDeclaration; } + @Nullable + private static PsiElement skipInsignificantElements(@NotNull PsiElement element, boolean down) { + PsiElement result = element; + + while (result instanceof PsiWhiteSpace || result.getTextLength() == 0) { + result = down ? result.getNextSibling() : result.getPrevSibling(); + if (result == null) break; + } + + return result; + } + @Override protected LineRange getElementSourceLineRange(@NotNull PsiElement element, @NotNull Editor editor, @NotNull LineRange oldRange) { JetDeclaration declaration = (JetDeclaration) element; - Document doc = editor.getDocument(); - TextRange textRange = declaration.getTextRange(); - if (doc.getTextLength() < textRange.getEndOffset()) return null; + PsiElement first = skipInsignificantElements(declaration.getFirstChild(), true); + PsiElement last = skipInsignificantElements(declaration.getLastChild(), false); - int startLine = editor.offsetToLogicalPosition(textRange.getStartOffset()).line; - int endLine = editor.offsetToLogicalPosition(textRange.getEndOffset()).line + 1; + if (first == null || last == null) return null; + + TextRange textRange1 = first.getTextRange(); + TextRange textRange2 = last.getTextRange(); + + Document doc = editor.getDocument(); + + if (doc.getTextLength() < textRange2.getEndOffset()) return null; + + int startLine = editor.offsetToLogicalPosition(textRange1.getStartOffset()).line; + int endLine = editor.offsetToLogicalPosition(textRange2.getEndOffset()).line + 1; if (startLine == oldRange.startLine || startLine == oldRange.endLine || endLine == oldRange.startLine || endLine == oldRange.endLine) { @@ -188,6 +208,13 @@ public class JetDeclarationMover extends AbstractJetUpDownMover { if (target instanceof JetPropertyAccessor && !(sibling instanceof JetPropertyAccessor)) return null; + if (start != null && start.getFirstChild() != null) { + start = skipInsignificantElements(start.getFirstChild(), true); + } + if (end != null && end.getFirstChild() != null) { + end = skipInsignificantElements(end.getLastChild(), false); + } + return start != null && end != null ? new LineRange(start, end, editor.getDocument()) : null; } diff --git a/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody1.kt b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody1.kt new file mode 100644 index 00000000000..ab8ed8c756d --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody1.kt @@ -0,0 +1,8 @@ +// MOVE: down +class A + +class D { +} + +class C { +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody1.kt.after b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody1.kt.after new file mode 100644 index 00000000000..2b29f95113c --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody1.kt.after @@ -0,0 +1,8 @@ +// MOVE: down +class D { + + class A +} + +class C { +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody2.kt b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody2.kt new file mode 100644 index 00000000000..44bdd3dcd47 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody2.kt @@ -0,0 +1,8 @@ +// MOVE: up +class D { +} + +class A + +class C { +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody2.kt.after b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody2.kt.after new file mode 100644 index 00000000000..3581434ea68 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody2.kt.after @@ -0,0 +1,8 @@ +// MOVE: up +class D { + class A + +} + +class C { +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody3.kt b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody3.kt new file mode 100644 index 00000000000..33ad1e2f29f --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody3.kt @@ -0,0 +1,5 @@ +// MOVE: down +class A + +class D { +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody3.kt.after b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody3.kt.after new file mode 100644 index 00000000000..07aa38c878f --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody3.kt.after @@ -0,0 +1,5 @@ +// MOVE: down +class D { + + class A +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody4.kt b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody4.kt new file mode 100644 index 00000000000..4027fb30c70 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody4.kt @@ -0,0 +1,5 @@ +// MOVE: up +class D { +} + +class A \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody4.kt.after b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody4.kt.after new file mode 100644 index 00000000000..d52a3645981 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody4.kt.after @@ -0,0 +1,5 @@ +// MOVE: up +class D { + class A + +} 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 244b9db74ff..ecd5afbec5c 100644 --- a/idea/tests/org/jetbrains/jet/plugin/codeInsight/moveUpDown/CodeMoverTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/moveUpDown/CodeMoverTestGenerated.java @@ -153,6 +153,26 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { doTestClassBodyDeclaration("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classAtProperty2.kt"); } + @TestMetadata("classWithoutBody1.kt") + public void testClassWithoutBody1() throws Exception { + doTestClassBodyDeclaration("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody1.kt"); + } + + @TestMetadata("classWithoutBody2.kt") + public void testClassWithoutBody2() throws Exception { + doTestClassBodyDeclaration("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody2.kt"); + } + + @TestMetadata("classWithoutBody3.kt") + public void testClassWithoutBody3() throws Exception { + doTestClassBodyDeclaration("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody3.kt"); + } + + @TestMetadata("classWithoutBody4.kt") + public void testClassWithoutBody4() throws Exception { + doTestClassBodyDeclaration("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class/classWithoutBody4.kt"); + } + } @TestMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/classInitializer")