About PsiJavaElementPattern matching interface method parameters

Answered

 

Hello
   Here is my method snippet:

I am writing a parameter matching expression, I can now match the parameters starting with @Param, but I have been unable to match the parameter c2, this is my expression psiElement(PsiParameterList.class).andNot(psiElement(PsiAnnotationParameterList.class) )

20 comments
Comment actions Permalink

Hi,

You are matching the parameters list, but want to match a single parameter, so this is the first issue in your pattern.

I suggest using PsiViewer to understand the code tree - that will help you with implementing your required pattern:

https://plugins.jetbrains.com/docs/intellij/explore-api.html#31-use-internal-mode-and-psiviewer

0
Comment actions Permalink

Hello
I use PsiParameterPattern or change the expression to PsiParameter but still can't match this parameter, I am confused, how to write

0
Comment actions Permalink

Please share your code's current state.

If it's the same as provided in the first message, then please correct it according to my answer.

0
Comment actions Permalink

Hello
  Here is the psi I got with psiview and my expression

private static PsiJavaElementPattern.Capture<PsiParameter> test(){
return PsiJavaPatterns.psiElement(PsiParameter.class).withParent(PsiJavaPatterns.psiElement(PsiParameterList.class));
}

0
Comment actions Permalink

Hello
  I have tested all kinds of elements given by psiview, but I can't locate this parameter. . .

0
Comment actions Permalink

Hi,

What element is tested against your pattern? It is not clear how you try to use this pattern.

If it is used for completion, then it will be a leaf element, and a possible issue is that you should match an element inside the parameter. Notice that in the tree under the PsiParameter you have more elements like PsiTypeElement (which has more children) and PsiIdentifier.

Try with e.g.:

private static PsiJavaElementPattern.Capture<PsiParameter> test(){
return PlatformPatterns.psiElement().inside(PsiJavaPatterns.psiElement(PsiParameter.class).withParent(PsiJavaPatterns.psiElement(PsiParameterList.class)));
}

If it doesn't help, then please provide more information and clarify as much as possible.

0
Comment actions Permalink

Hello
  Here is my code and test code

public class MybatisParamReferenceContributor extends PsiReferenceContributor {
private static final String CLAZZ_NAME = "org.apache.ibatis.type.JdbcType";
private static PsiElementPattern.Capture<PsiElement> TEST
=PlatformPatterns.psiElement().inside(PsiJavaPatterns.psiElement(PsiParameter.class).withParent(PsiJavaPatterns.psiElement(PsiParameterList.class)));
@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
registrar.registerReferenceProvider(TEST, new PsiReferenceProvider() {
@Override
public PsiReference @NotNull [] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
return new PsiReference[0];
}
});
}
}
public interface ActivityMapper {
Activity2 selectByPrimaryKey(String s8);

}

This is the psi seen by psiview

I tested it, but still can't match any element, the getReferencesByElement method will not trigger

0
Comment actions Permalink

OK, in this case, you shouldn't use the "inside()" matcher then.

Again, please be more specific about the issue. What are expected and actual behaviors?

Did you register MybatisParamReferenceContributor in the plugin.xml file?

0
Comment actions Permalink

Hello
  Thank you for your patient answer, I have configured it in the xml file, I want to query the parameters of the method, and then implement its reference

0
Comment actions Permalink

And what is the actual behavior? What is the exact error you are facing? How do you know it's not working?

0
Comment actions Permalink

I put the mouse cursor on the parameter of s8, and then in the window system environment, I hold down ctrl + left mouse button and click, it will not automatically jump to its reference address

0
Comment actions Permalink

But you are not providing any references:

return new PsiReference[0];

Why do you expect any resolving to work in this case?

0
Comment actions Permalink

Because I am doing a parameter acquisition test, and now I have not matched the s8 parameter, so the implementation in the method has not been written for the time being

0
Comment actions Permalink

Make sure that the element you are trying to provide references for is an instance of com.intellij.psi.ContributedReferenceHost. It is not possible to provide references via PsiReferenceContributor for elements not implementing ContributedReferenceHost.

0
Comment actions Permalink

The element I want to reference can be referenced, because if my parameter is modified with @Param(), my other expression can be matched. Now all the problems are that my expression has been unable to match to the parameters of this method

0
Comment actions Permalink

Please show your working reference contributor.

0
Comment actions Permalink

Hello
  This is my custom implemented language, implements ContributedReferenceHost,
  please help me fix my expression to match parameter s8

 

 

0
Comment actions Permalink

I'm sorry, but I asked you to show me your working reference contributor, but you paste me your PSI elements which are completely irrelevant. I can't help you if you are not providing me with the information I'm asking you for.

Please get back to my previous comment and carefully verify it:

Make sure that the element you are trying to provide references for is an instance of com.intellij.psi.ContributedReferenceHost. It is not possible to provide references via PsiReferenceContributor for elements not implementing ContributedReferenceHost.

It is very likely the reason.

I'm not going to continue this topic due to limited time. Consider asking for help on the Community Slack: https://plugins.jetbrains.com/slack/

0
Comment actions Permalink

Both PsiParameter and PsiIdentifier are idea's own internal psi

0

Please sign in to leave a comment.