From 903d772e5aa2500c1a9a1aaccef4b008a7abbf85 Mon Sep 17 00:00:00 2001 From: Vadim Makarov Date: Tue, 27 Jul 2021 16:57:20 +0200 Subject: [PATCH] [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 --- .../completion/AddSpaceInsertHandler.java | 4 +- .../completion/DeclarativeInsertHandler2.kt | 26 ++++++--- .../handlers/KotlinFunctionInsertHandler.kt | 53 +++---------------- 3 files changed, 28 insertions(+), 55 deletions(-) diff --git a/platform/analysis-impl/src/com/intellij/codeInsight/completion/AddSpaceInsertHandler.java b/platform/analysis-impl/src/com/intellij/codeInsight/completion/AddSpaceInsertHandler.java index 17aa77a69a90..4be0b7f0cea4 100644 --- a/platform/analysis-impl/src/com/intellij/codeInsight/completion/AddSpaceInsertHandler.java +++ b/platform/analysis-impl/src/com/intellij/codeInsight/completion/AddSpaceInsertHandler.java @@ -18,13 +18,13 @@ public class AddSpaceInsertHandler implements InsertHandler { private final static String VALID_COMPLETION_CHARS = "\u0000\n\t\r(,.:="; public final static InsertHandler INSTANCE = CompositeDeclarativeInsertHandler.Companion - .withUniversalHandler(VALID_COMPLETION_CHARS.toCharArray(), + .withUniversalHandler(VALID_COMPLETION_CHARS, new SingleInsertionDeclarativeInsertHandler(" ", DeclarativeInsertHandler2.PopupOptions.DoNotShow.INSTANCE) ); public final static InsertHandler INSTANCE_WITH_AUTO_POPUP = CompositeDeclarativeInsertHandler.Companion - .withUniversalHandler(VALID_COMPLETION_CHARS.toCharArray(), + .withUniversalHandler(VALID_COMPLETION_CHARS, new SingleInsertionDeclarativeInsertHandler(" ", DeclarativeInsertHandler2.PopupOptions.MemberLookup.INSTANCE) ); diff --git a/platform/analysis-impl/src/com/intellij/codeInsight/completion/DeclarativeInsertHandler2.kt b/platform/analysis-impl/src/com/intellij/codeInsight/completion/DeclarativeInsertHandler2.kt index 5773374fd810..4027255b2647 100644 --- a/platform/analysis-impl/src/com/intellij/codeInsight/completion/DeclarativeInsertHandler2.kt +++ b/platform/analysis-impl/src/com/intellij/codeInsight/completion/DeclarativeInsertHandler2.kt @@ -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>, +open class CompositeDeclarativeInsertHandler(val handlers: Map>, val fallbackInsertHandler: InsertHandler?) : InsertHandler { + + 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()::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): 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) diff --git a/plugins/kotlin/completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinFunctionInsertHandler.kt b/plugins/kotlin/completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinFunctionInsertHandler.kt index 5382e551536b..d70d6367eead 100644 --- a/plugins/kotlin/completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinFunctionInsertHandler.kt +++ b/plugins/kotlin/completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinFunctionInsertHandler.kt @@ -29,7 +29,7 @@ import org.jetbrains.kotlin.types.KotlinType class GenerateLambdaInfo(val lambdaType: KotlinType, val explicitParameters: Boolean) class KotlinFunctionCompositeDeclarativeInsertHandler( - handlers: Map>, + handlers: Map>, fallbackInsertHandler: InsertHandler?, 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>() + val lazyHandlers = mutableMapOf>() 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 { 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)