[completion][kotlin] refactor/clean up KotlinFunctionInsertHandler.kt and CompositeDeclarativeInsertHandler

Deleted unnecessary comments, also grouped insertHandlers by String of completion chars instead of just per char.

GitOrigin-RevId: 9f0920c0da604830724ff3766e6e9f522aa928a7
This commit is contained in:
Vadim Makarov
2021-07-28 11:57:53 +00:00
committed by intellij-monorepo-bot
parent 34006065ab
commit 903d772e5a
3 changed files with 28 additions and 55 deletions
@@ -18,13 +18,13 @@ public class AddSpaceInsertHandler implements InsertHandler<LookupElement> {
private final static String VALID_COMPLETION_CHARS = "\u0000\n\t\r(,.:=";
public final static InsertHandler<LookupElement> INSTANCE = CompositeDeclarativeInsertHandler.Companion
.withUniversalHandler(VALID_COMPLETION_CHARS.toCharArray(),
.withUniversalHandler(VALID_COMPLETION_CHARS,
new SingleInsertionDeclarativeInsertHandler(" ",
DeclarativeInsertHandler2.PopupOptions.DoNotShow.INSTANCE)
);
public final static InsertHandler<LookupElement> INSTANCE_WITH_AUTO_POPUP = CompositeDeclarativeInsertHandler.Companion
.withUniversalHandler(VALID_COMPLETION_CHARS.toCharArray(),
.withUniversalHandler(VALID_COMPLETION_CHARS,
new SingleInsertionDeclarativeInsertHandler(" ",
DeclarativeInsertHandler2.PopupOptions.MemberLookup.INSTANCE)
);
@@ -3,28 +3,42 @@ package com.intellij.codeInsight.completion
import com.intellij.codeInsight.AutoPopupController
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import org.jetbrains.annotations.ApiStatus
@ApiStatus.Experimental
open class CompositeDeclarativeInsertHandler(val handlers: Map<Char, Lazy<DeclarativeInsertHandler2>>,
open class CompositeDeclarativeInsertHandler(val handlers: Map<String, Lazy<DeclarativeInsertHandler2>>,
val fallbackInsertHandler: InsertHandler<LookupElement>?)
: InsertHandler<LookupElement> {
init {
// check `handlers` to make sure we do not have multiple keys sharing the same completionChar
val allKeysConcatenated = handlers.keys.fold(StringBuilder()) { stringBuilder, key ->
stringBuilder.append(key)
}
val allCharsUnique = allKeysConcatenated.all(HashSet<Char>()::add)
assert(allCharsUnique)
}
override fun handleInsert(context: InsertionContext, item: LookupElement) {
(handlers[context.completionChar]?.value ?: fallbackInsertHandler)?.handleInsert(context, item)
val declarativeHandler = handlers
.filterKeys { key -> key.contains(context.completionChar) }
.values.firstOrNull()?.value
(declarativeHandler ?: fallbackInsertHandler)?.handleInsert(context, item)
}
companion object {
fun withUniversalHandler(completionChars: CharArray,
fun withUniversalHandler(completionChars: String,
handler: Lazy<DeclarativeInsertHandler2>): CompositeDeclarativeInsertHandler {
val handlersMap = completionChars.associate { it to handler }
val handlersMap = mapOf(completionChars to handler)
// it's important not to provide a fallbackInsertHandler
return CompositeDeclarativeInsertHandler(handlersMap, null)
}
fun withUniversalHandler(completionChars: CharArray,
fun withUniversalHandler(completionChars: String,
handler: DeclarativeInsertHandler2): CompositeDeclarativeInsertHandler {
val lazyHandler = lazy { handler }
return withUniversalHandler(completionChars, lazyHandler)
@@ -29,7 +29,7 @@ import org.jetbrains.kotlin.types.KotlinType
class GenerateLambdaInfo(val lambdaType: KotlinType, val explicitParameters: Boolean)
class KotlinFunctionCompositeDeclarativeInsertHandler(
handlers: Map<Char, Lazy<DeclarativeInsertHandler2>>,
handlers: Map<String, Lazy<DeclarativeInsertHandler2>>,
fallbackInsertHandler: InsertHandler<LookupElement>?,
val isLambda: Boolean,
val inputValueArguments: Boolean,
@@ -38,10 +38,10 @@ class KotlinFunctionCompositeDeclarativeInsertHandler(
companion object {
fun withUniversalHandler(
completionChars: CharArray,
completionChars: String,
handler: DeclarativeInsertHandler2.LazyBuilder
): CompositeDeclarativeInsertHandler {
val handlersMap = completionChars.associate { it to handler }
val handlersMap = mapOf(completionChars to handler)
// it's important not to provide a fallbackInsertHandler here
return KotlinFunctionCompositeDeclarativeInsertHandler(handlersMap, null, false, false, false)
}
@@ -61,12 +61,12 @@ fun createNormalFunctionInsertHandler(
assert(argumentText == "")
}
val lazyHandlers = mutableMapOf<Char, Lazy<DeclarativeInsertHandler2>>()
val lazyHandlers = mutableMapOf<String, Lazy<DeclarativeInsertHandler2>>()
val chars = editor.document.charsSequence
// \n - NormalCompletion
lazyHandlers[Lookup.NORMAL_SELECT_CHAR] = DeclarativeInsertHandler2.LazyBuilder { builder ->
lazyHandlers[Lookup.NORMAL_SELECT_CHAR.toString()] = DeclarativeInsertHandler2.LazyBuilder { builder ->
val stringToInsert = StringBuilder()
val offset = editor.caretModel.offset
@@ -90,21 +90,17 @@ fun createNormalFunctionInsertHandler(
var lambdaCaseInsideBracketOffset = 0
var noLambdaCaseInsideBracketOffset = 0
if (insertLambda) {
// todo: get file outside
val file = PsiDocumentManager.getInstance(editor.project!!).getPsiFile(editor.document)!!
if (file.kotlinCustomSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD) {
//builder.offsetToPutCaret = stringToInsert.length + 4
stringToInsert.append(" { }")
lambdaCaseInsideBracketOffset = 3
} else {
//builder.offsetToPutCaret = stringToInsert.length + 3
stringToInsert.append(" {}")
lambdaCaseInsideBracketOffset = 2
}
} else {
stringToInsert.append("($argumentText)")
noLambdaCaseInsideBracketOffset = 1
//builder.offsetToPutCaret = stringToInsert.length + 1
}
val shouldPlaceCaretInBrackets = inputValueArguments || lambdaInfo != null
if (!insertTypeArguments) {
@@ -137,8 +133,8 @@ fun createNormalFunctionInsertHandler(
builder.withPostInsertHandler(InsertHandler<LookupElement> { context, item ->
var renderedText = item.lookupString
// TODO: maybe there is a way to detect this at declarative stage
if (!argumentsOnly) {
// TODO: maybe there is a way to perform all this at declarative stage
surroundWithBracesIfInStringTemplate(context)
val name = (item.`object` as? DeclarationLookupObject)?.name
@@ -178,45 +174,8 @@ fun createNormalFunctionInsertHandler(
}
}
})
}
// \t
/*
run {
// identifier123| ***
val replaceInsertHandler = DeclarativeInsertHandler2(
insertOperations = insertOperations,
offsetToPutCaret = offsetToPutCaret,
postInsertHandler = postInsertHandler
)
var offset = context.replacementOffset
val insertLambda = lambdaInfo != null && !chars.isCharAt(offset, '(')
var insertTypeArguments = inputTypeArguments && !(insertLambda && lambdaInfo!!.explicitParameters)
val offset1 = chars.skipSpaces(offset)
if (offset1 < chars.length) {
if (chars[offset1] == '<') {
val token = context.file.findElementAt(offset1)!!
if (token.node.elementType == KtTokens.LT) {
val parent = token.parent
*/
/* if type argument list is on multiple lines this is more likely wrong parsing*//*
if (parent is KtTypeArgumentList && parent.getText().indexOf('\n') < 0) {
offset = parent.endOffset
insertTypeArguments = false
}
}
}
}
}
*/
val fallbackHandler =
KotlinFunctionInsertHandler.Normal(callType, inputTypeArguments, inputValueArguments, argumentText, lambdaInfo, argumentsOnly)