Skip to content

Commit

Permalink
Only search within the same module when looking up symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
waleedyaseen committed Aug 29, 2024
1 parent 6866405 commit 9d94f06
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 8 additions & 4 deletions src/main/kotlin/io/runescript/plugin/ide/doc/RsDocReference.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,7 +25,8 @@ class RsDocReference(element: RsDocName) : PsiPolyVariantReferenceBase<RsDocName
override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
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()
}
Expand All @@ -40,7 +44,7 @@ class RsDocReference(element: RsDocName) : PsiPolyVariantReferenceBase<RsDocName
}

companion object {
fun resolve(project: Project, owner: RsScript?, typeName: String?, elementName: String): Array<PsiElement> {
fun resolve(project: Project, module: Module, owner: RsScript?, typeName: String?, elementName: String): Array<PsiElement> {
if (typeName == null) {
return owner
?.parameterList
Expand All @@ -61,7 +65,7 @@ class RsDocReference(element: RsDocName) : PsiPolyVariantReferenceBase<RsDocName
scriptIndexKey,
elementName,
project,
GlobalSearchScope.allScope(project),
GlobalSearchScope.moduleScope(module),
RsScript::class.java
)
return scripts.toTypedArray()
Expand All @@ -70,7 +74,7 @@ class RsDocReference(element: RsDocName) : PsiPolyVariantReferenceBase<RsDocName
RsSymbolIndex.KEY,
elementName,
project,
GlobalSearchScope.allScope(project),
GlobalSearchScope.moduleScope(module),
RsSymSymbol::class.java
)
return configs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.intellij.lang.documentation.DocumentationMarkup.DEFINITION_START
import com.intellij.lang.documentation.DocumentationSettings
import com.intellij.openapi.editor.colors.TextAttributesKey
import com.intellij.openapi.editor.richcopy.HtmlSyntaxInfoUtil
import com.intellij.openapi.module.ModuleUtil
import com.intellij.psi.*
import com.intellij.psi.util.childrenOfType
import com.intellij.psi.util.parentOfType
Expand Down Expand Up @@ -150,13 +151,14 @@ class RsDocumentationProvider : AbstractDocumentationProvider() {
}
val typeName = parts[0]
val elementName = parts[1]
val module = ModuleUtil.findModuleForPsiElement(context) ?: return null
if (typeName == "parameter") {
// TODO(Walied): Find a better way to do this
val doc = context.containingFile.findElementAt(parts[2].toInt())?.parentOfType<RsDoc>() ?: 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()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +22,8 @@ abstract class RsScopedVariableExpressionMixin : StubBasedPsiElementBase<RsScope
constructor(stub: RsScopedVariableExpressionStub?, 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 getReference(): PsiReference? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.intellij.extapi.psi.StubBasedPsiElementBase
import com.intellij.ide.projectView.PresentationData
import com.intellij.lang.ASTNode
import com.intellij.navigation.ItemPresentation
import com.intellij.openapi.module.ModuleUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.ResolveState
import com.intellij.psi.scope.PsiScopeProcessor
Expand Down Expand Up @@ -55,7 +56,8 @@ abstract class RsScriptMixin : StubBasedPsiElementBase<RsScriptStub>, 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,7 +15,8 @@ import io.runescript.plugin.lang.stubs.index.RsCommandScriptIndex
class RsCommandExpressionReference(element: RsCommandExpression) : PsiPolyVariantReferenceBase<RsCommandExpression>(element, element.nameLiteral.textRangeInParent) {

override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RsConstantReference(element: RsConstantExpression) :
}

override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
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))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<RsDynamicExpression>(element, element.nameLiteral.textRangeInParent) {
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<RsGosubExpression>(element, element.nameLiteral.textRangeInParent) {

override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
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()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,7 +15,8 @@ import io.runescript.plugin.lang.stubs.index.RsClientScriptIndex
class RsHookFragmentReference(element: RsHookFragment) : PsiPolyVariantReferenceBase<RsHookFragment>(element, element.nameLiteral.textRangeInParent) {

override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RsScopedVariableReference(element: RsScopedVariableExpression) :

override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
return scopedVarTypes
.mapNotNull { RsSymbolIndex.lookup(element.project, it, element.name!!) }
.mapNotNull { RsSymbolIndex.lookup(element, it, element.name!!) }
.map { PsiElementResolveResult(it) }
.toTypedArray()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ 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 != "_") {
if (subjectName[0] == '_') {
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(
Expand Down Expand Up @@ -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.'")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,10 +23,11 @@ class RsSymbolIndex : StringStubIndexExtension<RsSymSymbol>() {
companion object {
val KEY = StubIndexKey.createIndexKey<String, RsSymSymbol>("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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +21,8 @@ abstract class RsSymSymbolMixin : StubBasedPsiElementBase<RsSymSymbolStub>, 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)
Expand Down

0 comments on commit 9d94f06

Please sign in to comment.