Unable to use reference contributor on custom language lexer token.
Hello,
I have been fiddling about with the custom language tutorial for a few days, and the tutorial is quite well made, up to the confusing JAVA-Simple language interop.
I am trying to resolve references from inside template strings
The syntax highlighting with the annotator works well, i have configured the full declaration to use the extension point (it was tricky to find how btw). It even runs the function:
override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {
println("init")
registrar.registerReferenceProvider(PlatformPatterns.psiElement(ArtusNamedElement::class.java), ReferenceProvider())
}
class ReferenceProvider : PsiReferenceProvider() {
override fun getReferencesByElement(element: PsiElement, ctx: ProcessingContext): Array<PsiReference> {
if (element is ArtusNativeFunctionDefinition) {
val refs = mutableListOf<ArtusNativeVariableReference>()
val params = element.functionParameters.functionParameterList.map { it.name_.text }
element.node.getChildren(TokenSet.create(ArtusTypes.STRING)).forEach {
val str = it.text
val elem = it
params.forEach {
var ind = str.indexOf("$" + it)
while (ind > 0) {
val i = ind + elem.startOffset - element.textOffset
println(i)
refs.add(ArtusNativeVariableReference(element, element, TextRange(i, i + it.length + 1)))
ind = str.indexOf("$" + it, ind + it.length + 1)
}
}
}
return refs.toTypedArray()
}
return PsiReference.EMPTY_ARRAY
}
}
some of the output in the console:
init
63
67
63
67
63
67
63
67
63
67
63
67
63
67
63
67
48
52
61
But inside the editor nothing shows up at all, i cannot go to declaration, or refactor as needed (the other classes are similarly implemented to the tutorial classes.
may be it is the
ArtusNativeVariableReference
i use, here is the declaration:
class ArtusNativeVariableReference(val parent: ArtusNativeFunctionDefinition, element: PsiElement, textRange: TextRange): PsiReferenceBase<PsiElement>(element, textRange) {
val name = element.text.substring(textRange.startOffset, textRange.endOffset)
override fun resolve(): PsiElement? {
return parent.functionParameters.functionParameterList.find { it.name_.text == name }?.name_
}
override fun getVariants(): Array<Any> {
return parent.functionParameters.functionParameterList
.map { LookupElementBuilder.create(it.name_).withIcon(ArtusIcons.FILE).withTypeText(it.containingFile.name) }
.toTypedArray()
}
}
but it does correctly return the values it should according to the documentation...
here is the bnf declaration of the construct, as you see getName and setName are implemented:
nativeFunctionDefinition ::= DOC? VOLATILE? 'native' '(' architectureDefinition ')' ('<' genericTypeDefinitions '>')? NAME_ '(' functionParameters ')' STRING+ {methods=[getName setName getNameElement getAbsolutePath getAvailableImports]}
plugin.xml for reference:
<idea-plugin>
<id>com.artuslang.artuslang</id>
<name>Artus Language Support</name>
<version>1.0</version>
<vendor email="support@artuslang.com" url="http://www.artuslang.com">Artus Systems</vendor>
<description><![CDATA[
Official Artus Language Support.<br>
]]></description>
<change-notes><![CDATA[
-/<br>
]]>
</change-notes>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="145.0"/>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<depends>com.intellij.modules.lang</depends>
<extensions defaultExtensionNs="com.intellij">
<fileTypeFactory implementation="com.artuslang.artuslang.ArtusFileTypeFactory"/>
<lang.commenter language="Artus" implementationClass="com.artuslang.artuslang.lang.ArtusCommenter"/>
<lang.parserDefinition language="Artus" implementationClass="com.artuslang.artuslang.lang.ArtusParserDefinition"/>
<lang.syntaxHighlighterFactory language="Artus" implementationClass="com.artuslang.artuslang.syntax.ArtusSyntaxHighlighterFactory"/>
<annotator language="Artus" implementationClass="com.artuslang.artuslang.annotator.ArtusAnnotator"/>
<colorSettingsPage implementation="com.artuslang.artuslang.syntax.settings.ArtusColorSettingPage"/>
<psi.referenceContributor language="Artus" implementation="com.artuslang.artuslang.reference.ArtusReferenceContributor"/>
<lang.refactoringSupport language="Artus" implementationClass="com.artuslang.artuslang.refactoring.ArtusRefactoringSupportProvider"/>
<!-- Add your extensions here -->
</extensions>
<actions>
<!-- Add your actions here -->
</actions>
</idea-plugin>
Any ideas what i am doing wrong?
A tutorial dedicated to fully a independent language plugin with full feature set would be most welcome on the tutorial page.
Please sign in to leave a comment.
the issue was that the position to give the reference is relative, not absolute.