From 9d94f06d26ab9207a41665c1c01b1d918208b105 Mon Sep 17 00:00:00 2001 From: "Walied K. Yassen" Date: Thu, 29 Aug 2024 21:42:48 +0300 Subject: [PATCH] Only search within the same module when looking up symbols --- CHANGELOG.md | 1 + .../io/runescript/plugin/ide/doc/RsDocReference.kt | 12 ++++++++---- .../plugin/ide/doc/RsDocumentationProvider.kt | 6 ++++-- .../RuneScriptMissingScriptSymbolInspection.kt | 2 +- .../psi/mixin/RsScopedVariableExpressionMixin.kt | 4 +++- .../plugin/lang/psi/mixin/RsScriptMixin.kt | 4 +++- .../lang/psi/refs/RsCommandExpressionReference.kt | 4 +++- .../plugin/lang/psi/refs/RsConstantReference.kt | 2 +- .../lang/psi/refs/RsDynamicExpressionReference.kt | 8 ++++++-- .../plugin/lang/psi/refs/RsGosubReference.kt | 5 ++++- .../plugin/lang/psi/refs/RsHookFragmentReference.kt | 4 +++- .../lang/psi/refs/RsScopedVariableReference.kt | 2 +- .../plugin/lang/psi/refs/RsStringLiteralReference.kt | 3 +-- .../plugin/lang/psi/type/inference/CommandHandler.kt | 6 +++--- .../psi/type/inference/RsTypeInferenceVisitor.kt | 5 ++--- .../plugin/symbollang/psi/index/RsSymbolIndex.kt | 9 ++++++--- .../plugin/symbollang/psi/mixin/RsSymSymbolMixin.kt | 4 +++- 17 files changed, 53 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3457fd0..a14a24e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Allow for clientscript/command to be looked up in "Search Everywhere". - Fixed namedobj vs obj when type checking multiple args. - Allow '<' to be escaped in string literals. +- Only search within the same module when looking up symbols. ## [1.5.1] - 2024-04-15 diff --git a/src/main/kotlin/io/runescript/plugin/ide/doc/RsDocReference.kt b/src/main/kotlin/io/runescript/plugin/ide/doc/RsDocReference.kt index b21e642..dc5832b 100644 --- a/src/main/kotlin/io/runescript/plugin/ide/doc/RsDocReference.kt +++ b/src/main/kotlin/io/runescript/plugin/ide/doc/RsDocReference.kt @@ -1,5 +1,8 @@ package io.runescript.plugin.ide.doc +import com.intellij.openapi.module.Module +import com.intellij.openapi.module.ModuleUtil +import com.intellij.openapi.module.ModuleUtilCore import com.intellij.openapi.project.Project import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElement @@ -22,7 +25,8 @@ class RsDocReference(element: RsDocName) : PsiPolyVariantReferenceBase { val typeName = (element.firstChild as? RsDocName)?.text val elementName = element.lastChild.text - return resolve(element.project, element.getContainingDoc().owner, typeName, elementName) + val module = ModuleUtil.findModuleForPsiElement(element) ?: return emptyArray() + return resolve(element.project, module, element.getContainingDoc().owner, typeName, elementName) .map { PsiElementResolveResult(it) } .toTypedArray() } @@ -40,7 +44,7 @@ class RsDocReference(element: RsDocName) : PsiPolyVariantReferenceBase { + fun resolve(project: Project, module: Module, owner: RsScript?, typeName: String?, elementName: String): Array { if (typeName == null) { return owner ?.parameterList @@ -61,7 +65,7 @@ class RsDocReference(element: RsDocName) : PsiPolyVariantReferenceBase() ?: return null val owner = doc.owner - return RsDocReference.resolve(context.project, owner, null, elementName).singleOrNull() + return RsDocReference.resolve(context.project, module, owner, null, elementName).singleOrNull() } else { - return RsDocReference.resolve(context.project, null, typeName, elementName).singleOrNull() + return RsDocReference.resolve(context.project, module, null, typeName, elementName).singleOrNull() } } } diff --git a/src/main/kotlin/io/runescript/plugin/ide/inspections/RuneScriptMissingScriptSymbolInspection.kt b/src/main/kotlin/io/runescript/plugin/ide/inspections/RuneScriptMissingScriptSymbolInspection.kt index b5d1cb8..4fdccb5 100644 --- a/src/main/kotlin/io/runescript/plugin/ide/inspections/RuneScriptMissingScriptSymbolInspection.kt +++ b/src/main/kotlin/io/runescript/plugin/ide/inspections/RuneScriptMissingScriptSymbolInspection.kt @@ -22,7 +22,7 @@ class RuneScriptMissingScriptSymbolInspection : LocalInspectionTool() { val name = o.qualifiedName val length = o.rbracket.endOffset - o.lbracket.startOffset val range = TextRange.from(o.lbracket.startOffsetInParent, length) - if (RsSymbolIndex.lookup(o.project, RsPrimitiveType.CLIENTSCRIPT, name) == null) { + if (RsSymbolIndex.lookup(o, RsPrimitiveType.CLIENTSCRIPT, name) == null) { holder.registerProblem( o, RsBundle.message("inspection.error.script.symbol.not.found", name), diff --git a/src/main/kotlin/io/runescript/plugin/lang/psi/mixin/RsScopedVariableExpressionMixin.kt b/src/main/kotlin/io/runescript/plugin/lang/psi/mixin/RsScopedVariableExpressionMixin.kt index ab2207c..a5c8b71 100644 --- a/src/main/kotlin/io/runescript/plugin/lang/psi/mixin/RsScopedVariableExpressionMixin.kt +++ b/src/main/kotlin/io/runescript/plugin/lang/psi/mixin/RsScopedVariableExpressionMixin.kt @@ -2,6 +2,7 @@ package io.runescript.plugin.lang.psi.mixin import com.intellij.extapi.psi.StubBasedPsiElementBase import com.intellij.lang.ASTNode +import com.intellij.openapi.module.ModuleUtil import com.intellij.psi.PsiElement import com.intellij.psi.PsiReference import com.intellij.psi.search.GlobalSearchScope @@ -21,7 +22,8 @@ abstract class RsScopedVariableExpressionMixin : StubBasedPsiElementBase, RsScript { } override fun getUseScope(): SearchScope { - return GlobalSearchScope.projectScope(project) + val module = ModuleUtil.findModuleForPsiElement(this) ?: return super.getUseScope() + return GlobalSearchScope.moduleScope(module) } override fun setName(name: String): PsiElement { diff --git a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsCommandExpressionReference.kt b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsCommandExpressionReference.kt index 73172b1..ef11cfe 100644 --- a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsCommandExpressionReference.kt +++ b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsCommandExpressionReference.kt @@ -1,6 +1,7 @@ package io.runescript.plugin.lang.psi.refs import com.intellij.codeInsight.lookup.LookupElement +import com.intellij.openapi.module.ModuleUtil import com.intellij.psi.PsiElement import com.intellij.psi.PsiElementResolveResult import com.intellij.psi.PsiPolyVariantReferenceBase @@ -14,7 +15,8 @@ import io.runescript.plugin.lang.stubs.index.RsCommandScriptIndex class RsCommandExpressionReference(element: RsCommandExpression) : PsiPolyVariantReferenceBase(element, element.nameLiteral.textRangeInParent) { override fun multiResolve(incompleteCode: Boolean): Array { - val elements = StubIndex.getElements(RsCommandScriptIndex.KEY, element.nameLiteral.text, element.project, GlobalSearchScope.allScope(element.project), RsScript::class.java) + val module = ModuleUtil.findModuleForPsiElement(element) ?: return emptyArray() + val elements = StubIndex.getElements(RsCommandScriptIndex.KEY, element.nameLiteral.text, element.project, GlobalSearchScope.moduleScope(module), RsScript::class.java) return elements.map { PsiElementResolveResult(it) }.toTypedArray() } diff --git a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsConstantReference.kt b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsConstantReference.kt index b3fa366..cd5d5d9 100644 --- a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsConstantReference.kt +++ b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsConstantReference.kt @@ -14,7 +14,7 @@ class RsConstantReference(element: RsConstantExpression) : } override fun multiResolve(incompleteCode: Boolean): Array { - val symbol = RsSymbolIndex.lookup(element.project, RsPrimitiveType.CONSTANT, element.name!!) + val symbol = RsSymbolIndex.lookup(element, RsPrimitiveType.CONSTANT, element.name!!) ?: return emptyArray() return arrayOf(PsiElementResolveResult(symbol)) } diff --git a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsDynamicExpressionReference.kt b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsDynamicExpressionReference.kt index 3e8b987..ceaf9ef 100644 --- a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsDynamicExpressionReference.kt +++ b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsDynamicExpressionReference.kt @@ -1,6 +1,7 @@ package io.runescript.plugin.lang.psi.refs import com.intellij.codeInsight.lookup.LookupElement +import com.intellij.openapi.module.ModuleUtil import com.intellij.psi.* import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.stubs.StubIndex @@ -13,6 +14,7 @@ import io.runescript.plugin.lang.psi.type.RsPrimitiveType import io.runescript.plugin.lang.psi.type.RsType import io.runescript.plugin.lang.psi.type.type import io.runescript.plugin.lang.stubs.index.RsCommandScriptIndex +import io.runescript.plugin.lang.stubs.index.RsProcScriptIndex import io.runescript.plugin.symbollang.psi.index.RsSymbolIndex class RsDynamicExpressionReference(element: RsDynamicExpression) : PsiPolyVariantReferenceBase(element, element.nameLiteral.textRangeInParent) { @@ -42,14 +44,16 @@ class RsDynamicExpressionReference(element: RsDynamicExpression) : PsiPolyVarian // Try to resolve the element as a config reference. val project = element.project if (type is RsPrimitiveType) { - val resolvedConfig = RsSymbolIndex.lookup(project, type, elementName) + val resolvedConfig = RsSymbolIndex.lookup(element, type, elementName) if (resolvedConfig != null) { return arrayOf(PsiElementResolveResult(resolvedConfig)) } } // Try to resolve the element as a command reference. - val searchScope = GlobalSearchScope.allScope(project) + val module = ModuleUtil.findModuleForPsiElement(element) ?: return emptyArray() + val searchScope = GlobalSearchScope.moduleScope(module) + return StubIndex.getElements(RsCommandScriptIndex.KEY, elementName, project, searchScope, RsScript::class.java) .map { PsiElementResolveResult(it) } .toTypedArray() diff --git a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsGosubReference.kt b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsGosubReference.kt index 48ecc44..ec191c7 100644 --- a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsGosubReference.kt +++ b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsGosubReference.kt @@ -1,6 +1,7 @@ package io.runescript.plugin.lang.psi.refs import com.intellij.codeInsight.lookup.LookupElement +import com.intellij.openapi.module.ModuleUtil import com.intellij.psi.PsiElement import com.intellij.psi.PsiElementResolveResult import com.intellij.psi.PsiPolyVariantReferenceBase @@ -9,12 +10,14 @@ import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.stubs.StubIndex import io.runescript.plugin.lang.psi.RsGosubExpression import io.runescript.plugin.lang.psi.RsScript +import io.runescript.plugin.lang.stubs.index.RsCommandScriptIndex import io.runescript.plugin.lang.stubs.index.RsProcScriptIndex class RsGosubReference(element: RsGosubExpression) : PsiPolyVariantReferenceBase(element, element.nameLiteral.textRangeInParent) { override fun multiResolve(incompleteCode: Boolean): Array { - val elements = StubIndex.getElements(RsProcScriptIndex.KEY, element.nameLiteral.text, element.project, GlobalSearchScope.allScope(element.project), RsScript::class.java) + val module = ModuleUtil.findModuleForPsiElement(element) ?: return emptyArray() + val elements = StubIndex.getElements(RsProcScriptIndex.KEY, element.nameLiteral.text, element.project, GlobalSearchScope.moduleScope(module), RsScript::class.java) return elements.map { PsiElementResolveResult(it) }.toTypedArray() } diff --git a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsHookFragmentReference.kt b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsHookFragmentReference.kt index 4aa16ff..c6ee5c4 100644 --- a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsHookFragmentReference.kt +++ b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsHookFragmentReference.kt @@ -1,6 +1,7 @@ package io.runescript.plugin.lang.psi.refs import com.intellij.codeInsight.lookup.LookupElement +import com.intellij.openapi.module.ModuleUtil import com.intellij.psi.PsiElement import com.intellij.psi.PsiElementResolveResult import com.intellij.psi.PsiPolyVariantReferenceBase @@ -14,7 +15,8 @@ import io.runescript.plugin.lang.stubs.index.RsClientScriptIndex class RsHookFragmentReference(element: RsHookFragment) : PsiPolyVariantReferenceBase(element, element.nameLiteral.textRangeInParent) { override fun multiResolve(incompleteCode: Boolean): Array { - val elements = StubIndex.getElements(RsClientScriptIndex.KEY, element.nameLiteral.text, element.project, GlobalSearchScope.allScope(element.project), RsScript::class.java) + val module = ModuleUtil.findModuleForPsiElement(element) ?: return emptyArray() + val elements = StubIndex.getElements(RsClientScriptIndex.KEY, element.nameLiteral.text, element.project, GlobalSearchScope.moduleScope(module), RsScript::class.java) return elements.map { PsiElementResolveResult(it) }.toTypedArray() } diff --git a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsScopedVariableReference.kt b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsScopedVariableReference.kt index dd769dd..579e101 100644 --- a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsScopedVariableReference.kt +++ b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsScopedVariableReference.kt @@ -15,7 +15,7 @@ class RsScopedVariableReference(element: RsScopedVariableExpression) : override fun multiResolve(incompleteCode: Boolean): Array { return scopedVarTypes - .mapNotNull { RsSymbolIndex.lookup(element.project, it, element.name!!) } + .mapNotNull { RsSymbolIndex.lookup(element, it, element.name!!) } .map { PsiElementResolveResult(it) } .toTypedArray() } diff --git a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsStringLiteralReference.kt b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsStringLiteralReference.kt index b7466d6..883526b 100644 --- a/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsStringLiteralReference.kt +++ b/src/main/kotlin/io/runescript/plugin/lang/psi/refs/RsStringLiteralReference.kt @@ -32,9 +32,8 @@ class RsStringLiteralReference(element: RsStringLiteralExpression) : } val elementName = element.stringLiteralContent.text - val project = element.project if (type is RsPrimitiveType) { - val resolvedConfig = RsSymbolIndex.lookup(project, type, elementName) + val resolvedConfig = RsSymbolIndex.lookup(element, type, elementName) if (resolvedConfig != null) { return arrayOf(PsiElementResolveResult(resolvedConfig)) } diff --git a/src/main/kotlin/io/runescript/plugin/lang/psi/type/inference/CommandHandler.kt b/src/main/kotlin/io/runescript/plugin/lang/psi/type/inference/CommandHandler.kt index 17700a0..2881951 100644 --- a/src/main/kotlin/io/runescript/plugin/lang/psi/type/inference/CommandHandler.kt +++ b/src/main/kotlin/io/runescript/plugin/lang/psi/type/inference/CommandHandler.kt @@ -45,7 +45,7 @@ class ParamCommandHandler(private val subjectType: RsPrimitiveType) : CommandHan arguments[1].accept(this) if (arguments[1] is RsDynamicExpression) { val parameterSymbol = - RsSymbolIndex.lookup(arguments[1].project, RsPrimitiveType.PARAM, arguments[1].text) + RsSymbolIndex.lookup(arguments[1], RsPrimitiveType.PARAM, arguments[1].text) if (parameterSymbol == null || parameterSymbol.fieldList.size < 3) { arguments[1].error("Reference to a parameter with an invalid definition.") } else { @@ -80,7 +80,7 @@ class DbFindCommandHandler(private val withCount: Boolean) : CommandHandler { val arguments = o.argumentList.expressionList var keyType: RsType? = null if (arguments[0] is RsDynamicExpression) { - val dbTableSym = RsSymbolIndex.lookup(arguments[0].project, RsPrimitiveType.DBCOLUMN, arguments[0].text) + val dbTableSym = RsSymbolIndex.lookup(arguments[0], RsPrimitiveType.DBCOLUMN, arguments[0].text) if (dbTableSym == null || dbTableSym.fieldList.size < 3) { arguments[0].error("Reference to a dbtable with an invalid definition.") } else { @@ -127,7 +127,7 @@ data object DbGetFieldCommandHandler : CommandHandler { val arguments = o.argumentList.expressionList var outputType: RsType? = null if (arguments[1] is RsDynamicExpression) { - val dbTableSym = RsSymbolIndex.lookup(arguments[1].project, RsPrimitiveType.DBCOLUMN, arguments[1].text) + val dbTableSym = RsSymbolIndex.lookup(arguments[1], RsPrimitiveType.DBCOLUMN, arguments[1].text) if (dbTableSym == null || dbTableSym.fieldList.size < 3) { arguments[1].error("Reference to a dbtable with an invalid definition.") } else { diff --git a/src/main/kotlin/io/runescript/plugin/lang/psi/type/inference/RsTypeInferenceVisitor.kt b/src/main/kotlin/io/runescript/plugin/lang/psi/type/inference/RsTypeInferenceVisitor.kt index 0641a2d..e231e6a 100644 --- a/src/main/kotlin/io/runescript/plugin/lang/psi/type/inference/RsTypeInferenceVisitor.kt +++ b/src/main/kotlin/io/runescript/plugin/lang/psi/type/inference/RsTypeInferenceVisitor.kt @@ -35,7 +35,6 @@ class RsTypeInferenceVisitor(private val myInferenceData: RsTypeInference) : RsV } val subjectExpression = o.scriptNameExpression if (triggerType.subjectType != null) { - val project = subjectExpression.project var subjectType = triggerType.subjectType var subjectName = subjectExpression.text if (subjectName != "_") { @@ -43,7 +42,7 @@ class RsTypeInferenceVisitor(private val myInferenceData: RsTypeInference) : RsV subjectName = subjectName.substring(1) subjectType = RsPrimitiveType.CATEGORY } - val reference = RsSymbolIndex.lookup(project, subjectType, subjectName) + val reference = RsSymbolIndex.lookup(subjectExpression, subjectType, subjectName) if (reference == null) { subjectExpression.error( RsBundle.message( @@ -620,7 +619,7 @@ class RsTypeInferenceVisitor(private val myInferenceData: RsTypeInference) : RsV } else -> { - val configReference = RsSymbolIndex.lookup(o.project, type, value) + val configReference = RsSymbolIndex.lookup(o, type, value) if (configReference == null) { o.error("Could not resolve constant value '${value} to a reference.'") } diff --git a/src/main/kotlin/io/runescript/plugin/symbollang/psi/index/RsSymbolIndex.kt b/src/main/kotlin/io/runescript/plugin/symbollang/psi/index/RsSymbolIndex.kt index dabf3bf..f8c1b94 100644 --- a/src/main/kotlin/io/runescript/plugin/symbollang/psi/index/RsSymbolIndex.kt +++ b/src/main/kotlin/io/runescript/plugin/symbollang/psi/index/RsSymbolIndex.kt @@ -1,6 +1,8 @@ package io.runescript.plugin.symbollang.psi.index +import com.intellij.openapi.module.ModuleUtil import com.intellij.openapi.project.Project +import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.stubs.StringStubIndexExtension @@ -21,10 +23,11 @@ class RsSymbolIndex : StringStubIndexExtension() { companion object { val KEY = StubIndexKey.createIndexKey("io.runescript.plugin.symbollang.psi.index.RsSymbolIndex") - fun lookup(project: Project, type: RsPrimitiveType, name: String): RsSymSymbol? { + fun lookup(context: PsiElement, type: RsPrimitiveType, name: String): RsSymSymbol? { val lookupType = if (type == RsPrimitiveType.NAMEDOBJ) RsPrimitiveType.OBJ else type - val scope = GlobalSearchScope.allScope(project) - val configs = StubIndex.getElements(KEY, name, project, scope, RsSymSymbol::class.java) + val module = ModuleUtil.findModuleForPsiElement(context) ?: return null + val scope = GlobalSearchScope.moduleScope(module) + val configs = StubIndex.getElements(KEY, name, context.project, scope, RsSymSymbol::class.java) return configs.singleOrNull { it.containingFile.virtualFile.isSymbolFileOfTypeLiteral(lookupType.literal) } diff --git a/src/main/kotlin/io/runescript/plugin/symbollang/psi/mixin/RsSymSymbolMixin.kt b/src/main/kotlin/io/runescript/plugin/symbollang/psi/mixin/RsSymSymbolMixin.kt index 2671883..ac46edd 100644 --- a/src/main/kotlin/io/runescript/plugin/symbollang/psi/mixin/RsSymSymbolMixin.kt +++ b/src/main/kotlin/io/runescript/plugin/symbollang/psi/mixin/RsSymSymbolMixin.kt @@ -2,6 +2,7 @@ package io.runescript.plugin.symbollang.psi.mixin import com.intellij.extapi.psi.StubBasedPsiElementBase import com.intellij.lang.ASTNode +import com.intellij.openapi.module.ModuleUtil import com.intellij.psi.PsiElement import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.search.SearchScope @@ -20,7 +21,8 @@ abstract class RsSymSymbolMixin : StubBasedPsiElementBase, RsSy constructor(stub: RsSymSymbolStub?, type: IElementType?, node: ASTNode?) : super(stub, type, node) override fun getUseScope(): SearchScope { - return GlobalSearchScope.projectScope(project) + val module = ModuleUtil.findModuleForPsiElement(this) ?: return super.getUseScope() + return GlobalSearchScope.moduleScope(module) } override fun setName(name: String) = RsSymPsiImplUtil.setName(fieldList[getNameFieldIndex()], name)