How to create reference to non-literal PsiElements?

Answered

Hi,
in my plugin I want to put Java methods from various classes into one file/class.

I want all fields, method calls, etc. in those methods to have a reference directly to their declarations in original class, but I'm having troubles with creating new reference to PsiElement.

PsiReferenceContributor provides references only to literal expressions, comments, xml tags and attributes, which is not enough in this case. Is there maybe some pattern, that would include some/all PsiElements?

Or is there any way to create new reference to PsiElement, so getReference() wouldn't return null, but an element that I want it to?

I also tried replacing all PsiReferenceExpressions in my newly created method with the expressions in original method, but it didn't work. I created new method with PsiElementFactory.createMethodFromText().

Could you help me? I'm out of ideas.
Richard

0
7 comments

Do I understand correctly, that you create a new "fake" class containing elements from classes A,B, ... and these "fake" elements should resolve to their original declaration in A, B, ... class? Could you please explain why you need a fake class and what the use case is? Thanks

0
Avatar
Permanently deleted user

Thank you for replying.

Yes, I want to keep the context of these methods.

I need fake class, because I'm working with methods. If they're not in class I get bunch of compile errors. This fake class is only in memory, I'm not saving it anywhere.

I want to be able to display various methods in one file, so I wouldn't have to navigate between them. I'm trying to improve effectiveness.

0
Avatar
Permanently deleted user

It's part of an assignment I'm working on. To display those methods in one file, in one view.

0

You could create a custom FileEditor that shows custom UI/"fake text" for all the relevant elements ( com.intellij.openapi.fileEditor.FileEditorProvider)

0
Avatar
Permanently deleted user

Yes, I already tried it with custom FileEditor and it's working fine.

I wanted to try it some other way. I thought that maybe I could do something similar with PSI elements, but I guess it's not so easy this way, if it's even possible.

Anyway, thank you :) 

0

I think this might be the easiest solution, glad to hear you're making progress.

0

Please sign in to leave a comment.