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 f0f77f0 commit 857d5bc
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

* [X] 修复`some_scripted_trigger`可能被插件认为同时匹配`<scripted_trigger>``scope_field`的问题(以及类似问题)
* [X] 优化对嵌套的定义的支持(如,`swapped_civic`
* [X] 优化 #48 - 提示封装变量和本地化时,确保加入的提示项不超过`ide.completion.variant.limit`指定的上限
* [ ] 尝试优化插件性能

## 1.1.5
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/icu/windea/pls/core/PlatformExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,13 @@ fun createNavigationGutterIconBuilder(icon: Icon, gotoRelatedItemProvider: (PsiE
}

@Suppress("NOTHING_TO_INLINE", "UNCHECKED_CAST")
inline fun <T> Query<T>.processQuery(onlyMostRelevant: Boolean = false, consumer: Processor<in T>): Boolean {
inline fun <T> Query<T>.processQuery(onlyMostRelevant: Boolean = false, limited: Boolean = false, consumer: Processor<in T>): Boolean {
if(onlyMostRelevant && this is ParadoxQuery<*, *>) {
find()?.let { consumer.process(it as T) }
return true
}
return forEach(consumer)
val finalConsumer: Processor<in T> = if(limited) LimitedCompletionProcessor(consumer) else consumer
return forEach(finalConsumer)
}

@Suppress("NOTHING_TO_INLINE")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ inline fun <K : Any, reified T : PsiElement> StubIndexKey<K, T>.processAllElemen
key: K,
project: Project,
scope: GlobalSearchScope,
crossinline action: (T) -> Boolean
crossinline processor: (T) -> Boolean
): Boolean {
if(DumbService.isDumb(project)) return true
return StubIndex.getInstance().processElements(this, key, project, scope, T::class.java) { element ->
ProgressManager.checkCanceled()
action(element)
processor(element)
}
}

inline fun <K : Any, reified T : PsiElement> StubIndexKey<K, T>.processAllElementsByKeys(
project: Project,
scope: GlobalSearchScope,
crossinline keyPredicate: (key: K) -> Boolean = { true },
crossinline action: (key: K, element: T) -> Boolean
crossinline processor: (key: K, element: T) -> Boolean
): Boolean {
if(DumbService.isDumb(project)) return true

Expand All @@ -82,7 +82,7 @@ inline fun <K : Any, reified T : PsiElement> StubIndexKey<K, T>.processAllElemen
if(keyPredicate(key)) {
StubIndex.getInstance().processElements(this, key, project, scope, T::class.java) { element ->
ProgressManager.checkCanceled()
action(key, element)
processor(key, element)
}
}
true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package icu.windea.pls.core.codeInsight.completion

import com.intellij.openapi.util.registry.*
import com.intellij.util.*

/**
* 用于优化代码提示的性能。
*/
class LimitedCompletionProcessor<T>(
private val processor: Processor<T>
): Processor<T> {
private val limit: Int = Registry.intValue("ide.completion.variant.limit")
private var count = 0

override fun process(e: T): Boolean {
if(!processor.process(e)) return false
if(++count > limit) return false
return true
}
}
2 changes: 0 additions & 2 deletions src/main/kotlin/icu/windea/pls/core/psi/ParadoxPsiManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import icu.windea.pls.localisation.psi.*
object ParadoxPsiManager {
/**
* 判断当前位置应当是一个[ParadoxLocalisationLocale],还是一个[ParadoxLocalisationPropertyKey]。
*
* 优化代码提示时会用到此方法。
*/
fun isLocalisationLocaleLike(element: PsiElement): Boolean {
val elementType = element.elementType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class ParadoxLocalisationSearch : ExtensibleQueryFactory<ParadoxLocalisationProp

/**
* 基于本地化名字索引,根据关键字和推断的语言区域遍历所有的本地化(localisation),并按照本地化的键进行去重。
*
* 优化代码提示时会用到此方法
*
* 用于优化代码提示的性能
*/
@JvmStatic
fun processVariants(
keyword: String,
selector: ChainedParadoxSelector<ParadoxLocalisationProperty>,
processor: (ParadoxLocalisationProperty) -> Boolean
processor: Processor<ParadoxLocalisationProperty>
): Boolean {
//保证返回结果的名字的唯一性
val project = selector.project
Expand All @@ -65,7 +65,7 @@ class ParadoxLocalisationSearch : ExtensibleQueryFactory<ParadoxLocalisationProp
predicate = { element -> selector.select(element) },
getDefaultValue = { selector.defaultValue() },
resetDefaultValue = { selector.resetDefaultValue() },
processor = processor
processor = { processor.process(it) }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ class ParadoxSyncedLocalisationSearch : ExtensibleQueryFactory<ParadoxLocalisati
/**
* 基于同步本地化名字索引,根据关键字和推断的语言区域遍历所有的同步本地化(localisation_synced),并按照本地化的键进行去重。
*
* 优化代码提示时会用到此方法
* 用于优化代码提示的性能
*/
@JvmStatic
fun processVariants(
keyword: String,
selector: ChainedParadoxSelector<ParadoxLocalisationProperty>,
processor: (ParadoxLocalisationProperty) -> Boolean
processor: Processor<ParadoxLocalisationProperty>
): Boolean {
//保证返回结果的名字的唯一性
val project = selector.project
Expand All @@ -65,7 +65,7 @@ class ParadoxSyncedLocalisationSearch : ExtensibleQueryFactory<ParadoxLocalisati
keyPredicate = { key -> key.matchesKeyword(keyword) },
getDefaultValue = { selector.defaultValue() },
resetDefaultValue = { selector.resetDefaultValue() },
processor = processor
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) { localisation ->
ParadoxLocalisationSearch.processVariants(keyword, selector, LimitedCompletionProcessor { localisation ->
val name = localisation.name //=localisation.paradoxLocalisationInfo?.name
val typeFile = localisation.containingFile
val builder = ParadoxScriptExpressionLookupElementBuilder.create(localisation, name)
Expand All @@ -76,7 +76,7 @@ class ParadoxScriptLocalisationExpressionSupport : ParadoxScriptExpressionSuppor
.withTypeIcon(typeFile.icon)
result.addScriptExpressionElement(context, builder)
true
}
})
}
}

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) { localisation ->
ParadoxLocalisationSearch.processVariants(keyword, selector, LimitedCompletionProcessor { localisation ->
val name = localisation.name //=localisation.paradoxLocalisationInfo?.name
val typeFile = localisation.containingFile
val builder = ParadoxScriptExpressionLookupElementBuilder.create(localisation, name)
Expand All @@ -190,7 +190,7 @@ class ParadoxScriptInlineLocalisationExpressionSupport : ParadoxScriptExpression
.withTypeIcon(typeFile.icon)
result.addScriptExpressionElement(context, builder)
true
}
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ParadoxLocalisationNameCompletionProvider : CompletionProvider<CompletionP
.preferLocale(ParadoxLocaleHandler.getPreferredLocale())
.notSamePosition(element)
//.distinctByName() //这里selector不需要指定去重
val processor: (ParadoxLocalisationProperty) -> Boolean = processor@{
val processor = LimitedCompletionProcessor<ParadoxLocalisationProperty> {
ProgressManager.checkCanceled()
val name = it.name
val icon = it.icon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ParadoxLocalisationPropertyReferenceCompletionProvider : CompletionProvide
.contextSensitive()
.preferLocale(ParadoxLocaleHandler.getPreferredLocale())
//.distinctByName() //这里selector不需要指定去重
val processor: (ParadoxLocalisationProperty) -> Boolean = {
val processor = LimitedCompletionProcessor<ParadoxLocalisationProperty> {
ProgressManager.checkCanceled()
val name = it.name
val icon = it.icon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class ParadoxScriptedVariableCompletionProvider : CompletionProvider<CompletionP
val element = parameters.position
val project = parameters.originalFile.project
val selector = scriptedVariableSelector(project, element).contextSensitive().distinctByName()
ParadoxGlobalScriptedVariableSearch.search(selector = selector).processQuery { processScriptedVariable(it, result) }
ParadoxGlobalScriptedVariableSearch.search(selector = selector).processQuery(limited = true) { processScriptedVariable(it, result) }
}

@Suppress("SameReturnValue")
private fun processScriptedVariable(it: ParadoxScriptScriptedVariable, result: CompletionResultSet): Boolean {
ProgressManager.checkCanceled()
val name = it.name ?: return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class ParadoxScriptedVariableCompletionProvider : CompletionProvider<CompletionP
val project = file.project
val selector = scriptedVariableSelector(project, element).contextSensitive().distinctByName()
ParadoxLocalScriptedVariableSearch.search(selector).processQuery { processScriptedVariable(it, result) }
ParadoxGlobalScriptedVariableSearch.search(selector).processQuery { processScriptedVariable(it, result) }
ParadoxGlobalScriptedVariableSearch.search(selector).processQuery(limited = true) { processScriptedVariable(it, result) }
}

@Suppress("SameReturnValue")
private fun processScriptedVariable(scriptedVariable: ParadoxScriptScriptedVariable, result: CompletionResultSet): Boolean {
ProgressManager.checkCanceled()
val name = scriptedVariable.name ?: return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ class ParadoxScriptedVariableNameCompletionProvider: CompletionProvider<Completi
val file = parameters.originalFile
val project = file.project
val selector = scriptedVariableSelector(project, element).contextSensitive().notSamePosition(element).distinctByName()
ParadoxGlobalScriptedVariableSearch.search(selector).processQuery { processScriptedVariable(it, result) }
ParadoxGlobalScriptedVariableSearch.search(selector).processQuery(limited = true) { processScriptedVariable(it, result) }
}

@Suppress("SameReturnValue")
private fun processScriptedVariable(scriptedVariable: ParadoxScriptScriptedVariable, result: CompletionResultSet): Boolean {
//不自动插入后面的等号
ProgressManager.checkCanceled()
Expand Down

0 comments on commit 857d5bc

Please sign in to comment.