intellij-postfix-templates
intellij-postfix-templates copied to clipboard
Support some simple generic constraints
It would be great if I could do something like:
java.lang.Collection<? extends Car> -> CarUtils.shredAll($expr$);
Is something like this on the radar?
So far I do not have an important use case for a special treatment of generics. I mean, in theory your extension proposal makes fully sense. But it is also quite some effort to implement this. Do you also have a real world example where you would actually apply this feature?
@xylo Sure.
I have several methods/expressions that work on collections of Entities for example, or Collections of other things.
e.g. a method like:
Entities.mapById(Collection<T extends Entity>): Map<Integer, T>
And a postfix template:
students.mapById
would make sense, but it wouldn't be applicable to a collection of strings.
I wouldn't want .mapById
to appear on all lists and collections.
Since I would like to create a couple of postfix templates that are only applicable to collections containing elements of certain types, the postfix completions would not be applicable most of the times and pollute the completion list.
Does that make sense?
Ah, I see. Originally, I thought you wanted to use multiple of those generics in one template. But you're right, the pollution of the completion list is indeed also an unpleasant matter. And I don't have any solution for that at the moment.
I did a first look what has to be replaced for implementing this, but it looks complicated. At the moment I use a high-level function from IDEA to determine if a A is a subtype of B. This function is even string based and not working with generics at all. So there are 2 things to do to implement this:
- Extend the language for defining postfix templates so that generics can be used here.
- Replace the high-level function for checking the sub-type property.
Both will take quite some time. So I put this feature request to my list of things to be done after releasing version 2.0.
Thanks for the detailed reply!
I have raised a feature request with JetBrains for this. Maybe support in the built in postfix templates would make implementation here easier, too.