Idea plugin cannot match JavaScript import syntax

Hello!

I want to implement JavaScript custom module parsing, such as importing A from 'A.js';

A. JS can be found anywhere in the project, and the search rule is to match the entire project. A.JS is unique within the project.

So I need to develop a plugin, but PlatformPatterns. psiElement() can match all other code, including require imports, but it cannot match the import syntax at all, so it cannot be parsed

public class AliasReferenceContributor extends PsiReferenceContributor {
    @Override
    public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
        registrar.registerReferenceProvider(
                PlatformPatterns.psiElement(),  // any
                new PsiReferenceProvider() {
                    @Override
                    public PsiReference @NotNull [] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
                        String text = element.getText();
                        System.out.println("text =>" + text);
                        if (text != null && (text.contains("import"))) {
                            System.out.println("✅match => " + text + ", class: " + element.getClass().getSimpleName());
                        }
                        return PsiReference.EMPTY_ARRAY;
                    }
                }
        );
    }
}

 

 

1

It is best to ask this on a special forum dedicated to plugin development -- https://intellij-support.jetbrains.com/hc/en-us/community/topics/200366979-IntelliJ-IDEA-Open-API-and-Plugin-Development , you may get a more extensive reply and other opinions there.

0

Hello! I'm not sure that I correctly understood your question, but I think exploring the PSI with PSI viewer can be helpful. If you want to add a reference to the text after `from`, it should be `PlatformPatterns.psiElement(ES6FromClause.class)`. Also there is `JavaScript.moduleReferenceContributor` extension point, example is here.

0

请先登录再写评论。