[kotlin] KTIJ-31100 proper rendering

GitOrigin-RevId: 6e809ed88caa0193f4581f085b01a9246b304087
This commit is contained in:
Andrew Kozlov
2024-09-05 11:16:36 +00:00
committed by intellij-monorepo-bot
parent 446dcfbbda
commit 0b8f4cc1e8
10 changed files with 61 additions and 14 deletions
@@ -91,7 +91,7 @@ internal object TailTextProvider {
}
private fun FqName.asStringForTailText(): String =
if (isRoot) "<root>" else asString()
if (isRoot) "<root>" else render()
context(KaSession)
fun insertLambdaBraces(symbol: KaFunctionType): Boolean {
@@ -5,13 +5,14 @@ import com.intellij.codeInsight.completion.InsertHandler
import com.intellij.codeInsight.completion.InsertionContext
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.openapi.util.NlsSafe
import org.jetbrains.annotations.NonNls
import org.jetbrains.kotlin.analysis.api.KaExperimentalApi
import org.jetbrains.kotlin.analysis.api.KaSession
import org.jetbrains.kotlin.analysis.api.renderer.base.annotations.KaRendererAnnotationsFilter
import org.jetbrains.kotlin.analysis.api.renderer.types.impl.KaTypeRendererForSource
import org.jetbrains.kotlin.analysis.api.symbols.KaClassLikeSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KaClassifierSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KaTypeParameterSymbol
import org.jetbrains.kotlin.analysis.api.types.KaFunctionType
import org.jetbrains.kotlin.analysis.api.types.KaType
import org.jetbrains.kotlin.analysis.api.types.KaTypeParameterType
@@ -20,6 +21,8 @@ import org.jetbrains.kotlin.idea.KotlinIcons
import org.jetbrains.kotlin.idea.completion.lookups.TailTextProvider.getTailText
import org.jetbrains.kotlin.idea.completion.lookups.factories.insertAndShortenReferencesInStringUsingTemporarySuffix
import org.jetbrains.kotlin.idea.completion.lookups.withClassifierSymbolInfo
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.types.Variance
internal object TypeLookupElementFactory {
@@ -52,21 +55,34 @@ internal object TypeLookupElementFactory {
context(KaSession)
fun createLookup(symbol: KaClassifierSymbol): LookupElement? {
val (relativeNameAsString, fqNameAsString) = when (symbol) {
is KaTypeParameterSymbol -> symbol.name.asString().let { it to it }
val relativeName = symbol.name
?: return null
is KaClassLikeSymbol -> when (val classId = symbol.classId) {
null -> symbol.name?.asString()?.let { it to it }
else -> classId.relativeClassName.asString() to classId.asFqNameString()
}
} ?: return null
val descriptor = when (symbol) {
is KaClassLikeSymbol -> symbol.classId
?.let { (packageFqName, relativeClassName, _) ->
ClassDescriptor(
relativeClassName = relativeClassName,
renderedClassId = buildString { // see ClassId::asFqNameString
if (!packageFqName.isRoot) {
append(packageFqName.render())
append(".")
}
append(relativeClassName.render())
},
tailText = getTailText(symbol, usePackageFqName = true),
)
}
val tailText = (symbol as? KaClassLikeSymbol)?.let { getTailText(symbol, usePackageFqName = true) }
else -> null
}
return LookupElementBuilder.create(TypeLookupObject(fqNameAsString), relativeNameAsString)
.withInsertHandler(TypeInsertHandler)
return LookupElementBuilder.create(
/* lookupObject = */ TypeLookupObject(descriptor?.renderedClassId ?: relativeName.render()),
/* lookupString = */ descriptor?.relativeClassName?.asString() ?: relativeName.asString(),
).withInsertHandler(TypeInsertHandler)
.let { withClassifierSymbolInfo(symbol, it) }
.withTailText(tailText)
.withTailText(descriptor?.tailText)
}
private fun KaType.getSymbolIfTypeParameterOrUsualClass(): KaClassifierSymbol? = when (this) {
@@ -93,4 +109,10 @@ private object TypeInsertHandler : InsertHandler<LookupElement> {
val lookupObject = item.`object` as TypeLookupObject
context.insertAndShortenReferencesInStringUsingTemporarySuffix(lookupObject.fqRenderedType)
}
}
}
private data class ClassDescriptor(
val relativeClassName: FqName,
val renderedClassId: @NonNls String,
val tailText: @NlsSafe String,
)
@@ -0,0 +1,3 @@
package `foo bar`
class FooBar
@@ -0,0 +1,4 @@
// IGNORE_K1
fun bar(foo<caret>)
// EXIST: { itemText: "fooBar: FooBar", tailText: " (`foo bar`)" }
@@ -0,0 +1 @@
fun bar(fooBar<caret>)
@@ -0,0 +1,3 @@
package `foo bar`
class FooBar
@@ -0,0 +1,3 @@
import `foo bar`.FooBar
fun bar(fooBar: FooBar<caret>)
@@ -195,6 +195,11 @@ public class MultiFileJvmBasicCompletionTestGenerated extends AbstractMultiFileJ
runTest("../testData/basic/multifile/InImportCompanionObjectMembers/");
}
@TestMetadata("InImportEscaped")
public void testInImportEscaped() throws Exception {
runTest("../testData/basic/multifile/InImportEscaped/");
}
@TestMetadata("InImportExtension")
public void testInImportExtension() throws Exception {
runTest("../testData/basic/multifile/InImportExtension/");
@@ -45,6 +45,7 @@ abstract class AbstractCompletionMultiFileHandlerTest : KotlinFixtureCompletionB
fun testNotImportedTypeAlias() = doTest()
fun testKT12077() = doTest()
fun testClassInRootPackage() = doTest()
fun testInImportEscaped() = if (isFirPlugin) doTest(tailText = " (`foo bar`)") else Unit
fun testPackageDirective() = if (isFirPlugin) doTest() else Unit
fun testPackageInImportDirective() = if (isFirPlugin) doTest() else Unit
@@ -195,6 +195,11 @@ public class HighLevelMultiFileJvmBasicCompletionTestGenerated extends AbstractH
runTest("../../completion/testData/basic/multifile/InImportCompanionObjectMembers/");
}
@TestMetadata("InImportEscaped")
public void testInImportEscaped() throws Exception {
runTest("../../completion/testData/basic/multifile/InImportEscaped/");
}
@TestMetadata("InImportExtension")
public void testInImportExtension() throws Exception {
runTest("../../completion/testData/basic/multifile/InImportExtension/");