Skip to content

Commit

Permalink
1.1.8 优化与BUG修复
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonKnightOfBreeze committed Sep 9, 2023
1 parent 1b909fa commit 608ce79
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ParadoxParameterElement(
parent: PsiElement,
private val name: String,
val contextName: String,
val contextIcon: Icon,
val contextIcon: Icon?,
val contextKey: String,
val rangeInParent: TextRange?,
val readWriteAccess: ReadWriteAccessDetector.Access,
Expand Down
35 changes: 27 additions & 8 deletions src/main/kotlin/icu/windea/pls/lang/ParadoxModifierHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import icu.windea.pls.lang.modifier.*
import icu.windea.pls.lang.modifier.impl.*
import icu.windea.pls.model.*
import icu.windea.pls.model.constraints.*
import icu.windea.pls.model.data.*
import icu.windea.pls.script.psi.*
import icu.windea.pls.tool.localisation.*

Expand Down Expand Up @@ -51,7 +52,7 @@ object ParadoxModifierHandler {

fun resolveModifier(name: String, element: PsiElement, configGroup: CwtConfigGroup, useSupport: ParadoxModifierSupport? = null): ParadoxModifierElement? {
val modifierData = getModifierData(name, element, configGroup, useSupport)
return modifierData?.toModifierElement(element)
return modifierData?.toPsiElement(element)
}

fun completeModifier(context: ProcessingContext, result: CompletionResultSet) {
Expand All @@ -62,13 +63,6 @@ object ParadoxModifierHandler {
ParadoxModifierSupport.completeModifier(context, result, modifierNames)
}

fun getModifierData(name: String, element: PsiElement): ParadoxModifierData? {
val gameType = selectGameType(element) ?: return null
val project = element.project
val configGroup = getConfigGroups(project).get(gameType)
return getModifierData(name, element, configGroup)
}

fun getModifierData(name: String, element: PsiElement, configGroup: CwtConfigGroup, useSupport: ParadoxModifierSupport? = null): ParadoxModifierData? {
val rootFile = selectRootFile(element) ?: return null
val project = configGroup.project
Expand All @@ -84,6 +78,31 @@ object ParadoxModifierHandler {
return modifierData
}

fun getModifierData(name: String, element: PsiElement): ParadoxModifierData? {
val gameType = selectGameType(element) ?: return null
val rootFile = selectRootFile(element) ?: return null
val project = element.project
val cache = project.modifierDataCache.get(rootFile)
val configGroup = getConfigGroups(project).get(gameType)
val cacheKey = name
val modifierData = cache.getOrPut(cacheKey) {
ParadoxModifierSupport.resolveModifier(name, element, configGroup) ?: ParadoxModifierData.EMPTY
}
if(modifierData == ParadoxModifierData.EMPTY) return null
return modifierData
}

fun getModifierData(modifierElement: ParadoxModifierElement): ParadoxModifierData? {
val rootFile = selectRootFile(modifierElement) ?: return null
val project = modifierElement.project
val cache = project.modifierDataCache.get(rootFile)
val cacheKey = modifierElement.name
val modifierData = cache.getOrPut(cacheKey) {
modifierElement.toData()
}
return modifierData
}

fun getModifierNameKeys(name: String, element: PsiElement): Set<String> {
val modifierData = getModifierData(name, element) ?: return emptySet()
return modifierData.getOrPutUserData(PlsKeys.modifierNameKeys) {
Expand Down
122 changes: 42 additions & 80 deletions src/main/kotlin/icu/windea/pls/lang/ParadoxParameterHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import com.intellij.codeInsight.highlighting.*
import com.intellij.codeInsight.lookup.*
import com.intellij.openapi.editor.*
import com.intellij.openapi.progress.*
import com.intellij.openapi.project.*
import com.intellij.openapi.util.*
import com.intellij.openapi.vfs.*
import com.intellij.psi.*
import com.intellij.psi.util.*
import com.intellij.util.*
Expand All @@ -22,20 +24,13 @@ import icu.windea.pls.lang.cwt.config.*
import icu.windea.pls.lang.cwt.expression.*
import icu.windea.pls.lang.parameter.*
import icu.windea.pls.model.*
import icu.windea.pls.model.data.*
import icu.windea.pls.script.codeStyle.*
import icu.windea.pls.script.psi.*
import java.util.*

@Suppress("UNUSED_PARAMETER")
object ParadoxParameterHandler {
object Keys {
val inferredConfig = Key.create<CwtValueConfig>("paradox.parameter.inferredConfig")
val inferredContextConfigs = Key.create<List<CwtMemberConfig<*>>>("paradox.parameter.inferredContextConfigs")
val parameterCache = KeyWithDefaultValue.create<Cache<String, ParadoxParameterElement>>("paradox.parameter.cache") { CacheBuilder.newBuilder().buildCache() }
val parameterModificationTracker = Key.create<ModificationTracker>("paradox.parameter.modificationTracker")
val parameterModificationCount = Key.create<Long>("paradox.parameter.modificationCount")
}

/**
* 得到[element]对应的参数上下文信息。
*
Expand Down Expand Up @@ -166,47 +161,32 @@ object ParadoxParameterHandler {
}
}

fun getParameterData(parameterElement: ParadoxParameterElement): ParadoxParameterData? {
val rootFile = selectRootFile(parameterElement) ?: return null
val project = parameterElement.project
val cache = project.parameterDataCache.get(rootFile)
val cacheKey = parameterElement.name + "@" + parameterElement.contextKey
val parameterData = cache.getOrPut(cacheKey) {
parameterElement.toData()
}
return parameterData
}

/**
* 尝试推断得到参数对应的CWT规则。
*/
fun getInferredConfig(parameterElement: ParadoxParameterElement): CwtValueConfig? {
val cacheKey = parameterElement.contextKey + "@" + parameterElement.name
val parameterCache = selectRootFile(parameterElement.parent)?.getUserData(Keys.parameterCache) ?: return null
val cached = parameterCache.get(cacheKey)
if(cached != null) {
val modificationTracker = cached.getUserData(Keys.parameterModificationTracker)
if(modificationTracker != null) {
val modificationCount = cached.getUserData(Keys.parameterModificationCount) ?: 0
if(modificationCount == modificationTracker.modificationCount) {
val resolved = cached.getUserData(Keys.inferredConfig)
if(resolved != null) {
return resolved.takeIf { it !== CwtValueConfig.EmptyConfig }
}
}
}
}

val result = Ref.create<CwtValueConfig>()
ParadoxParameterSupport.processContext(parameterElement, true) p@{ context ->
ProgressManager.checkCanceled()
val contextInfo = ParadoxParameterSupport.getContextInfo(context) ?: return@p true
val config = getInferredConfig(parameterElement.name, contextInfo)
result.mergeValue(config) { v1, v2 -> ParadoxConfigMerger.mergeValueConfig(v1, v2) }
}
val resolved = result.get()

val ep = parameterElement.getUserData(ParadoxParameterSupport.Keys.support)
if(ep != null) {
val modificationTracker = ep.getModificationTracker(parameterElement)
if(modificationTracker != null) {
parameterElement.putUserData(Keys.inferredConfig, resolved ?: CwtValueConfig.EmptyConfig)
parameterElement.putUserData(Keys.parameterModificationTracker, modificationTracker)
parameterElement.putUserData(Keys.parameterModificationCount, modificationTracker.modificationCount)
parameterCache.put(cacheKey, parameterElement)
val parameterData = getParameterData(parameterElement) ?: return null
return parameterData.getOrPutUserData(PlsKeys.parameterInferredConfig, CwtValueConfig.EmptyConfig) {
val result = Ref.create<CwtValueConfig>()
ParadoxParameterSupport.processContext(parameterElement, true) p@{ context ->
ProgressManager.checkCanceled()
val contextInfo = ParadoxParameterSupport.getContextInfo(context) ?: return@p true
val config = getInferredConfig(parameterElement.name, contextInfo)
result.mergeValue(config) { v1, v2 -> ParadoxConfigMerger.mergeValueConfig(v1, v2) }
}
result.get()
}

return resolved
}

fun getInferredConfig(parameterName: String, parameterContextInfo: ParadoxParameterContextInfo): CwtValueConfig? {
Expand All @@ -226,43 +206,17 @@ object ParadoxParameterHandler {
* 尝试推断得到参数对应的上下文CWT规则。
*/
fun getInferredContextConfigs(parameterElement: ParadoxParameterElement): List<CwtMemberConfig<*>> {
val cacheKey = parameterElement.contextKey + "@" + parameterElement.name
val parameterCache = selectRootFile(parameterElement.parent)?.getUserData(Keys.parameterCache) ?: return emptyList()
val cached = parameterCache.get(cacheKey)
if(cached != null) {
val modificationTracker = cached.getUserData(Keys.parameterModificationTracker)
if(modificationTracker != null) {
val modificationCount = cached.getUserData(Keys.parameterModificationCount) ?: 0
if(modificationCount == modificationTracker.modificationCount) {
val resolved = cached.getUserData(Keys.inferredContextConfigs)
if(resolved != null) {
return resolved
}
}
}
}

val result = Ref.create<List<CwtMemberConfig<*>>>()
ParadoxParameterSupport.processContext(parameterElement, true) p@{ context ->
ProgressManager.checkCanceled()
val contextInfo = ParadoxParameterSupport.getContextInfo(context) ?: return@p true
val contextConfigs = getInferredContextConfigs(parameterElement.name, contextInfo).orNull()
result.mergeValue(contextConfigs) { v1, v2 -> ParadoxConfigMerger.mergeConfigs(v1, v2) }
}
val resolved = result.get().orEmpty()

val ep = parameterElement.getUserData(ParadoxParameterSupport.Keys.support)
if(ep != null) {
val modificationTracker = ep.getModificationTracker(parameterElement)
if(modificationTracker != null) {
parameterElement.putUserData(Keys.inferredContextConfigs, resolved)
parameterElement.putUserData(Keys.parameterModificationTracker, modificationTracker)
parameterElement.putUserData(Keys.parameterModificationCount, modificationTracker.modificationCount)
parameterCache.put(cacheKey, parameterElement)
val parameterData = getParameterData(parameterElement) ?: return emptyList()
return parameterData.getOrPutUserData(PlsKeys.parameterInferredContextConfigs) {
val result = Ref.create<List<CwtMemberConfig<*>>>()
ParadoxParameterSupport.processContext(parameterElement, true) p@{ context ->
ProgressManager.checkCanceled()
val contextInfo = ParadoxParameterSupport.getContextInfo(context) ?: return@p true
val contextConfigs = getInferredContextConfigs(parameterElement.name, contextInfo).orNull()
result.mergeValue(contextConfigs) { v1, v2 -> ParadoxConfigMerger.mergeConfigs(v1, v2) }
}
return result.get().orEmpty()
}

return resolved
}

fun getInferredContextConfigs(parameterName: String, parameterContextInfo: ParadoxParameterContextInfo): List<CwtMemberConfig<*>> {
Expand All @@ -272,7 +226,7 @@ object ParadoxParameterHandler {
parameterInfos.process { parameterInfo ->
ProgressManager.checkCanceled()
val contextConfigs = ParadoxParameterInferredConfigProvider.getContextConfigs(parameterInfo, parameterContextInfo).orNull()
result.mergeValue(contextConfigs) { v1,v2 -> ParadoxConfigMerger.mergeConfigs(v1, v2) }
result.mergeValue(contextConfigs) { v1, v2 -> ParadoxConfigMerger.mergeConfigs(v1, v2) }
}
return result.get().orEmpty()
}
Expand All @@ -283,4 +237,12 @@ object ParadoxParameterHandler {
else -> false
}
}
}
}

private val PlsKeys.parameterDataCache by createKey("paradox.parameterDataCache") {
NestedCache<VirtualFile, _, _, _> { CacheBuilder.newBuilder().buildCache<String, ParadoxParameterData>().trackedBy { it.modificationTracker } }
}
private val Project.parameterDataCache by PlsKeys.parameterDataCache

private val PlsKeys.parameterInferredConfig by Key.create<CwtValueConfig>("paradox.parameterInferredConfig")
private val PlsKeys.parameterInferredContextConfigs by Key.create<List<CwtMemberConfig<*>>>("paradox.parameterInferredContextConfigs")
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ object StellarisEconomicCategoryHandler {
// <economic_category>_<resource>_enum[economic_modifier_categories]_enum[economic_modifier_types] = { "AI Economy" }

fun addModifier(key: String, category: String, type: String, triggered: Boolean, useParentIcon: Boolean) {
if(key.isEmpty()) return //skip invalid keys
if(useForAiBudget || (type == "mult" && useForAiBudgetForMult)) {
modifiers.add(StellarisEconomicCategoryModifierInfo(key, null, category, type, triggered, useParentIcon))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ class ParadoxParameterValueConfigContextProvider : ParadoxConfigContextProvider

val parameterElement = configContext.parameterElement ?: return null

//unsupported -> return null
val inferredContextConfigs = ParadoxParameterHandler.getInferredContextConfigs(parameterElement)
if(inferredContextConfigs.singleOrNull() == CwtValueConfig.EmptyConfig) return null
return inferredContextConfigs
return ParadoxParameterHandler.getInferredContextConfigs(parameterElement)
}

//skip MissingExpressionInspection and TooManyExpressionInspection at root level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ enum class CwtConfigType(
return when(this) {
Type, Subtype, Enum, ComplexEnum, ValueSet -> name.substringIn('[',']')
SingleAlias -> name.substringIn('[',']')
Alias, Modifier, Trigger, Effect -> name.substringIn('[',']').substringAfter(':')
Alias, Trigger, Effect -> name.substringIn('[',']').substringAfter(':')
else -> name
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.intellij.psi.*
import icu.windea.pls.core.*
import icu.windea.pls.core.annotations.*
import icu.windea.pls.core.collections.*
import icu.windea.pls.model.*
import icu.windea.pls.model.data.*

/**
* 用于为修正提供图标的图片。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.intellij.psi.*
import icu.windea.pls.core.*
import icu.windea.pls.core.annotations.*
import icu.windea.pls.core.collections.*
import icu.windea.pls.model.*
import icu.windea.pls.model.data.*

/**
* 用于为修正提供名字和描述的本地化。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import icu.windea.pls.core.psi.*
import icu.windea.pls.lang.cwt.*
import icu.windea.pls.lang.cwt.config.*
import icu.windea.pls.model.*
import icu.windea.pls.model.data.*
import icu.windea.pls.script.psi.*

/**
Expand Down Expand Up @@ -106,6 +107,7 @@ val ParadoxModifierSupport.Keys.support by createKey<ParadoxModifierSupport>("pa
val ParadoxModifierSupport.Keys.modifierConfig by createKey<CwtModifierConfig>("paradox.modifier.support.modifierConfig")

var ParadoxModifierData.support by ParadoxModifierSupport.Keys.support
var ParadoxModifierElement.support by ParadoxModifierSupport.Keys.support
var ParadoxModifierData.modifierConfig by ParadoxModifierSupport.Keys.modifierConfig

var ParadoxModifierElement.support by ParadoxModifierSupport.Keys.support
var ParadoxModifierElement.modifierConfig by ParadoxModifierSupport.Keys.modifierConfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import icu.windea.pls.lang.cwt.config.*
import icu.windea.pls.lang.cwt.expression.*
import icu.windea.pls.lang.modifier.*
import icu.windea.pls.model.*
import icu.windea.pls.model.data.*
import icu.windea.pls.script.psi.*

class ParadoxBaseModifierIconProvider : ParadoxModifierIconProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package icu.windea.pls.lang.modifier.impl

import com.intellij.psi.*
import icu.windea.pls.lang.modifier.*
import icu.windea.pls.model.*
import icu.windea.pls.model.data.*

class ParadoxBaseModifierNameDescProvider : ParadoxModifierNameDescProvider {
override fun addModifierNameKey(modifierData: ParadoxModifierData, element: PsiElement, registry: MutableSet<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import icu.windea.pls.lang.*
import icu.windea.pls.lang.cwt.*
import icu.windea.pls.lang.cwt.config.*
import icu.windea.pls.lang.modifier.*
import icu.windea.pls.model.*
import icu.windea.pls.model.data.*
import icu.windea.pls.script.psi.*

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import icu.windea.pls.lang.cwt.config.*
import icu.windea.pls.lang.cwt.expression.*
import icu.windea.pls.lang.modifier.*
import icu.windea.pls.model.*
import icu.windea.pls.model.data.*
import icu.windea.pls.script.psi.*

/**
Expand Down Expand Up @@ -209,4 +210,5 @@ class ParadoxTemplateModifierSupport : ParadoxModifierSupport {
val ParadoxModifierSupport.Keys.templateReferences by createKey<List<ParadoxTemplateSnippetExpressionReference>>("paradox.modifier.support.templateReferences")

var ParadoxModifierData.templateReferences by ParadoxModifierSupport.Keys.templateReferences

var ParadoxModifierElement.templateReferences by ParadoxModifierSupport.Keys.templateReferences
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import icu.windea.pls.lang.cwt.*
import icu.windea.pls.lang.cwt.config.*
import icu.windea.pls.lang.modifier.*
import icu.windea.pls.model.*
import icu.windea.pls.model.data.*
import icu.windea.pls.script.psi.*

/**
Expand Down Expand Up @@ -186,6 +187,7 @@ val ParadoxModifierSupport.Keys.economicCategoryInfo by createKey<StellarisEcono
val ParadoxModifierSupport.Keys.economicCategoryModifierInfo by createKey<StellarisEconomicCategoryModifierInfo>("paradox.modifier.support.economicCategoryModifierInfo")

var ParadoxModifierData.economicCategoryInfo by ParadoxModifierSupport.Keys.economicCategoryInfo
var ParadoxModifierElement.economicCategoryInfo by ParadoxModifierSupport.Keys.economicCategoryInfo
var ParadoxModifierData.economicCategoryModifierInfo by ParadoxModifierSupport.Keys.economicCategoryModifierInfo

var ParadoxModifierElement.economicCategoryInfo by ParadoxModifierSupport.Keys.economicCategoryInfo
var ParadoxModifierElement.economicCategoryModifierInfo by ParadoxModifierSupport.Keys.economicCategoryModifierInfo
Loading

0 comments on commit 608ce79

Please sign in to comment.