FIR IDE: do not fail whole completion on exception in lookup element creation
This commit is contained in:
+1
-1
@@ -68,7 +68,7 @@ private class KotlinAvailableScopesCompletionProvider(prefixMatcher: PrefixMatch
|
|||||||
|
|
||||||
private fun CompletionResultSet.addSymbolToCompletion(symbol: KtSymbol) {
|
private fun CompletionResultSet.addSymbolToCompletion(symbol: KtSymbol) {
|
||||||
if (symbol !is KtNamedSymbol) return
|
if (symbol !is KtNamedSymbol) return
|
||||||
addElement(lookupElementFactory.createLookupElement(symbol))
|
lookupElementFactory.createLookupElement(symbol)?.let(::addElement)
|
||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(InvalidWayOfUsingAnalysisSession::class)
|
@OptIn(InvalidWayOfUsingAnalysisSession::class)
|
||||||
|
|||||||
+20
-7
@@ -11,7 +11,10 @@ import com.intellij.codeInsight.completion.InsertionContext
|
|||||||
import com.intellij.codeInsight.lookup.Lookup
|
import com.intellij.codeInsight.lookup.Lookup
|
||||||
import com.intellij.codeInsight.lookup.LookupElement
|
import com.intellij.codeInsight.lookup.LookupElement
|
||||||
import com.intellij.codeInsight.lookup.LookupElementBuilder
|
import com.intellij.codeInsight.lookup.LookupElementBuilder
|
||||||
|
import com.intellij.openapi.diagnostic.ControlFlowException
|
||||||
|
import com.intellij.openapi.diagnostic.logger
|
||||||
import com.intellij.openapi.editor.Document
|
import com.intellij.openapi.editor.Document
|
||||||
|
import com.intellij.openapi.progress.ProcessCanceledException
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.kotlin.idea.frontend.api.symbols.*
|
import org.jetbrains.kotlin.idea.frontend.api.symbols.*
|
||||||
@@ -30,14 +33,14 @@ internal class KotlinFirLookupElementFactory {
|
|||||||
private val functionLookupElementFactory = FunctionLookupElementFactory()
|
private val functionLookupElementFactory = FunctionLookupElementFactory()
|
||||||
private val typeParameterLookupElementFactory = TypeParameterLookupElementFactory()
|
private val typeParameterLookupElementFactory = TypeParameterLookupElementFactory()
|
||||||
|
|
||||||
fun createLookupElement(symbol: KtNamedSymbol): LookupElement {
|
fun createLookupElement(symbol: KtNamedSymbol): LookupElement? {
|
||||||
val elementBuilder = when (symbol) {
|
val elementBuilder = when (symbol) {
|
||||||
is KtFunctionSymbol -> functionLookupElementFactory.createLookup(symbol)
|
is KtFunctionSymbol -> functionLookupElementFactory.createLookup(symbol)
|
||||||
is KtVariableLikeSymbol -> variableLookupElementFactory.createLookup(symbol)
|
is KtVariableLikeSymbol -> variableLookupElementFactory.createLookup(symbol)
|
||||||
is KtClassLikeSymbol -> classLookupElementFactory.createLookup(symbol)
|
is KtClassLikeSymbol -> classLookupElementFactory.createLookup(symbol)
|
||||||
is KtTypeParameterSymbol -> typeParameterLookupElementFactory.createLookup(symbol)
|
is KtTypeParameterSymbol -> typeParameterLookupElementFactory.createLookup(symbol)
|
||||||
else -> throw IllegalArgumentException("Cannot create a lookup element for $symbol")
|
else -> throw IllegalArgumentException("Cannot create a lookup element for $symbol")
|
||||||
}
|
} ?: return null
|
||||||
|
|
||||||
return elementBuilder
|
return elementBuilder
|
||||||
.withPsiElement(symbol.psi) // TODO check if it is a heavy operation and should be postponed
|
.withPsiElement(symbol.psi) // TODO check if it is a heavy operation and should be postponed
|
||||||
@@ -75,11 +78,17 @@ private class VariableLookupElementFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class FunctionLookupElementFactory {
|
private class FunctionLookupElementFactory {
|
||||||
fun createLookup(symbol: KtFunctionSymbol): LookupElementBuilder {
|
fun createLookup(symbol: KtFunctionSymbol): LookupElementBuilder? {
|
||||||
return LookupElementBuilder.create(UniqueLookupObject(), symbol.name.asString())
|
return try {
|
||||||
.withTailText(getTailText(symbol), true)
|
LookupElementBuilder.create(UniqueLookupObject(), symbol.name.asString())
|
||||||
.withTypeText(ShortNamesRenderer.renderType(symbol.type))
|
.withTailText(getTailText(symbol), true)
|
||||||
.withInsertHandler(createInsertHandler(symbol))
|
.withTypeText(ShortNamesRenderer.renderType(symbol.type))
|
||||||
|
.withInsertHandler(createInsertHandler(symbol))
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
if (e is ControlFlowException) throw e
|
||||||
|
LOG.error(e)
|
||||||
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getTailText(symbol: KtFunctionSymbol): String {
|
private fun getTailText(symbol: KtFunctionSymbol): String {
|
||||||
@@ -98,6 +107,10 @@ private class FunctionLookupElementFactory {
|
|||||||
insertEmptyLambda = insertLambdaBraces(symbol)
|
insertEmptyLambda = insertLambdaBraces(symbol)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val LOG = logger<FunctionLookupElementFactory>()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user