diff --git a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/KotlinMultiFileTestCase.kt b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/KotlinMultiFileTestCase.kt index 2dbfaef9bba..a6f3c635156 100644 --- a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/KotlinMultiFileTestCase.kt +++ b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/KotlinMultiFileTestCase.kt @@ -16,33 +16,11 @@ package org.jetbrains.kotlin.idea.test -import com.intellij.openapi.editor.Document import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess -import com.intellij.psi.PsiDocumentManager import com.intellij.refactoring.MultiFileTestCase -import org.jetbrains.kotlin.idea.util.application.runWriteAction import org.jetbrains.kotlin.test.KotlinTestUtils abstract class KotlinMultiFileTestCase : MultiFileTestCase() { - protected fun extractCaretOffset(doc: Document): Int { - val offset = runWriteAction { - val text = StringBuilder(doc.getText()) - val offset = text.indexOf("") - - if (offset >= 0) { - text.delete(offset, offset + "".length) - doc.setText(text.toString()) - } - - offset - } - - PsiDocumentManager.getInstance(myProject).commitAllDocuments() - PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(doc) - - return offset - } - override fun setUp() { super.setUp() VfsRootAccess.allowRootAccess(KotlinTestUtils.getHomeDirectory()) diff --git a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/testUtils.kt b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/testUtils.kt index c863d3132c2..bd8753e2bb3 100644 --- a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/testUtils.kt +++ b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/testUtils.kt @@ -17,11 +17,13 @@ package org.jetbrains.kotlin.idea.test import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.editor.Document import com.intellij.openapi.module.Module import com.intellij.openapi.project.Project import com.intellij.openapi.roots.ModifiableRootModel import com.intellij.openapi.roots.ModuleRootModificationUtil.updateModel import com.intellij.openapi.vfs.VirtualFile +import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiManager import com.intellij.psi.impl.PsiManagerEx import com.intellij.psi.impl.file.impl.FileManagerImpl @@ -34,6 +36,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.LibraryModificationTracker import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult import org.jetbrains.kotlin.idea.decompiler.KotlinDecompiledFileViewProvider import org.jetbrains.kotlin.idea.decompiler.KtDecompiledFile +import org.jetbrains.kotlin.idea.util.application.runWriteAction import org.jetbrains.kotlin.psi.KtFile import java.util.* @@ -115,4 +118,23 @@ fun unInvalidateBuiltinsAndStdLib(project: Project, runnable: () -> Unit) { fun invalidateLibraryCache(project: Project) { LibraryModificationTracker.getInstance(project).incModificationCount() +} + +fun Document.extractMarkerOffset(project: Project, caretMarker: String = ""): Int { + val offset = runWriteAction { + val text = StringBuilder(getText()) + val offset = text.indexOf(caretMarker) + + if (offset >= 0) { + text.delete(offset, offset + caretMarker.length) + setText(text.toString()) + } + + offset + } + + PsiDocumentManager.getInstance(project).commitAllDocuments() + PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(this) + + return offset } \ No newline at end of file diff --git a/idea/testData/refactoring/rename/automaticRenamerJavaParameter/after/JavaSuper.java b/idea/testData/refactoring/rename/automaticRenamerJavaParameter/after/JavaSuper.java index d02f9e764f2..63592c9e6d8 100644 --- a/idea/testData/refactoring/rename/automaticRenamerJavaParameter/after/JavaSuper.java +++ b/idea/testData/refactoring/rename/automaticRenamerJavaParameter/after/JavaSuper.java @@ -1,4 +1,4 @@ class JavaSuper { - void foo(int /*rename*/aa, String b) { + void foo(int aa, String b) { } } \ No newline at end of file diff --git a/idea/testData/refactoring/rename/automaticRenamerParameter/after/main.kt b/idea/testData/refactoring/rename/automaticRenamerParameter/after/main.kt index 4956257bcdc..da334eaffd7 100644 --- a/idea/testData/refactoring/rename/automaticRenamerParameter/after/main.kt +++ b/idea/testData/refactoring/rename/automaticRenamerParameter/after/main.kt @@ -11,7 +11,7 @@ open class Super { } open class Middle : Super(), Trait { - override fun foo(/*rename*/aa: Int, b: String) { + override fun foo(aa: Int, b: String) { } } diff --git a/idea/testData/refactoring/rename/automaticRenamerParameterInExtension/after/main.kt b/idea/testData/refactoring/rename/automaticRenamerParameterInExtension/after/main.kt index 6ad71794bcd..4f81920253b 100644 --- a/idea/testData/refactoring/rename/automaticRenamerParameterInExtension/after/main.kt +++ b/idea/testData/refactoring/rename/automaticRenamerParameterInExtension/after/main.kt @@ -11,7 +11,7 @@ open class Super { } open class Middle : Super(), Trait { - override fun Int.foo(/*rename*/aa: Int, b: String) { + override fun Int.foo(aa: Int, b: String) { } } diff --git a/idea/testData/refactoring/rename/renameArgumentsWhenParameterRenamed/after/main.kt b/idea/testData/refactoring/rename/renameArgumentsWhenParameterRenamed/after/main.kt index 94f1a649ede..0d507af6982 100644 --- a/idea/testData/refactoring/rename/renameArgumentsWhenParameterRenamed/after/main.kt +++ b/idea/testData/refactoring/rename/renameArgumentsWhenParameterRenamed/after/main.kt @@ -1,4 +1,4 @@ -fun foo(/*rename*/aa: Int, b: String) { +fun foo(aa: Int, b: String) { } fun main(args: Array) { diff --git a/idea/testData/refactoring/rename/renameFieldIdentifier/after/test.kt b/idea/testData/refactoring/rename/renameFieldIdentifier/after/test.kt index 50037226269..03fc9c54c76 100644 --- a/idea/testData/refactoring/rename/renameFieldIdentifier/after/test.kt +++ b/idea/testData/refactoring/rename/renameFieldIdentifier/after/test.kt @@ -1,5 +1,5 @@ class Exp(p1: String) { - val /*rename*/prop11: String = p1 + val prop11: String = p1 get(): String { return field } diff --git a/idea/testData/refactoring/rename/renameKotlinClassByConstructorRef/after/test.kt b/idea/testData/refactoring/rename/renameKotlinClassByConstructorRef/after/test.kt index 62d85bae30c..13becc38b4d 100644 --- a/idea/testData/refactoring/rename/renameKotlinClassByConstructorRef/after/test.kt +++ b/idea/testData/refactoring/rename/renameKotlinClassByConstructorRef/after/test.kt @@ -4,5 +4,5 @@ public open class B() { } fun test() { - /*rename*/B() + B() } \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPrimaryConstructorProperty/after/RenameKotlinPrimaryConstructorProperty.kt b/idea/testData/refactoring/rename/renameKotlinPrimaryConstructorProperty/after/RenameKotlinPrimaryConstructorProperty.kt index e901547c5a5..83e5977bf54 100644 --- a/idea/testData/refactoring/rename/renameKotlinPrimaryConstructorProperty/after/RenameKotlinPrimaryConstructorProperty.kt +++ b/idea/testData/refactoring/rename/renameKotlinPrimaryConstructorProperty/after/RenameKotlinPrimaryConstructorProperty.kt @@ -1,6 +1,6 @@ package testing.rename -public open class Foo(public open val /*rename*/second: String) +public open class Foo(public open val second: String) public class Bar : Foo("abc") { override val second = "xyzzy" diff --git a/idea/testData/refactoring/rename/renameKotlinPrimaryConstructorPropertyFromOverride/after/RenameKotlinPrimaryConstructorPropertyFromOverride.kt b/idea/testData/refactoring/rename/renameKotlinPrimaryConstructorPropertyFromOverride/after/RenameKotlinPrimaryConstructorPropertyFromOverride.kt index 85a3f232acd..43a7e031aa1 100644 --- a/idea/testData/refactoring/rename/renameKotlinPrimaryConstructorPropertyFromOverride/after/RenameKotlinPrimaryConstructorPropertyFromOverride.kt +++ b/idea/testData/refactoring/rename/renameKotlinPrimaryConstructorPropertyFromOverride/after/RenameKotlinPrimaryConstructorPropertyFromOverride.kt @@ -2,7 +2,7 @@ package testing.rename public open class Foo(public open val second: String) -public class Bar(public override val /*rename*/second: String) : Foo(second) { +public class Bar(public override val second: String) : Foo(second) { } fun usages(f: Foo, b: Bar): String { diff --git a/idea/testData/refactoring/rename/renameKotlinTopLevelVarProperty/after/RenameTopLevelVarProperty.kt b/idea/testData/refactoring/rename/renameKotlinTopLevelVarProperty/after/RenameTopLevelVarProperty.kt index ea708325a81..0f5e7cb4a94 100644 --- a/idea/testData/refactoring/rename/renameKotlinTopLevelVarProperty/after/RenameTopLevelVarProperty.kt +++ b/idea/testData/refactoring/rename/renameKotlinTopLevelVarProperty/after/RenameTopLevelVarProperty.kt @@ -1,3 +1,3 @@ package testing.rename -var /*rename*/bar: String = "xyzzy" +var bar: String = "xyzzy" diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/AbstractMultiFileIntentionTest.kt b/idea/tests/org/jetbrains/kotlin/idea/intentions/AbstractMultiFileIntentionTest.kt index ad1bd29b6c2..3195d813acb 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/AbstractMultiFileIntentionTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/AbstractMultiFileIntentionTest.kt @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.idea.jsonUtils.getString import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil import org.jetbrains.kotlin.idea.test.KotlinMultiFileTestCase import org.jetbrains.kotlin.idea.test.PluginTestCaseBase +import org.jetbrains.kotlin.idea.test.extractMarkerOffset import org.jetbrains.kotlin.idea.util.application.executeWriteCommand import org.jetbrains.kotlin.test.KotlinTestUtils import org.junit.Assert @@ -53,7 +54,7 @@ abstract class AbstractMultiFileIntentionTest : KotlinMultiFileTestCase() { val conflictFile = rootDir.findFileByRelativePath("$mainFilePath.conflicts") val document = FileDocumentManager.getInstance().getDocument(mainFile)!! val editor = EditorFactory.getInstance()!!.createEditor(document, project!!)!! - editor.caretModel.moveToOffset(extractCaretOffset(document)) + editor.caretModel.moveToOffset(document.extractMarkerOffset(project)) val mainPsiFile = PsiManager.getInstance(project!!).findFile(mainFile)!! try { diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/AbstractMoveTest.kt b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/AbstractMoveTest.kt index cfadddc4aef..dac4b734d47 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/AbstractMoveTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/AbstractMoveTest.kt @@ -49,7 +49,7 @@ import org.jetbrains.kotlin.idea.stubindex.KotlinFullClassNameIndex import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil import org.jetbrains.kotlin.idea.test.KotlinMultiFileTestCase import org.jetbrains.kotlin.idea.test.PluginTestCaseBase -import org.jetbrains.kotlin.idea.util.application.runWriteAction +import org.jetbrains.kotlin.idea.test.extractMarkerOffset import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.psi.KtFile @@ -80,7 +80,7 @@ abstract class AbstractMoveTest : KotlinMultiFileTestCase() { val document = FileDocumentManager.getInstance().getDocument(mainFile)!! val editor = EditorFactory.getInstance()!!.createEditor(document, project!!)!! - val caretOffset = extractCaretOffset(document) + val caretOffset = document.extractMarkerOffset(project) val elementAtCaret = if (caretOffset >= 0) { TargetElementUtilBase.getInstance()!!.findTargetElement( editor, diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/AbstractRenameTest.kt b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/AbstractRenameTest.kt index 759d9ccded9..e007ba91866 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/AbstractRenameTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/AbstractRenameTest.kt @@ -147,17 +147,17 @@ abstract class AbstractRenameTest : KotlinMultiFileTestCase() { val mainFile = rootDir.findChild(mainFilePath)!! val psiFile = PsiManager.getInstance(context.project).findFile(mainFile)!! - val MARKER_TEXT = "/*rename*/" - val marker = psiFile.text.indexOf(MARKER_TEXT) + val doc = PsiDocumentManager.getInstance(project).getDocument(psiFile)!! + val marker = doc.extractMarkerOffset(project, "/*rename*/") assert(marker != -1) val toRename = if (renameParamsObject["byRef"]?.asBoolean ?: false) { val editor = createEditor(mainFile) - editor.caretModel.moveToOffset(marker + MARKER_TEXT.length) + editor.caretModel.moveToOffset(marker) TargetElementUtilBase.findTargetElement(editor, TargetElementUtilBase.getInstance().allAccepted)!! } else { - psiFile.findElementAt(marker + MARKER_TEXT.length)!!.getNonStrictParentOfType()!! + psiFile.findElementAt(marker)!!.getNonStrictParentOfType()!! } val substitution = RenamePsiElementProcessor.forElement(toRename).substituteElementToRename(toRename, null)