TypeScript object method call psi element not found

Answered

Having simple TypeScript file:

I'm creating reference contributor:

<psi.referenceContributor language="TypeScript" implementation="com.vaadin.plugin.endpoints.VaadinTypeScriptReferenceContributor"/>

As:

internal class VaadinTypeScriptReferenceContributor : PsiReferenceContributor() {
    override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {

        registrar.registerReferenceProvider(
            PlatformPatterns.psiElement(JSReferenceExpression::class.java),
            object : PsiReferenceProvider() {
                override fun getReferencesByElement(element: PsiElement, context: ProcessingContext): Array<PsiReference> {
                    println(element)
                    println(element.text)
                    return emptyArray()
                }
            }
        )
    }
}

It does find any element. What I'm doing wrong?

 

After changing pattern to PlatformPatterns.psiElement() I've got only printed:

JSLiteralExpression
‘asd’

Test project:

IntelliJ IDEA 2023.3 (Ultimate Edition)
Build #IU-233.11799.241, built on December 2, 2023

Runtime version: 17.0.9+7-b1087.7 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 15.1.1
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 8
Metal Rendering is ON
Registry:
 markdown.open.vcs.log.link=true
Non-Bundled Plugins:
 com.vaadin.intellij-plugin (1.0-SNAPSHOT)
Kotlin: 233.11799.241-IJ

My development IDE

IntelliJ IDEA 2024.3.1 Preview (Ultimate Edition)
Build #IU-243.22562.13, built on November 20, 2024

Runtime version: 21.0.5+8-b631.19 aarch64 (JCEF 122.1.9)
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Toolkit: sun.lwawt.macosx.LWCToolkit
macOS 15.1.1
Kotlin analyzer version: 2.1.20-ij243-27
GC: G1 Young Generation, G1 Concurrent GC, G1 Old Generation
Memory: 8600M
Cores: 8
Metal Rendering is ON
Registry:
 debugger.new.tool.window.layout=true
 ide.experimental.ui=true
 i18n.locale=
Non-Bundled Plugins:
 com.vaadin.intellij-plugin (1.0-SNAPSHOT)
 org.asciidoctor.intellij.asciidoc (0.43.3)
 com.github.copilot (1.5.29.7524)
Kotlin: 243.22562.13-IJ
0
5 comments

Hi Marcin,

JSReferenceExpression extends PsiExternalReferenceHost, which is a part of Symbol API.

Please try implementing com.intellij.model.psi.PsiSymbolReferenceProvider.

1

Switching to:

<psi.symbolReferenceProvider hostLanguage="TypeScript"
                             hostElementClass="com.intellij.psi.PsiReference"
                             referenceClass="com.intellij.openapi.paths.UrlReference"
                             targetClass="com.intellij.openapi.paths.UrlSymbol"
                             implementationClass="com.vaadin.plugin.endpoints.VaadinTypeScriptReferenceProvider"/>

and then

internal class VaadinTypeScriptReferenceProvider : PsiSymbolReferenceProvider {

 

Seems to work, now it finds method calls, thank you!

 

0

Thanks Karol Lewandowski , I want to implement TypeScript service call reference to annotated Java class. Using above I stuck at:

override fun getReferences(host: PsiExternalReferenceHost, hints: PsiSymbolReferenceHints): Collection<PsiSymbolReference> {
    val myAnnotation = JavaPsiFacade.getInstance(host.project).findClass(MY_ANNOTATION_CLASS, ProjectScope.getLibrariesScope(host.project)) ?: return emptyList()

    val scope = GlobalSearchScope.allScope(host.project)
    return AnnotatedElementsSearch.searchPsiClasses(myAnnotation, scope).filter { it.name == host.text }.map {
        UrlReference(it, it.textRange, it.text)
    }
}

Whatever I do I end up with:

PluginException: Range must be inside element being annotated: (96,113); but got: (370,627)

How to create proper reference?

0

Hi Marcin,

Please share the full stacktrace.

0

Please sign in to leave a comment.