From 6b5c31e2fc33912d31f1caf639963e5f48cf740c Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Mon, 18 Nov 2019 16:08:18 +0900 Subject: [PATCH] Move statement up: do not apply to @file annotation #KT-10790 Fixed --- .../kotlin/generators/tests/GenerateTests.kt | 1 + .../AbstractKotlinUpDownMover.java | 24 +++++++++++++++---- .../upDownMover/KotlinDeclarationMover.java | 2 ++ .../upDownMover/KotlinExpressionMover.kt | 2 ++ .../moveUpDown/line/fileAnnotation.kt | 5 ++++ .../moveUpDown/line/fileAnnotation.kt.after | 5 ++++ .../moveUpDown/AbstractCodeMoverTest.kt | 5 ++++ .../MoveStatementTestGenerated.java | 18 ++++++++++++++ 8 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 idea/testData/codeInsight/moveUpDown/line/fileAnnotation.kt create mode 100644 idea/testData/codeInsight/moveUpDown/line/fileAnnotation.kt.after diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 2abe8a9c2d1..dc5bf4b7523 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -466,6 +466,7 @@ fun main(args: Array) { model("codeInsight/moveUpDown/classBodyDeclarations", pattern = KT_OR_KTS, testMethod = "doTestClassBodyDeclaration") model("codeInsight/moveUpDown/closingBraces", 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/trailingComma", testMethod = "doTestExpressionWithTrailingComma") } diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/AbstractKotlinUpDownMover.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/AbstractKotlinUpDownMover.java index 3fe21f12735..915da8d59d8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/AbstractKotlinUpDownMover.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/AbstractKotlinUpDownMover.java @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.codeInsight.upDownMover; import com.intellij.codeInsight.editorActions.moveUpDown.LineMover; import com.intellij.codeInsight.editorActions.moveUpDown.LineRange; +import com.intellij.lang.ASTNode; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; 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.Nullable; import org.jetbrains.kotlin.idea.core.util.PsiLinesUtilsKt; -import org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringUtilKt; -import org.jetbrains.kotlin.psi.KtBlockExpression; -import org.jetbrains.kotlin.psi.KtFile; -import org.jetbrains.kotlin.psi.KtFunctionLiteral; +import org.jetbrains.kotlin.lexer.KtTokens; +import org.jetbrains.kotlin.psi.*; public abstract class AbstractKotlinUpDownMover extends LineMover { protected AbstractKotlinUpDownMover() { @@ -146,6 +145,23 @@ public abstract class AbstractKotlinUpDownMover extends LineMover { 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) { return PsiTreeUtil.isAncestor(block, blockElement, true) && comment instanceof PsiComment; } diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinDeclarationMover.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinDeclarationMover.java index f9dfb2780e9..46f0114e2c6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinDeclarationMover.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinDeclarationMover.java @@ -164,6 +164,8 @@ public class KotlinDeclarationMover extends AbstractKotlinUpDownMover { private static KtDeclaration getMovableDeclaration(@Nullable PsiElement element) { if (element == null) return null; + if (getParentFileAnnotationEntry(element) != null) return null; + KtDeclaration declaration = null; if (element.getNode().getElementType() == KtTokens.LBRACE) { KtLambdaExpression lambda = PsiTreeUtil.getParentOfType(element, KtLambdaExpression.class, true); diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinExpressionMover.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinExpressionMover.kt index 8f89fcae16e..0852e8ee8c2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinExpressionMover.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinExpressionMover.kt @@ -500,6 +500,8 @@ class KotlinExpressionMover : AbstractKotlinUpDownMover() { return element } + if (getParentFileAnnotationEntry(element) != null) return null + val movableElement = element.getParentOfTypesAndPredicate( strict = false, parentClasses = *MOVABLE_ELEMENT_CLASSES, diff --git a/idea/testData/codeInsight/moveUpDown/line/fileAnnotation.kt b/idea/testData/codeInsight/moveUpDown/line/fileAnnotation.kt new file mode 100644 index 00000000000..45c638af39d --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/line/fileAnnotation.kt @@ -0,0 +1,5 @@ +// MOVE: up +package foo +@file:JvmName("Utils") +fun g(x: Int) { +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/line/fileAnnotation.kt.after b/idea/testData/codeInsight/moveUpDown/line/fileAnnotation.kt.after new file mode 100644 index 00000000000..16f78356f86 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/line/fileAnnotation.kt.after @@ -0,0 +1,5 @@ +// MOVE: up +@file:JvmName("Utils") +package foo +fun g(x: Int) { +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/AbstractCodeMoverTest.kt b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/AbstractCodeMoverTest.kt index 71f913619c0..d07c6c39598 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/AbstractCodeMoverTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/AbstractCodeMoverTest.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.idea.codeInsight.moveUpDown import com.intellij.application.options.CodeStyle import com.intellij.codeInsight.editorActions.moveLeftRight.MoveElementLeftAction 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.MoveStatementUpAction import com.intellij.codeInsight.editorActions.moveUpDown.StatementUpDownMover @@ -41,6 +42,10 @@ abstract class AbstractMoveStatementTest : AbstractCodeMoverTest() { doTest(path, KotlinExpressionMover::class.java, true) } + protected fun doTestLine(path: String) { + doTest(path, LineMover::class.java) + } + private fun doTest(path: String, defaultMoverClass: Class, trailingComma: Boolean = false) { doTest(path, trailingComma) { isApplicableExpected, direction -> val movers = Extensions.getExtensions(StatementUpDownMover.STATEMENT_UP_DOWN_MOVER_EP) diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java index a5f62cfd174..654d7f104d8 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java @@ -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") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)