Code Insight: Implement package references inside of string literals
#KT-12136 In Progress
This commit is contained in:
@@ -4,6 +4,10 @@
|
||||
language="kotlin"
|
||||
implementation="org.jetbrains.kotlin.idea.spring.references.KotlinSpringReferenceContributor"
|
||||
order="before kotlinFilePathReferenceContributor" />
|
||||
<psi.referenceContributor
|
||||
language="kotlin"
|
||||
implementation="org.jetbrains.kotlin.idea.jam.KotlinJamReferenceContributor"
|
||||
order="before kotlinFilePathReferenceContributor" />
|
||||
|
||||
<multiHostInjector implementation="org.jetbrains.kotlin.idea.spring.el.KotlinSpringELInjector"/>
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package org.jetbrains.kotlin.idea.jam
|
||||
|
||||
import com.intellij.codeInsight.completion.CompletionUtil
|
||||
import com.intellij.jam.JamService
|
||||
import com.intellij.jam.JamStringAttributeElement
|
||||
import com.intellij.jam.reflect.JamStringAttributeMeta
|
||||
import com.intellij.psi.PsiElementRef
|
||||
import com.intellij.psi.PsiReference
|
||||
import com.intellij.psi.PsiReferenceRegistrar
|
||||
import com.intellij.util.SmartList
|
||||
import org.jetbrains.kotlin.asJava.toLightAnnotation
|
||||
import org.jetbrains.kotlin.asJava.unwrapped
|
||||
import org.jetbrains.kotlin.idea.references.AbstractKotlinReferenceContributor
|
||||
import org.jetbrains.kotlin.psi.KtAnnotationEntry
|
||||
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
|
||||
import org.jetbrains.kotlin.psi.KtValueArgument
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isPlain
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parents
|
||||
|
||||
// Based on the JamReferenceContributor
|
||||
class KotlinJamReferenceContributor : AbstractKotlinReferenceContributor() {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {
|
||||
registrar.registerMultiProvider<KtStringTemplateExpression> { expression ->
|
||||
if (!expression.isPlain()) return@registerMultiProvider PsiReference.EMPTY_ARRAY
|
||||
val argument = expression
|
||||
.parents
|
||||
.filterIsInstance<KtValueArgument>()
|
||||
.firstOrNull() { it.parent?.parent is KtAnnotationEntry } ?: return@registerMultiProvider PsiReference.EMPTY_ARRAY
|
||||
val annotationEntry = argument.parent.parent as KtAnnotationEntry
|
||||
val lightAnnotation = (CompletionUtil.getOriginalOrSelf(annotationEntry)).toLightAnnotation()
|
||||
?: return@registerMultiProvider PsiReference.EMPTY_ARRAY
|
||||
|
||||
val service = JamService.getJamService(expression.project)
|
||||
val annotationMeta = service.getMeta(lightAnnotation)
|
||||
?: return@registerMultiProvider PsiReference.EMPTY_ARRAY
|
||||
val attributeName = argument.getArgumentName()?.asName?.asString()
|
||||
val attribute = annotationMeta.findAttribute(attributeName) as? JamStringAttributeMeta<Any, Any>
|
||||
?: return@registerMultiProvider PsiReference.EMPTY_ARRAY
|
||||
val jam = attribute.getJam(PsiElementRef.real(lightAnnotation))
|
||||
val converter = attribute.converter
|
||||
when (jam) {
|
||||
is JamStringAttributeElement<*> -> converter.createReferences(jam as JamStringAttributeElement<Any>)
|
||||
is List<*> -> {
|
||||
val list = SmartList<PsiReference>()
|
||||
for (item in jam) {
|
||||
val jamElement = item as? JamStringAttributeElement<Any> ?: continue
|
||||
if (jamElement.psiElement?.unwrapped != expression) continue
|
||||
list += converter.createReferences(jamElement)
|
||||
}
|
||||
list.toTypedArray()
|
||||
}
|
||||
else -> PsiReference.EMPTY_ARRAY
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// ELEMENT: java
|
||||
// CHAR: \n
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan
|
||||
import org.springframework.context.annotation.Configuration
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = arrayOf("ja<caret>va"))
|
||||
open class App {
|
||||
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// ELEMENT: java
|
||||
// CHAR: \n
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan
|
||||
import org.springframework.context.annotation.Configuration
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = arrayOf("javava"))
|
||||
open class App {
|
||||
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// ELEMENT: java
|
||||
// CHAR: \t
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan
|
||||
import org.springframework.context.annotation.Configuration
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = arrayOf("ja<caret>va"))
|
||||
open class App {
|
||||
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// ELEMENT: java
|
||||
// CHAR: \t
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan
|
||||
import org.springframework.context.annotation.Configuration
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = arrayOf("java"))
|
||||
open class App {
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// NUMBER: 2
|
||||
// EXIST: { lookupString:"java" }
|
||||
// EXIST: { lookupString:"javax" }
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan
|
||||
import org.springframework.context.annotation.Configuration
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = arrayOf("ja<caret>va"))
|
||||
open class App {
|
||||
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// NO_XML_CONFIG
|
||||
// REF: java
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan
|
||||
import org.springframework.context.annotation.Configuration
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = arrayOf("<caret>java"))
|
||||
open class App {
|
||||
|
||||
}
|
||||
+12
@@ -35,6 +35,18 @@ public class SpringReferenceCompletionHandlerTestGenerated extends AbstractSprin
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("ultimate/testData/spring/core/references/completion/handler"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("packageReferenceEnter.kt")
|
||||
public void testPackageReferenceEnter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("ultimate/testData/spring/core/references/completion/handler/packageReferenceEnter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("packageReferenceTab.kt")
|
||||
public void testPackageReferenceTab() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("ultimate/testData/spring/core/references/completion/handler/packageReferenceTab.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("qualifierReferenceEnter.kt")
|
||||
public void testQualifierReferenceEnter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("ultimate/testData/spring/core/references/completion/handler/qualifierReferenceEnter.kt");
|
||||
|
||||
+6
@@ -47,6 +47,12 @@ public class SpringReferenceCompletionTestGenerated extends AbstractSpringRefere
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("packageReference.kt")
|
||||
public void testPackageReference() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("ultimate/testData/spring/core/references/completion/variants/packageReference.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("qualifierReference1Xml.kt")
|
||||
public void testQualifierReference1Xml() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("ultimate/testData/spring/core/references/completion/variants/qualifierReference1Xml.kt");
|
||||
|
||||
+6
@@ -47,6 +47,12 @@ public class SpringReferenceNavigationTestGenerated extends AbstractSpringRefere
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("packageReferenceInComponentScan.kt")
|
||||
public void testPackageReferenceInComponentScan() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("ultimate/testData/spring/core/references/navigation/packageReferenceInComponentScan.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("qualifierReference.kt")
|
||||
public void testQualifierReference() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("ultimate/testData/spring/core/references/navigation/qualifierReference.kt");
|
||||
|
||||
Reference in New Issue
Block a user