Move statement up: do not apply to @file annotation

#KT-10790 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-11-18 16:08:18 +09:00
committed by Vladimir Dolzhenko
parent aeed7fe52d
commit 6b5c31e2fc
8 changed files with 58 additions and 4 deletions
@@ -466,6 +466,7 @@ fun main(args: Array<String>) {
model("codeInsight/moveUpDown/classBodyDeclarations", pattern = KT_OR_KTS, testMethod = "doTestClassBodyDeclaration") model("codeInsight/moveUpDown/classBodyDeclarations", pattern = KT_OR_KTS, testMethod = "doTestClassBodyDeclaration")
model("codeInsight/moveUpDown/closingBraces", testMethod = "doTestExpression") model("codeInsight/moveUpDown/closingBraces", testMethod = "doTestExpression")
model("codeInsight/moveUpDown/expressions", pattern = KT_OR_KTS, testMethod = "doTestExpression") model("codeInsight/moveUpDown/expressions", pattern = KT_OR_KTS, testMethod = "doTestExpression")
model("codeInsight/moveUpDown/line", testMethod = "doTestLine")
model("codeInsight/moveUpDown/parametersAndArguments", testMethod = "doTestExpression") model("codeInsight/moveUpDown/parametersAndArguments", testMethod = "doTestExpression")
model("codeInsight/moveUpDown/trailingComma", testMethod = "doTestExpressionWithTrailingComma") model("codeInsight/moveUpDown/trailingComma", testMethod = "doTestExpressionWithTrailingComma")
} }
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.codeInsight.upDownMover;
import com.intellij.codeInsight.editorActions.moveUpDown.LineMover; import com.intellij.codeInsight.editorActions.moveUpDown.LineMover;
import com.intellij.codeInsight.editorActions.moveUpDown.LineRange; import com.intellij.codeInsight.editorActions.moveUpDown.LineRange;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.Pair;
@@ -29,10 +30,8 @@ import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.core.util.PsiLinesUtilsKt; import org.jetbrains.kotlin.idea.core.util.PsiLinesUtilsKt;
import org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringUtilKt; import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.KtBlockExpression; import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.psi.KtFunctionLiteral;
public abstract class AbstractKotlinUpDownMover extends LineMover { public abstract class AbstractKotlinUpDownMover extends LineMover {
protected AbstractKotlinUpDownMover() { protected AbstractKotlinUpDownMover() {
@@ -146,6 +145,23 @@ public abstract class AbstractKotlinUpDownMover extends LineMover {
return lastElement; return lastElement;
} }
@Nullable
protected static KtAnnotationEntry getParentFileAnnotationEntry(@Nullable PsiElement element) {
if (element == null) return null;
KtAnnotationEntry annotationEntry = PsiTreeUtil.getParentOfType(element, KtAnnotationEntry.class);
if (annotationEntry == null) return null;
KtAnnotationUseSiteTarget useSiteTarget = annotationEntry.getUseSiteTarget();
if (useSiteTarget == null) return null;
ASTNode node = useSiteTarget.getNode().getFirstChildNode();
if (node == null) return null;
if (node.getElementType() != KtTokens.FILE_KEYWORD) return null;
return annotationEntry;
}
private static boolean checkCommentAtBlockBound(PsiElement blockElement, PsiElement comment, KtBlockExpression block) { private static boolean checkCommentAtBlockBound(PsiElement blockElement, PsiElement comment, KtBlockExpression block) {
return PsiTreeUtil.isAncestor(block, blockElement, true) && comment instanceof PsiComment; return PsiTreeUtil.isAncestor(block, blockElement, true) && comment instanceof PsiComment;
} }
@@ -164,6 +164,8 @@ public class KotlinDeclarationMover extends AbstractKotlinUpDownMover {
private static KtDeclaration getMovableDeclaration(@Nullable PsiElement element) { private static KtDeclaration getMovableDeclaration(@Nullable PsiElement element) {
if (element == null) return null; if (element == null) return null;
if (getParentFileAnnotationEntry(element) != null) return null;
KtDeclaration declaration = null; KtDeclaration declaration = null;
if (element.getNode().getElementType() == KtTokens.LBRACE) { if (element.getNode().getElementType() == KtTokens.LBRACE) {
KtLambdaExpression lambda = PsiTreeUtil.getParentOfType(element, KtLambdaExpression.class, true); KtLambdaExpression lambda = PsiTreeUtil.getParentOfType(element, KtLambdaExpression.class, true);
@@ -500,6 +500,8 @@ class KotlinExpressionMover : AbstractKotlinUpDownMover() {
return element return element
} }
if (getParentFileAnnotationEntry(element) != null) return null
val movableElement = element.getParentOfTypesAndPredicate( val movableElement = element.getParentOfTypesAndPredicate(
strict = false, strict = false,
parentClasses = *MOVABLE_ELEMENT_CLASSES, parentClasses = *MOVABLE_ELEMENT_CLASSES,
@@ -0,0 +1,5 @@
// MOVE: up
package foo
@file:JvmName("Utils")<caret>
fun g(x: Int) {
}
@@ -0,0 +1,5 @@
// MOVE: up
@file:JvmName("Utils")<caret>
package foo
fun g(x: Int) {
}
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.idea.codeInsight.moveUpDown
import com.intellij.application.options.CodeStyle import com.intellij.application.options.CodeStyle
import com.intellij.codeInsight.editorActions.moveLeftRight.MoveElementLeftAction import com.intellij.codeInsight.editorActions.moveLeftRight.MoveElementLeftAction
import com.intellij.codeInsight.editorActions.moveLeftRight.MoveElementRightAction import com.intellij.codeInsight.editorActions.moveLeftRight.MoveElementRightAction
import com.intellij.codeInsight.editorActions.moveUpDown.LineMover
import com.intellij.codeInsight.editorActions.moveUpDown.MoveStatementDownAction import com.intellij.codeInsight.editorActions.moveUpDown.MoveStatementDownAction
import com.intellij.codeInsight.editorActions.moveUpDown.MoveStatementUpAction import com.intellij.codeInsight.editorActions.moveUpDown.MoveStatementUpAction
import com.intellij.codeInsight.editorActions.moveUpDown.StatementUpDownMover import com.intellij.codeInsight.editorActions.moveUpDown.StatementUpDownMover
@@ -41,6 +42,10 @@ abstract class AbstractMoveStatementTest : AbstractCodeMoverTest() {
doTest(path, KotlinExpressionMover::class.java, true) doTest(path, KotlinExpressionMover::class.java, true)
} }
protected fun doTestLine(path: String) {
doTest(path, LineMover::class.java)
}
private fun doTest(path: String, defaultMoverClass: Class<out StatementUpDownMover>, trailingComma: Boolean = false) { private fun doTest(path: String, defaultMoverClass: Class<out StatementUpDownMover>, trailingComma: Boolean = false) {
doTest(path, trailingComma) { isApplicableExpected, direction -> doTest(path, trailingComma) { isApplicableExpected, direction ->
val movers = Extensions.getExtensions(StatementUpDownMover.STATEMENT_UP_DOWN_MOVER_EP) val movers = Extensions.getExtensions(StatementUpDownMover.STATEMENT_UP_DOWN_MOVER_EP)
@@ -1339,6 +1339,24 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
} }
} }
@TestMetadata("idea/testData/codeInsight/moveUpDown/line")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Line extends AbstractMoveStatementTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTestLine, this, testDataFilePath);
}
public void testAllFilesPresentInLine() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/line"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@TestMetadata("fileAnnotation.kt")
public void testFileAnnotation() throws Exception {
runTest("idea/testData/codeInsight/moveUpDown/line/fileAnnotation.kt");
}
}
@TestMetadata("idea/testData/codeInsight/moveUpDown/parametersAndArguments") @TestMetadata("idea/testData/codeInsight/moveUpDown/parametersAndArguments")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class) @RunWith(JUnit3RunnerWithInners.class)