How to add custom reference provider for PsiField
Hi!
I'm trying to add ReferenceProvider for PsiField as follows:
com.intellij.psi.PsiReferenceRegistrar.registerReferenceProvider(new com.intellij.patterns.PsiFieldPattern(), new MyReferenceProvider());
but com.intellij.patterns.InitialPatternCondition#accepts is not even called. Can I add ReferenceProvider for PsiField?
请先登录再写评论。
Hi,
no, reference providers are not supported for PsiFields. What is the initial task you want to solve?
Thanks,
Anna
I have JPA Entity for example:
For the Entity there are specific rules for the localization of attributes. In the simple case, this messages.properties is located in the same package as the entity with the keys in the following format:
Customer.name = Name
Customer = Customer
In any case, I can write an inspection.
Sorry, I don't understand where do you need a reference. But if you want only to check some conditions than it's better to avoid registering a reference as the reference would influence navigation, find usages, completion, etc
Reference for the PsiField 'name' of PsiClass 'Customer' to property 'Customer.name' of messages.properties. I think it's better to write an inspection.
If I would create a reference, then from a property to a field, not vice versa. I guess, it's feasible to rename both simultaneously? Then a reference makes sense.
The idea was to show a warning for the entity field if it does not have a localized message, and not the other way around. I wrote the inspection and it works well.
Thanks a lot, Anna Kozlova!
I have a similar kind of issue
the company i work for has translated texts in a properties file
Unfortunately they don't use the standard java I18n but some home made stuff.
within the same directory as the properties file there is a _Messages.java file
and inside of this _Messages file are multiple static fields of a specific object
and each field has the same exact name as the keys of the properties file.
I really get crazy to do a lot of manual stuff that could be done directly in a smart IDE.
For example if i need to rename one _Messages field, i then need to rename the properties key manually :/
I want to add the PsiElement that correspond to the the properties key as a reference to the PsiField
So that wheneven I use the rename fonction, that the IDE knows that it should also rename the key inside of the properties file ;)
I was also going to register a contributor that will only listen to fields of a dedicated type from class with the name _Message . But the comment Anna Kozlova left seems to say that I should do it on the properties side.
Of course the best solution would be that the IDE don't care if i rename from the properties file or from some java file, that it automatically rename all references.
Is there a way to do that with a custom PsiReferenceContributor ?
Hi Alain,
sorry for the late response.
If you would register reference in the property file, then you'll be able to search the field and find it in the properties file and thus rename it there. But in order to navigate to property, you'll need to do find usages as well. Is it ok?
If so, you'll need to register a reference contributor and reference searcher.
Anna