Imports on paste: added checking of symbols to import into test + fixed bug on copy/paste import/package directive
This commit is contained in:
+14
-5
@@ -68,8 +68,8 @@ import java.util.ArrayList
|
||||
public class KotlinCopyPasteReferenceProcessor() : CopyPastePostProcessor<KotlinReferenceTransferableData>() {
|
||||
private val LOG = Logger.getInstance(javaClass<KotlinCopyPasteReferenceProcessor>())
|
||||
|
||||
private val IGNORE_REFERENCES_INSIDE: Array<Class<out JetElement>?> = array(
|
||||
javaClass<JetImportDirective>(),
|
||||
private val IGNORE_REFERENCES_INSIDE: Array<Class<out JetElement>> = array(
|
||||
javaClass<JetImportList>(),
|
||||
javaClass<JetPackageDirective>()
|
||||
)
|
||||
|
||||
@@ -138,10 +138,12 @@ public class KotlinCopyPasteReferenceProcessor() : CopyPastePostProcessor<Kotlin
|
||||
startOffsets: IntArray,
|
||||
endOffsets: IntArray
|
||||
) {
|
||||
if (PsiTreeUtil.getParentOfType(element, *IGNORE_REFERENCES_INSIDE) != null) return
|
||||
if (PsiTreeUtil.getNonStrictParentOfType(element, *IGNORE_REFERENCES_INSIDE) != null) return
|
||||
|
||||
element.accept(object : PsiElementVisitor() {
|
||||
override fun visitElement(element: PsiElement) {
|
||||
if (element.javaClass in IGNORE_REFERENCES_INSIDE) return
|
||||
|
||||
element.acceptChildren(this)
|
||||
|
||||
val reference = element.getReference() as? JetReference ?: return
|
||||
@@ -322,13 +324,15 @@ public class KotlinCopyPasteReferenceProcessor() : CopyPastePostProcessor<Kotlin
|
||||
}
|
||||
|
||||
private fun showRestoreReferencesDialog(project: Project, referencesToRestore: List<ReferenceToRestoreData>): Collection<ReferenceToRestoreData> {
|
||||
val fqNames = referencesToRestore.map { it.refData.fqName }.toSortedSet()
|
||||
|
||||
declarationsToImportSuggested = fqNames
|
||||
|
||||
val shouldShowDialog = CodeInsightSettings.getInstance().ADD_IMPORTS_ON_PASTE == CodeInsightSettings.ASK
|
||||
if (!shouldShowDialog || referencesToRestore.isEmpty()) {
|
||||
return referencesToRestore
|
||||
}
|
||||
|
||||
val fqNames = referencesToRestore.map { it.refData.fqName }.toSortedSet()
|
||||
|
||||
val dialog = RestoreReferencesDialog(project, fqNames.copyToArray())
|
||||
dialog.show()
|
||||
|
||||
@@ -345,4 +349,9 @@ public class KotlinCopyPasteReferenceProcessor() : CopyPastePostProcessor<Kotlin
|
||||
if (getContainingFile() != fileCopiedFrom) return false
|
||||
return toTextRanges(startOffsets, endOffsets).any { this.range in it }
|
||||
}
|
||||
|
||||
companion object {
|
||||
// for tests
|
||||
public var declarationsToImportSuggested: Collection<String> = emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
a.E.ENTRY
|
||||
a.Outer.Inner
|
||||
a.Outer.Nested
|
||||
a.Outer.NestedAnnotation
|
||||
a.Outer.NestedEnum
|
||||
a.Outer.NestedObj
|
||||
a.Outer.NestedTrait
|
||||
@@ -0,0 +1,5 @@
|
||||
a.A
|
||||
a.A.Companion
|
||||
a.B
|
||||
a.B.Companion
|
||||
a.T
|
||||
@@ -0,0 +1,5 @@
|
||||
a.Outer.Companion.Nested
|
||||
a.Outer.Companion.NestedAnnotation
|
||||
a.Outer.Companion.NestedEnum
|
||||
a.Outer.Companion.NestedObj
|
||||
a.Outer.Companion.NestedTrait
|
||||
@@ -0,0 +1,2 @@
|
||||
a.a
|
||||
a.a.Companion
|
||||
@@ -0,0 +1 @@
|
||||
a.A
|
||||
@@ -0,0 +1 @@
|
||||
a.A
|
||||
@@ -0,0 +1 @@
|
||||
a.A
|
||||
@@ -0,0 +1 @@
|
||||
a.A
|
||||
@@ -0,0 +1,6 @@
|
||||
a.Outer
|
||||
a.Outer.Inner.II
|
||||
a.Outer.Nested.NI
|
||||
a.Outer.Nested.NN
|
||||
a.Outer.Nested.NN2
|
||||
a.with
|
||||
@@ -0,0 +1 @@
|
||||
A
|
||||
@@ -0,0 +1,3 @@
|
||||
a.A
|
||||
a.get
|
||||
a.set
|
||||
@@ -0,0 +1,3 @@
|
||||
java.JavaClass
|
||||
java.JavaClass.NestedClass
|
||||
java.JavaClass.staticMethod
|
||||
@@ -0,0 +1,15 @@
|
||||
d.A
|
||||
d.ClassObject.Companion
|
||||
d.E.ENTRY
|
||||
d.O1
|
||||
d.O2
|
||||
d.Outer.Inner
|
||||
d.Outer.Nested
|
||||
d.Outer.NestedAnnotation
|
||||
d.Outer.NestedEnum
|
||||
d.Outer.NestedObj
|
||||
d.Outer.NestedTrait
|
||||
d.T
|
||||
d.c
|
||||
d.ext
|
||||
d.g
|
||||
@@ -0,0 +1,2 @@
|
||||
kotlin.Int
|
||||
kotlin.concurrent.FunctionalList
|
||||
@@ -0,0 +1,3 @@
|
||||
a.A.A
|
||||
a.A.B
|
||||
a.A.C
|
||||
@@ -0,0 +1,3 @@
|
||||
a.f
|
||||
a.g
|
||||
a.p
|
||||
@@ -0,0 +1,3 @@
|
||||
a.A
|
||||
a.infix
|
||||
a.plus
|
||||
@@ -0,0 +1,2 @@
|
||||
a.ext
|
||||
a.p
|
||||
@@ -0,0 +1,5 @@
|
||||
a.ext
|
||||
a.infix
|
||||
a.minus
|
||||
a.p
|
||||
a.plus
|
||||
@@ -0,0 +1,4 @@
|
||||
a.A
|
||||
a.hasNext
|
||||
a.iterator
|
||||
a.next
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
a.g
|
||||
@@ -0,0 +1,2 @@
|
||||
a.A
|
||||
a.get
|
||||
@@ -0,0 +1,15 @@
|
||||
d.A
|
||||
d.ClassObject.Companion
|
||||
d.E.ENTRY
|
||||
d.O1
|
||||
d.O2
|
||||
d.Outer.Inner
|
||||
d.Outer.Nested
|
||||
d.Outer.NestedAnnotation
|
||||
d.Outer.NestedEnum
|
||||
d.Outer.NestedObj
|
||||
d.Outer.NestedTrait
|
||||
d.T
|
||||
d.c
|
||||
d.ext
|
||||
d.g
|
||||
@@ -0,0 +1,3 @@
|
||||
a.A.Nested
|
||||
a.ext
|
||||
a.f
|
||||
@@ -0,0 +1,7 @@
|
||||
a.Outer
|
||||
a.Outer.Inner
|
||||
a.Outer.Nested
|
||||
a.Outer.NestedAnnotation
|
||||
a.Outer.NestedEnum
|
||||
a.Outer.NestedObj
|
||||
a.Outer.NestedTrait
|
||||
@@ -0,0 +1,2 @@
|
||||
a.A
|
||||
a.invoke
|
||||
@@ -0,0 +1,2 @@
|
||||
a.JavaF
|
||||
a.JavaM
|
||||
@@ -0,0 +1,3 @@
|
||||
a.A
|
||||
a.component1
|
||||
a.component2
|
||||
@@ -0,0 +1,4 @@
|
||||
a.A
|
||||
a.B
|
||||
a.hasNext
|
||||
a.next
|
||||
@@ -0,0 +1,2 @@
|
||||
a.b
|
||||
a.d
|
||||
@@ -0,0 +1,7 @@
|
||||
a.E.ENTRY
|
||||
a.Outer.Inner
|
||||
a.Outer.Nested
|
||||
a.Outer.NestedAnnotation
|
||||
a.Outer.NestedEnum
|
||||
a.Outer.NestedObj
|
||||
a.Outer.NestedTrait
|
||||
@@ -0,0 +1,3 @@
|
||||
a.A
|
||||
a.B
|
||||
a.T
|
||||
@@ -0,0 +1 @@
|
||||
a.b
|
||||
@@ -0,0 +1 @@
|
||||
a.Foo
|
||||
@@ -0,0 +1 @@
|
||||
a.A
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
a.b
|
||||
a.c
|
||||
@@ -0,0 +1 @@
|
||||
a.A
|
||||
@@ -0,0 +1,2 @@
|
||||
a.A
|
||||
a.T
|
||||
@@ -17,22 +17,20 @@
|
||||
package org.jetbrains.kotlin.idea.codeInsight
|
||||
|
||||
import com.intellij.openapi.actionSystem.IdeActions
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import org.jetbrains.kotlin.checkers.DebugInfoUtil
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticUtils
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages
|
||||
import org.jetbrains.kotlin.idea.AbstractCopyPasteTest
|
||||
import org.jetbrains.kotlin.idea.PluginTestCaseBase
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.*
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
|
||||
import org.jetbrains.kotlin.psi.JetFile
|
||||
import org.jetbrains.kotlin.psi.JetReferenceExpression
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.test.InTextDirectivesUtils
|
||||
|
||||
import org.jetbrains.kotlin.test.JetTestUtils
|
||||
import java.io.File
|
||||
import kotlin.test.fail
|
||||
|
||||
@@ -61,10 +59,15 @@ public abstract class AbstractInsertImportOnPasteTest : AbstractCopyPasteTest()
|
||||
myFixture.configureByFile(testFileName)
|
||||
myFixture.performEditorAction(cutOrCopy)
|
||||
|
||||
KotlinCopyPasteReferenceProcessor.declarationsToImportSuggested = emptyList()
|
||||
|
||||
val toFileName = testFileName.replace(".kt", ".to.kt")
|
||||
val toFile = configureToFile(toFileName)
|
||||
performNotWriteEditorAction(IdeActions.ACTION_PASTE)
|
||||
|
||||
val namesToImportDump = KotlinCopyPasteReferenceProcessor.declarationsToImportSuggested.joinToString("\n")
|
||||
JetTestUtils.assertEqualsToFile(File(path.replace(".kt", ".expected.names")), namesToImportDump)
|
||||
|
||||
myFixture.checkResultByFile(testFileName.replace(".kt", ".expected.kt"))
|
||||
|
||||
if (!InTextDirectivesUtils.isDirectiveDefined(FileUtil.loadFile(testFile, true), ALLOW_UNRESOLVED_DIRECTIVE)) {
|
||||
|
||||
Reference in New Issue
Block a user