Skip to content

Commit

Permalink
Find Usages now includes occurrences in string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
ghik committed Oct 16, 2019
1 parent d432c4b commit 8f14e33
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand All @@ -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
}

0 comments on commit 8f14e33

Please sign in to comment.