Fixed moving of classes without body

This commit is contained in:
Alexey Sedunov
2013-06-20 15:18:22 +04:00
parent 07ede8898b
commit fa01d59ef8
10 changed files with 104 additions and 5 deletions
@@ -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;
}
@@ -0,0 +1,8 @@
// MOVE: down
<caret>class A
class D {
}
class C {
}
@@ -0,0 +1,8 @@
// MOVE: down
class D {
<caret>class A
}
class C {
}
@@ -0,0 +1,8 @@
// MOVE: up
class D {
}
<caret>class A
class C {
}
@@ -0,0 +1,8 @@
// MOVE: up
class D {
<caret>class A
}
class C {
}
@@ -0,0 +1,5 @@
// MOVE: down
<caret>class A
class D {
}
@@ -0,0 +1,5 @@
// MOVE: down
class D {
<caret>class A
}
@@ -0,0 +1,5 @@
// MOVE: up
class D {
}
<caret>class A
@@ -0,0 +1,5 @@
// MOVE: up
class D {
<caret>class A
}
@@ -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")