From 8f14e33ce0630a837b26c98dc2c262cfefb6e6a7 Mon Sep 17 00:00:00 2001 From: ghik Date: Wed, 16 Oct 2019 18:52:54 +0200 Subject: [PATCH] Find Usages now includes occurrences in string literals --- .../navigation/HoconFindUsagesHandler.scala | 33 +++++++++---------- .../HoconReadWriteAccessDetector.scala | 4 +-- .../HoconPropertiesReferenceProvider.scala | 9 +++-- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/org/jetbrains/plugins/hocon/navigation/HoconFindUsagesHandler.scala b/src/org/jetbrains/plugins/hocon/navigation/HoconFindUsagesHandler.scala index bfe7507..1380b51 100644 --- a/src/org/jetbrains/plugins/hocon/navigation/HoconFindUsagesHandler.scala +++ b/src/org/jetbrains/plugins/hocon/navigation/HoconFindUsagesHandler.scala @@ -58,27 +58,26 @@ class HoconFindUsagesHandlerFactory extends FindUsagesHandlerFactory { class HoconFindUsagesHandler(element: PsiElement) extends FindUsagesHandler(element) { override def processElementUsages( element: PsiElement, processor: Processor[UsageInfo], options: FindUsagesOptions - ): Boolean = element match { - case hkey: HKey => ReadAction.compute { () => - val resOpt = for { - globalSearchScope <- options.searchScope.opt.collectOnly[GlobalSearchScope] - keyPath <- hkey.fullContainingPath - referentiable = !hkey.inFieldInArray - } yield { - val strPath = keyPath.map(_.stringValue) + ): Boolean = { + val res = element match { + case hkey: HKey if options.isUsages => ReadAction.compute { () => + hkey.fullContainingPath.forall { keyPath => + val strPath = keyPath.map(_.stringValue) - def onKeyFound(foundKey: HKey): Boolean = - processor.process(new UsageInfo(foundKey, 0, foundKey.getTextLength, false)) + def onKeyFound(foundKey: HKey): Boolean = + processor.process(new UsageInfo(foundKey, 0, foundKey.getTextLength, false)) - if (referentiable) - HoconPathIndex.processHKeys(strPath, hkey.getProject, globalSearchScope)(onKeyFound) - else - HoconFindUsagesHandler.localUsagesOf(hkey).forall(onKeyFound) + options.searchScope match { + case gss: GlobalSearchScope if !hkey.inFieldInArray => + HoconPathIndex.processHKeys(strPath, hkey.getProject, gss)(onKeyFound) + case _ => + HoconFindUsagesHandler.localUsagesOf(hkey).forall(onKeyFound) + } + } } - resOpt.getOrElse(true) + case _ => true } - case _ => - true + res && super.processElementUsages(element, processor, options) } } object HoconFindUsagesHandler { diff --git a/src/org/jetbrains/plugins/hocon/navigation/HoconReadWriteAccessDetector.scala b/src/org/jetbrains/plugins/hocon/navigation/HoconReadWriteAccessDetector.scala index 794d810..0f388f7 100644 --- a/src/org/jetbrains/plugins/hocon/navigation/HoconReadWriteAccessDetector.scala +++ b/src/org/jetbrains/plugins/hocon/navigation/HoconReadWriteAccessDetector.scala @@ -4,7 +4,7 @@ package navigation import com.intellij.codeInsight.highlighting.ReadWriteAccessDetector import com.intellij.codeInsight.highlighting.ReadWriteAccessDetector.Access import com.intellij.psi.{PsiElement, PsiReference} -import org.jetbrains.plugins.hocon.psi.{HFieldKey, HKey, HSubstitutionKey} +import org.jetbrains.plugins.hocon.psi.{HFieldKey, HKey} class HoconReadWriteAccessDetector extends ReadWriteAccessDetector { def isReadWriteAccessible(element: PsiElement): Boolean = element match { @@ -19,6 +19,6 @@ class HoconReadWriteAccessDetector extends ReadWriteAccessDetector { def getExpressionAccess(expression: PsiElement): Access = expression match { case _: HFieldKey => Access.Write - case _: HSubstitutionKey => Access.Read + case _ => Access.Read } } diff --git a/src/org/jetbrains/plugins/hocon/ref/HoconPropertiesReferenceProvider.scala b/src/org/jetbrains/plugins/hocon/ref/HoconPropertiesReferenceProvider.scala index 0214d02..d9b869e 100644 --- a/src/org/jetbrains/plugins/hocon/ref/HoconPropertiesReferenceProvider.scala +++ b/src/org/jetbrains/plugins/hocon/ref/HoconPropertiesReferenceProvider.scala @@ -5,7 +5,7 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.util.TextRange import com.intellij.psi._ import com.intellij.util.ProcessingContext -import org.jetbrains.plugins.hocon.psi.HoconPsiElementFactory +import org.jetbrains.plugins.hocon.psi.{HFieldKey, HoconPsiElementFactory} import org.jetbrains.plugins.hocon.semantics.{ResOpts, ToplevelCtx} import org.jetbrains.plugins.hocon.settings.HoconProjectSettings @@ -68,6 +68,7 @@ class HoconPropertyReference( def getElement: PsiElement = lit def getRangeInElement: TextRange = range + // TODO: resolve with respect to local file if literal is inside HOCON file def resolve(): PsiElement = ToplevelCtx(lit, ToplevelCtx.ApplicationResource) .occurrences(fullPath, ResOpts(reverse = true)) @@ -92,7 +93,11 @@ class HoconPropertyReference( def bindToElement(element: PsiElement): PsiElement = null - def isReferenceTo(element: PsiElement): Boolean = false + // important for ReferencesSearch and therefore Find Usages + def isReferenceTo(element: PsiElement): Boolean = element match { + case hkey: HFieldKey => hkey.fullStringPath.contains(fullPath.dropRight(reverseIndex)) + case _ => false + } def isSoft: Boolean = true }