Hint action to update usages on cut/paste of top-level declarations

This commit is contained in:
Valentin Kipyatkov
2017-03-19 16:51:25 +03:00
parent cb5459b7d7
commit 40bbf82a41
31 changed files with 759 additions and 10 deletions
@@ -0,0 +1,89 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.codeInsight
import com.intellij.codeInsight.actions.OptimizeImportsProcessor
import com.intellij.openapi.actionSystem.IdeActions
import com.intellij.openapi.util.io.FileUtil
import com.intellij.psi.PsiDocumentManager
import junit.framework.TestCase
import org.jetbrains.kotlin.idea.AbstractCopyPasteTest
import org.jetbrains.kotlin.idea.refactoring.cutPaste.MoveDeclarationsEditorCookie
import org.jetbrains.kotlin.idea.refactoring.cutPaste.MoveDeclarationsIntentionAction
import org.jetbrains.kotlin.idea.refactoring.cutPaste.MoveDeclarationsProcessor
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
import org.jetbrains.kotlin.idea.test.dumpTextWithErrors
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.test.KotlinTestUtils
import java.io.File
abstract class AbstractMoveOnCutPasteTest : AbstractCopyPasteTest() {
private val BASE_PATH = PluginTestCaseBase.getTestDataPathBase() + "/copyPaste/moveDeclarations"
private val OPTIMIZE_IMPORTS_AFTER_CUT_DIRECTIVE = "// OPTIMIZE_IMPORTS_AFTER_CUT"
private val IS_AVAILABLE_DIRECTIVE = "// IS_AVAILABLE:"
private val COPY_DIRECTIVE = "// COPY"
override fun getTestDataPath() = BASE_PATH
protected fun doTest(sourceFilePath: String) {
myFixture.testDataPath = BASE_PATH
val testFile = File(sourceFilePath)
val sourceFileName = testFile.name
val testFileText = FileUtil.loadFile(testFile, true)
val dependencyFileName = sourceFileName.replace(".kt", ".dependency.kt")
val dependencyPsiFile = configureByDependencyIfExists(dependencyFileName) as KtFile?
val sourcePsiFile = myFixture.configureByFile(sourceFileName) as KtFile
val useCopy = InTextDirectivesUtils.isDirectiveDefined(testFileText, COPY_DIRECTIVE)
myFixture.performEditorAction(if (useCopy) IdeActions.ACTION_COPY else IdeActions.ACTION_CUT)
PsiDocumentManager.getInstance(project).commitAllDocuments()
if (InTextDirectivesUtils.isDirectiveDefined(testFileText, OPTIMIZE_IMPORTS_AFTER_CUT_DIRECTIVE)) {
OptimizeImportsProcessor(project, sourcePsiFile).run()
}
editor.putUserData(MoveDeclarationsEditorCookie.KEY, null) // because editor is reused
val targetFileName = sourceFileName.replace(".kt", ".to.kt")
val targetPsiFile = configureTargetFile(targetFileName)
performNotWriteEditorAction(IdeActions.ACTION_PASTE)
val shouldBeAvailable = InTextDirectivesUtils.getPrefixedBoolean(testFileText, IS_AVAILABLE_DIRECTIVE) ?: true
val cookie = editor.getUserData(MoveDeclarationsEditorCookie.KEY)
val processor = cookie?.let { MoveDeclarationsProcessor.build(editor, cookie) }
TestCase.assertEquals(shouldBeAvailable, processor != null)
if (processor != null) {
processor.performRefactoring()
PsiDocumentManager.getInstance(project).commitAllDocuments()
if (dependencyPsiFile != null) {
KotlinTestUtils.assertEqualsToFile(File(BASE_PATH, dependencyFileName.replace(".kt", ".expected.kt")),
dependencyPsiFile.dumpTextWithErrors())
}
KotlinTestUtils.assertEqualsToFile(File(BASE_PATH, sourceFileName.replace(".kt", ".expected.kt")),
sourcePsiFile.dumpTextWithErrors())
KotlinTestUtils.assertEqualsToFile(File(BASE_PATH, targetFileName.replace(".kt", ".expected.kt")),
targetPsiFile.dumpTextWithErrors())
}
}
}
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.codeInsight;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TargetBackend;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("idea/testData/copyPaste/moveDeclarations")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class MoveOnCutPasteTestGenerated extends AbstractMoveOnCutPasteTest {
public void testAllFilesPresentInMoveDeclarations() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/copyPaste/moveDeclarations"), Pattern.compile("^([^.]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("ChangePackage.kt")
public void testChangePackage() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/copyPaste/moveDeclarations/ChangePackage.kt");
doTest(fileName);
}
@TestMetadata("Copy.kt")
public void testCopy() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/copyPaste/moveDeclarations/Copy.kt");
doTest(fileName);
}
@TestMetadata("OptimizeImportsAfterCut.kt")
public void testOptimizeImportsAfterCut() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/copyPaste/moveDeclarations/OptimizeImportsAfterCut.kt");
doTest(fileName);
}
@TestMetadata("SamePackage.kt")
public void testSamePackage() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/copyPaste/moveDeclarations/SamePackage.kt");
doTest(fileName);
}
}