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:
@@ -48,7 +48,16 @@ import org.jetbrains.kotlin.renderer.render
|
|||||||
import org.jetbrains.kotlin.resolve.ModifierCheckerCore
|
import org.jetbrains.kotlin.resolve.ModifierCheckerCore
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
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 {
|
object KeywordCompletion {
|
||||||
private val ALL_KEYWORDS = (KEYWORDS.types + SOFT_KEYWORDS.types)
|
private val ALL_KEYWORDS = (KEYWORDS.types + SOFT_KEYWORDS.types)
|
||||||
|
|||||||
+7
-1
@@ -100,6 +100,11 @@ fun CharSequence.isCharAt(offset: Int, c: Char) = offset < length && this[offset
|
|||||||
fun Document.isTextAt(offset: Int, text: String) =
|
fun Document.isTextAt(offset: Int, text: String) =
|
||||||
offset + text.length <= textLength && getText(TextRange(offset, offset + text.length)) == text
|
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(
|
fun createKeywordConstructLookupElement(
|
||||||
project: Project,
|
project: Project,
|
||||||
keyword: String,
|
keyword: String,
|
||||||
@@ -136,7 +141,8 @@ fun createKeywordConstructLookupElement(
|
|||||||
"..." +
|
"..." +
|
||||||
(if (tailAfterCaret.contains('\n')) tailAfterCaret.replace("\n", "").trimStart() else tailAfterCaret)
|
(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()
|
.bold()
|
||||||
.withTailText(tailText)
|
.withTailText(tailText)
|
||||||
.withInsertHandler { insertionContext, _ ->
|
.withInsertHandler { insertionContext, _ ->
|
||||||
|
|||||||
Reference in New Issue
Block a user