Skip to content

Commit

Permalink
1.1.6 优化 #48 - 提示封装变量和本地化时,确保加入的提示项不超过ide.completion.variant.limit指…
Browse files Browse the repository at this point in the history
…定的上限
  • Loading branch information
DragonKnightOfBreeze committed Aug 10, 2023
1 parent 857d5bc commit 89f9cdb
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 43 deletions.
27 changes: 0 additions & 27 deletions src/main/kotlin/icu/windea/pls/core/StdlibExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -355,33 +355,6 @@ fun String.toCapitalizedWords(): String {

private val keywordDelimiters = charArrayOf('.', '_')

/**
* 判断指定的关键词是否匹配当前字符串。
*/
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
fun String.matchesKeyword(keyword: String): Boolean {
if(keyword.isEmpty()) return true

//IDEA底层如何匹配关键词:
//com.intellij.codeInsight.completion.PrefixMatcher.prefixMatches(java.lang.String)

//这里如何匹配关键词:按顺序包含所有字符,忽略大小写
var index = -1
for(i in 0 until keyword.length) {
val c = keyword[i]
val index1 = (this as java.lang.String).indexOf(c.lowercaseChar().code, index + 1)
val index2 = (this as java.lang.String).indexOf(c.uppercaseChar().code, index + 1)
when {
index1 == -1 && index2 == -1 -> return false
index1 == -1 -> index = index2
index2 == -1 -> index = index1
else -> index = min(index, index2)
}
}

return true
}

fun CharSequence.indicesOf(char: Char, startIndex: Int = 0, ignoreCase: Boolean = false): List<Int> {
var indices: MutableList<Int>? = null
var lastIndex = indexOf(char, startIndex, ignoreCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import com.intellij.openapi.util.registry.*
import com.intellij.util.*

/**
* 进行代码提示时,确保加入的提示项不超过`ide.completion.variant.limit`指定的上限。
*
* 用于优化代码提示的性能。
*/
class LimitedCompletionProcessor<T>(
private val processor: Processor<T>
): Processor<T> {
private val limit: Int = Registry.intValue("ide.completion.variant.limit")
private var count = 0
@Volatile private var count = 0

override fun process(e: T): Boolean {
if(!processor.process(e)) return false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package icu.windea.pls.core.search

import com.intellij.codeInsight.completion.PrefixMatcher
import com.intellij.openapi.extensions.*
import com.intellij.psi.search.searches.*
import com.intellij.util.*
Expand Down Expand Up @@ -46,13 +47,11 @@ class ParadoxLocalisationSearch : ExtensibleQueryFactory<ParadoxLocalisationProp
}

/**
* 基于本地化名字索引,根据关键字和推断的语言区域遍历所有的本地化(localisation),并按照本地化的键进行去重。
*
* 用于优化代码提示的性能。
*/
@JvmStatic
fun processVariants(
keyword: String,
prefixMatcher: PrefixMatcher,
selector: ChainedParadoxSelector<ParadoxLocalisationProperty>,
processor: Processor<ParadoxLocalisationProperty>
): Boolean {
Expand All @@ -61,7 +60,7 @@ class ParadoxLocalisationSearch : ExtensibleQueryFactory<ParadoxLocalisationProp
val scope = selector.scope
return ParadoxLocalisationNameIndexKey.processFirstElementByKeys(
project, scope,
keyPredicate = { key -> key.matchesKeyword(keyword) },
keyPredicate = { key -> prefixMatcher.prefixMatches(key) },
predicate = { element -> selector.select(element) },
getDefaultValue = { selector.defaultValue() },
resetDefaultValue = { selector.resetDefaultValue() },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package icu.windea.pls.core.search

import com.intellij.codeInsight.completion.*
import com.intellij.openapi.extensions.*
import com.intellij.psi.search.searches.*
import com.intellij.util.*
Expand Down Expand Up @@ -46,13 +47,11 @@ class ParadoxSyncedLocalisationSearch : ExtensibleQueryFactory<ParadoxLocalisati
}

/**
* 基于同步本地化名字索引,根据关键字和推断的语言区域遍历所有的同步本地化(localisation_synced),并按照本地化的键进行去重。
*
* 用于优化代码提示的性能。
*/
@JvmStatic
fun processVariants(
keyword: String,
prefixMatcher: PrefixMatcher,
selector: ChainedParadoxSelector<ParadoxLocalisationProperty>,
processor: Processor<ParadoxLocalisationProperty>
): Boolean {
Expand All @@ -61,8 +60,8 @@ class ParadoxSyncedLocalisationSearch : ExtensibleQueryFactory<ParadoxLocalisati
val scope = selector.scope
return ParadoxSyncedLocalisationNameIndexKey.processFirstElementByKeys(
project, scope,
keyPredicate = { key -> prefixMatcher.prefixMatches(key) },
predicate = { element -> selector.select(element) },
keyPredicate = { key -> key.matchesKeyword(keyword) },
getDefaultValue = { selector.defaultValue() },
resetDefaultValue = { selector.resetDefaultValue() },
processor = { processor.process(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ParadoxScriptLocalisationExpressionSupport : ParadoxScriptExpressionSuppor
val selector = localisationSelector(project, contextElement).contextSensitive()
.preferLocale(ParadoxLocaleHandler.getPreferredLocale())
//.distinctByName() //这里selector不需要指定去重
ParadoxLocalisationSearch.processVariants(keyword, selector, LimitedCompletionProcessor { localisation ->
ParadoxLocalisationSearch.processVariants(result.prefixMatcher, selector, LimitedCompletionProcessor { localisation ->
val name = localisation.name //=localisation.paradoxLocalisationInfo?.name
val typeFile = localisation.containingFile
val builder = ParadoxScriptExpressionLookupElementBuilder.create(localisation, name)
Expand Down Expand Up @@ -121,7 +121,7 @@ class ParadoxScriptSyncedLocalisationExpressionSupport : ParadoxScriptExpression
val tailText = ParadoxConfigHandler.getScriptExpressionTailText(config)
//这里selector不需要指定去重
val selector = localisationSelector(project, contextElement).contextSensitive().preferLocale(ParadoxLocaleHandler.getPreferredLocale())
ParadoxSyncedLocalisationSearch.processVariants(keyword, selector) { syncedLocalisation ->
ParadoxSyncedLocalisationSearch.processVariants(result.prefixMatcher, selector) { syncedLocalisation ->
val name = syncedLocalisation.name //=localisation.paradoxLocalisationInfo?.name
val typeFile = syncedLocalisation.containingFile
val builder = ParadoxScriptExpressionLookupElementBuilder.create(syncedLocalisation, name)
Expand Down Expand Up @@ -180,7 +180,7 @@ class ParadoxScriptInlineLocalisationExpressionSupport : ParadoxScriptExpression
val selector = localisationSelector(project, contextElement).contextSensitive()
.preferLocale(ParadoxLocaleHandler.getPreferredLocale())
//.distinctByName() //这里selector不需要指定去重
ParadoxLocalisationSearch.processVariants(keyword, selector, LimitedCompletionProcessor { localisation ->
ParadoxLocalisationSearch.processVariants(result.prefixMatcher, selector, LimitedCompletionProcessor { localisation ->
val name = localisation.name //=localisation.paradoxLocalisationInfo?.name
val typeFile = localisation.containingFile
val builder = ParadoxScriptExpressionLookupElementBuilder.create(localisation, name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class ParadoxLocalisationNameCompletionProvider : CompletionProvider<CompletionP
true
}
when(category) {
ParadoxLocalisationCategory.Localisation -> ParadoxLocalisationSearch.processVariants(keyword, selector, processor)
ParadoxLocalisationCategory.SyncedLocalisation -> ParadoxSyncedLocalisationSearch.processVariants(keyword, selector, processor)
ParadoxLocalisationCategory.Localisation -> ParadoxLocalisationSearch.processVariants(result.prefixMatcher, selector, processor)
ParadoxLocalisationCategory.SyncedLocalisation -> ParadoxSyncedLocalisationSearch.processVariants(result.prefixMatcher, selector, processor)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class ParadoxLocalisationPropertyReferenceCompletionProvider : CompletionProvide
true
}
when(category) {
ParadoxLocalisationCategory.Localisation -> ParadoxLocalisationSearch.processVariants(keyword, selector, processor)
ParadoxLocalisationCategory.SyncedLocalisation -> ParadoxSyncedLocalisationSearch.processVariants(keyword, selector, processor)
ParadoxLocalisationCategory.Localisation -> ParadoxLocalisationSearch.processVariants(result.prefixMatcher, selector, processor)
ParadoxLocalisationCategory.SyncedLocalisation -> ParadoxSyncedLocalisationSearch.processVariants(result.prefixMatcher, selector, processor)
}
}
}

0 comments on commit 89f9cdb

Please sign in to comment.