Custom language. Open file by its relative path in a code.

I need to open a file by clicking Ctrl+Left-mouse-click on its relative path in a code.

Small example:

#Include "../test/dummy.lkt

I used "Reference Contibutor"[1] section of tutorial as a starting point.

include_path ::= string {mixin="com.myplugin.psi.impl.MyNamedElementImpl" 
implements="com.myplugin.psi.impl.MyNamedElementImpl"
methods=[getName setName getNameIdentifier followIncludePath]}

Here is code for followIncludePath method body:

public static void followIncludePath(MyIncludePath pathElement) {
final FileEditorManager f = FileEditorManager.getInstance(pathElement.getProject());
final Object o = FileEditorManager.getInstance(pathElement.getProject()).getOpenFiles(); //Testing
final String relativeFilePath = "../" + pathElement.getNode().getText().replaceAll("\"", ""); // For some reason same level elements should contain `../` prefix.
final VirtualFile targetFile = pathElement.getContainingFile().getVirtualFile().findFileByRelativePath(relativeFilePath);

new OpenFileDescriptor(pathElement.getProject(), targetFile);
// f.openFile(targetFile, true); //Also doesn't work
}

However, it doesn't work and I guess I did something wrong. The method by itself is not invoked. I put him inside getName util method but there I have multiple invocations. I would be very grateful if you help me with that issue. As I mentioned I need to open a file by clicking on its relative name.

http://www.jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support/reference_contributor.html

0
3 comments
Official comment

Have you followed the sections about reference/contributor defining, 10.5-10.7 in the same tutorial? You should return "PsiManager.getInstance(project).findFile(targetFile") from your reference's "resolve" method.

Avatar
Permanently deleted user

Peter,

Thank you very much for your help. I am very appreciate that.

I did follow that tutorial part. At that moment I had all methods inside MyReference(section 10.5) empty with breakpoints. Unfortunately they are enabled but not checked as active by intellij.

MyReferenceContributor(section 10.6) is enabled in plugin.xml. During the launch debugger catches call of MyReferenceContributor#registerReferenceProviders method. However, it never catches any call of created anonymous class PsiReferenceProvider#getReferencesByElement.

And after intellij is launched and I click on #Include "path" path none of methods from MyReference are invoked.

0

I see. The tutorial is a bit misleading here. The reference contributor is for those who want to contribute references to Java strings (PsiLiteralExpressions). In your case, you have your own PsiElement, and you can return your reference directly from your element's getReference method.

0

Please sign in to leave a comment.