Make keyword lookup objects correctly comparable

- This is done to fix the problem with duplicated keyword items in
CodeWithMe plugin (see https://youtrack.jetbrains.com/issue/CWM-438)
This commit is contained in:
Roman Golyshev
2020-08-10 14:56:20 +03:00
parent 18ae665d41
commit 166b6db764
2 changed files with 17 additions and 2 deletions

View File

@@ -48,7 +48,16 @@ import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.ModifierCheckerCore
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
open class KeywordLookupObject
/**
* We want all [KeywordLookupObject]s to be equal to each other.
*
* That way, if the same keyword is completed twice, it would not be duplicated in the completion. This is not required in the regular
* completion, but can be a problem in CodeWithMe plugin (see https://youtrack.jetbrains.com/issue/CWM-438).
*/
open class KeywordLookupObject {
override fun equals(other: Any?): Boolean = this === other || javaClass == other?.javaClass
override fun hashCode(): Int = javaClass.hashCode()
}
object KeywordCompletion {
private val ALL_KEYWORDS = (KEYWORDS.types + SOFT_KEYWORDS.types)

View File

@@ -100,6 +100,11 @@ fun CharSequence.isCharAt(offset: Int, c: Char) = offset < length && this[offset
fun Document.isTextAt(offset: Int, text: String) =
offset + text.length <= textLength && getText(TextRange(offset, offset + text.length)) == text
private data class KeywordConstructLookupObject(
private val keyword: String,
private val constructToInsert: String
) : KeywordLookupObject()
fun createKeywordConstructLookupElement(
project: Project,
keyword: String,
@@ -136,7 +141,8 @@ fun createKeywordConstructLookupElement(
"..." +
(if (tailAfterCaret.contains('\n')) tailAfterCaret.replace("\n", "").trimStart() else tailAfterCaret)
return LookupElementBuilder.create(KeywordLookupObject(), keyword)
val lookupElement = KeywordConstructLookupObject(keyword, fileTextToReformat)
return LookupElementBuilder.create(lookupElement, keyword)
.bold()
.withTailText(tailText)
.withInsertHandler { insertionContext, _ ->