Call Word-Index for Global Labels and Symbols instead of iterating over the files. Removed unused file.

This commit is contained in:
Chris Hodges 2021-07-24 19:46:28 +02:00
parent d872feeec4
commit a800b13a3e
3 changed files with 21 additions and 67 deletions

View File

@ -8,6 +8,9 @@ import com.intellij.psi.PsiPolyVariantReferenceBase
import com.intellij.psi.ResolveResult
import com.intellij.psi.impl.source.resolve.ResolveCache
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.PsiSearchHelper
import com.intellij.psi.search.UsageSearchContext
import com.intellij.util.SmartList
import de.platon42.intellij.plugins.m68k.psi.M68kGlobalLabel
import de.platon42.intellij.plugins.m68k.psi.M68kLookupUtil
import de.platon42.intellij.plugins.m68k.psi.M68kSymbolDefinition
@ -19,28 +22,34 @@ class M68kGlobalLabelSymbolReference(element: M68kSymbolReference) :
companion object {
val INSTANCE = Resolver()
fun findGlobalLabels(element: M68kSymbolReference, predicate: (M68kGlobalLabel) -> Boolean): List<M68kGlobalLabel> {
return M68kLookupUtil.findAllGlobalLabels(element.project).filter(predicate)
}
fun findGlobalLabels(element: M68kSymbolReference, predicate: (M68kGlobalLabel) -> Boolean): List<M68kGlobalLabel> =
M68kLookupUtil.findAllGlobalLabels(element.project).filter(predicate)
fun findSymbolDefinitions(element: M68kSymbolReference, predicate: (M68kSymbolDefinition) -> Boolean): List<M68kSymbolDefinition> {
return M68kLookupUtil.findAllSymbolDefinitions(element.project).filter(predicate)
}
private fun getCurrentFileSearchScope(element: PsiElement): GlobalSearchScope {
return GlobalSearchScope.fileScope(element.containingFile.originalFile)
}
fun findSymbolDefinitions(element: M68kSymbolReference, predicate: (M68kSymbolDefinition) -> Boolean): List<M68kSymbolDefinition> =
M68kLookupUtil.findAllSymbolDefinitions(element.project).filter(predicate)
}
class Resolver : ResolveCache.PolyVariantResolver<M68kGlobalLabelSymbolReference> {
override fun resolve(ref: M68kGlobalLabelSymbolReference, incompleteCode: Boolean): Array<ResolveResult> {
val refName = ref.element.symbolName
val project = ref.element.project
val targets: MutableList<PsiElement> = SmartList()
PsiSearchHelper.getInstance(project).processElementsWithWord(
{ elem, offset ->
when (elem) {
is M68kGlobalLabel, is M68kSymbolDefinition -> targets.add(elem)
}
true
}, GlobalSearchScope.allScope(project),
refName, UsageSearchContext.IN_CODE, true
)
val globalLabelMatches: Array<ResolveResult> = findGlobalLabels(ref.myElement) { it.name == refName }
.map { PsiElementResolveResult(it) }
.toTypedArray()
if (globalLabelMatches.isNotEmpty()) return globalLabelMatches
return findSymbolDefinitions(ref.myElement) { it.name == refName }
return targets
.map { PsiElementResolveResult(it) }
.toTypedArray()
}

View File

@ -1,55 +0,0 @@
package de.platon42.intellij.plugins.m68k.refs
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementResolveResult
import com.intellij.psi.PsiPolyVariantReferenceBase
import com.intellij.psi.ResolveResult
import com.intellij.psi.impl.source.resolve.ResolveCache
import com.intellij.psi.search.GlobalSearchScope
import de.platon42.intellij.plugins.m68k.psi.M68kLookupUtil
import de.platon42.intellij.plugins.m68k.psi.M68kSymbolDefinition
import de.platon42.intellij.plugins.m68k.psi.M68kSymbolReference
class M68kSymbolDefinitionReference(element: M68kSymbolReference) :
PsiPolyVariantReferenceBase<M68kSymbolReference>(element, TextRange(0, element.textLength)) {
companion object {
val INSTANCE = Resolver()
fun findSymbolDefinitions(element: M68kSymbolReference, predicate: (M68kSymbolDefinition) -> Boolean): List<M68kSymbolDefinition> {
return M68kLookupUtil.findAllSymbolDefinitions(element.project).filter(predicate)
}
private fun getCurrentFileSearchScope(element: PsiElement): GlobalSearchScope {
return GlobalSearchScope.fileScope(element.containingFile.originalFile)
}
}
class Resolver : ResolveCache.PolyVariantResolver<M68kSymbolDefinitionReference> {
override fun resolve(ref: M68kSymbolDefinitionReference, incompleteCode: Boolean): Array<ResolveResult> {
val refName = ref.element.symbolName
return findSymbolDefinitions(ref.myElement) { it.name == refName }
.map { PsiElementResolveResult(it) }
.toTypedArray()
}
}
override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
return ResolveCache.getInstance(element.project)
.resolveWithCaching(this, INSTANCE, false, incompleteCode)
}
override fun resolve(): PsiElement? {
val resolveResults = multiResolve(false)
return resolveResults.singleOrNull()?.element
}
override fun getVariants(): Array<Any> {
return findSymbolDefinitions(element) { true }
.map { LookupElementBuilder.createWithIcon(it) }
.toTypedArray()
}
}

View File

@ -19,7 +19,7 @@ class M68kFindUsagesProvider : FindUsagesProvider {
M68kLexer(M68kLexerPrefs()), // FIXME Oh no! More Prefs!
TokenSet.create(M68kTypes.SYMBOLDEF, M68kTypes.GLOBAL_LABEL_DEF, M68kTypes.LOCAL_LABEL_DEF, M68kTypes.SYMBOL),
TokenSet.create(M68kTypes.COMMENT),
TokenSet.create(M68kTypes.STRINGLIT),
TokenSet.create(M68kTypes.STRINGLIT, M68kTypes.DECIMAL, M68kTypes.HEXADECIMAL, M68kTypes.OCTAL, M68kTypes.BINARY),
TokenSet.EMPTY
)
}