SSR - How does reference validity determined for a template variable?

Answered

Hi,

I'm investigating the inner workings of the SSR Reference filter but I got stuck a bit.

I see, or at least I assume there might be a logic that checks whether the template specified in the Reference filter matches and/or applicable to the template variable the filter is added to.

I noticed that in case of templates that are not applicable, the template will simply not match at all. (Could you please confirm?)

Can you give me some hints how the applicability of a template to a template variable/PSI node is determined? Or if there is indeed such logic under the hood?
Maybe point me to the right part of the intellij-community project so I can dig into it a bit more.

Thanks you very much in advance!

0
4 comments

> I noticed that in case of templates that are not applicable, the template will simply not match at all. (Could you please confirm?)

I am not sure I got you. May you share an example? Screenshot of the Structural replace window maybe? 

> Can you give me some hints how the applicability of a template to a template variable/PSI node is determined?

See https://github.com/JetBrains/intellij-community/blob/master/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/ui/filters/FilterPanel.java

0

Hi Konstantin,

So, I might be overthinking the problem, but what I mean is that for example in case of the method calls to deprecated methods existing template:

$Instance$.$MethodCall$($Parameter$)

its MethodCall variable is configured with a Reference filter for the deprecated methods template:

@Deprecated
$ReturnType$ $Method$($ParameterType$ $Parameter$);

which makes sense because a method call references a method definition.

But if I, in the same variable, for the method call, set a reference to another existing template that references e.g a class definition or even a for loop, that doesn't really seem to make sense, and the whole template most probably won't match with any code snippets.

So my guess is that either a specific part of the referenced template is used to determine whether that part of the template matches with a certain code snippet, or the referenced template is evaluated to something to check the matching.

 

I hope I could clarify things a little more.

0

> Can you give me some hints how the applicability of a template to a template variable/PSI node is determined? Or if there is indeed such logic under the hood?

There is no applicability logic for a template variable. You can insert any references. 

Correct me if I am wrong, but seems that the templates you are describing do not have any meaning. For example:

Setting for "$MethodCall$" the reference for "for loop" (a reference to another existing template). The whole find query transforms into

$Instance$.for ($Type$ $Variable$ : $Expression$) {
$Statement$;
}
($Parameter$)

Which will not return any result. 

May you say what you are trying to achieve? I'll try to create a find template for you. 

0

I'm writing blog posts/articles about SSR and the current one would be about the Reference filter. I'm trying to figure out in general how it works and cover different aspects of it, so that anyone could get a better understanding how it works, and be able to use the Reference filter to create more meaningful SSR templates.

The fact that the template variable is replaced with the contents of the referenced template, puts it into another perspective. I was not aware that it works that way. So, is the find query (or that format) something that would be accepted as a valid SSR template too in the editor, or is it interpreted somewhat differently under the hood?

0

Please sign in to leave a comment.