How do I make a Java PsiLiteralExpression behave as a declaration for references and refactoring?
I want to link elements in a markdown file to java string literals so the string literal appears as the declaration and the markdown element as a reference.
The issue is that the java string literals can override literals by the same name from a super class so sometimes the literal will refer to a literal in another class.
Markdown elements in multiple files can refer to string literal in the same java file and elements from one markdown file can refer to string literal in several java class files.
I figured that the only way to accomplish this is via a FakePsiElement which is used as a fake declaration with both the Java string literal and Markdown referring to the fake element in their references.
I had it working without the fake psi element except without having string literal override ones in a base class. I put it into an overview diagram with all relevant points:

Q1: The question I have is how to have all elements be refactored together when they reference the same FakePsiLiteralExpression?
Q2: Do I need to provide a SearchReferences extension point and when the elementToSearch is a FakePsiLiteralExpression then provide references from markdown elements that use it?
Q3: How do I make sure that a FakePsiLiteralExpression element is treated identically to another instance which refers to the same underlying PsiLiteralExpression instance?
Q4: Is having equals() return true and hashCode() return the same value for these FakePsiLiteralExpression instances enough or do I need to have a manager to ensure that for every PsiLiteralExpression the same instance of FakePsiLiteralExpression is always used?
Please sign in to leave a comment.
Another question is about
FakePsiLiteralExpression.isPhysical()should it returnfalseor delegate to underlyingPsiLiteralExpression.isPhysical()?After playing around with this I made the following diagram to show the relationships between test classes, spec resource declaration defines which test spec file (markdown) will be used to generate the test data and the options used for each test.
In the example, java test class
MdCachedFileElementsTestdeclares test spec file to befile_element_stash_spec_test.mdand two options:find-headers,find-itemsand inherits optiontypefrom its super class.Test class
MdCachedFileElementsTest2declares test spec file also asfile_element_stash_spec_test.md, but defines its own options:find-headers,find-itemsandtypefrom its super class.In the markdown file
find-headersappears in two tests so each of these options will resolve to corresponding definition in the two test classes. Optiontypewill resolve to one test class and one in the super class of a test class. It is also possible for an option to be defined in a super class but overridden in a sub class.I would like to navigate between the Java option declaration and the markdown option use in both directions and perform rename refactoring on all Java and Markdown options (matched by name) at the same time.
Any recommendations on how to do this would be greatly appreciated. I can handle one to many condition, and it works. However, as soon as there is a many to many condition, only one set of options is renamed.
The markdown test spec looks like the following:
Made it work by providing all possible references in ReferenceSearch extension for all related items no matter which element is being searched. Rename refactoring then affects all items.