forked from cjllanwarne/winstanley
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from broadinstitute/cjl_go_to_declaration
Reference resolution from values to declarations.
- Loading branch information
Showing
8 changed files
with
140 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package winstanley.psi | ||
|
||
import com.intellij.psi.PsiNameIdentifierOwner | ||
|
||
trait WdlNamedElement extends PsiNameIdentifierOwner { | ||
def declaredValueName: Option[String] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package winstanley.psi.impl | ||
|
||
import com.intellij.extapi.psi.ASTWrapperPsiElement | ||
import com.intellij.lang.ASTNode | ||
import winstanley.structure.WdlImplicits._ | ||
import winstanley.psi.WdlNamedElement | ||
|
||
abstract class WdlNamedElementImpl(astNode: ASTNode) extends ASTWrapperPsiElement(astNode) with WdlNamedElement { | ||
// The Option-ality is a little paranoid, but if the declaration is being edited it might be temporarily nameless: | ||
override def declaredValueName: Option[String] = astNode.getPsi.getIdentifierNode.map(_.getText) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package winstanley.psi.impl | ||
|
||
import com.intellij.psi.{PsiElement, PsiReference} | ||
import winstanley.psi.{WdlVariableLookup, WdlWorkflowBlock} | ||
import winstanley.references.WdlDeclarationReference | ||
import winstanley.structure.WdlImplicits._ | ||
|
||
|
||
/** | ||
* This class is used by the .bnf compiler to implement the 'methods=[...]' methods on PsiElements. | ||
* | ||
* Put all your other junk util methods somewhere else! | ||
*/ | ||
object WdlPsiImplUtil extends | ||
WdlNamedElementImplUtil with | ||
WdlVariableLookupImplUtil | ||
|
||
/** | ||
* Provides the getName, setName and getNameIdentifier methods for the WdlNamedElement subclasses (see (eg) declaration and scatter_declaration in the bnf) | ||
*/ | ||
sealed trait WdlNamedElementImplUtil { | ||
def getName(namedElement: WdlNamedElementImpl): String = namedElement.declaredValueName.orNull | ||
// TODO: Implement for "refactor/rename" functionality | ||
def setName(namedElement: WdlNamedElementImpl, newName: String): PsiElement = ??? | ||
def getNameIdentifier(namedElement: WdlNamedElementImpl): PsiElement = namedElement.getIdentifierNode.map(_.getPsi).orNull | ||
} | ||
|
||
sealed trait WdlVariableLookupImplUtil { | ||
/** | ||
* This is the method that enables the 'go to declaration' functionality for variable usages. | ||
*/ | ||
def getReferences(wdlVariableLookup: WdlVariableLookup): Array[PsiReference] = Array(WdlDeclarationReference(wdlVariableLookup)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package winstanley.references | ||
|
||
import javax.annotation.Nullable | ||
|
||
import com.intellij.openapi.util.TextRange | ||
import com.intellij.psi.{PsiElement, PsiReferenceBase} | ||
import winstanley.psi.WdlVariableLookup | ||
import winstanley.structure.WdlImplicits._ | ||
|
||
final case class WdlDeclarationReference(value: WdlVariableLookup) extends PsiReferenceBase[PsiElement](value, value.getTextRange){ | ||
|
||
/** | ||
* Returns the element which is the target of the reference !!! OR NULL IF NOT FOUND !!! | ||
* | ||
* @return the target element, or null if it was not possible to resolve the reference to a valid target. | ||
* @see PsiPolyVariantReference#multiResolve(boolean) | ||
*/ | ||
@Nullable | ||
override def resolve(): PsiElement = { | ||
value.findDeclarationsAvailableInScope.find(d => value.getIdentifierNode.exists(_.getText == d.getNameIdentifier.getText)).map(_.getNameIdentifier).orNull | ||
} | ||
|
||
/** | ||
* Returns the array of String, PsiElement and/or LookupElement | ||
* instances representing all identifiers that are visible at the location of the reference. The contents | ||
* of the returned array is used to build the lookup list for basic code completion. (The list | ||
* of visible identifiers may not be filtered by the completion prefix string - the | ||
* filtering is performed later by IDEA core.) | ||
* | ||
* @return the array of available identifiers. | ||
*/ | ||
override def getVariants: Array[AnyRef] = Array.empty[AnyRef] | ||
|
||
/** | ||
* This override is required to make reference-lookup work. | ||
* | ||
* It needs a relative range within the PsiElement 'value' to count as the reference, | ||
* which in this case is the entire 'WdlVariableLookup' element. | ||
*/ | ||
override def getRangeInElement: TextRange = new TextRange(0, value.getTextLength - 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters