Auto generate presence checking methods
At the moment there is a way to generate a PsiSomething getSomething() method:
some_rule ::= something
{
methods = [ something="" ]
}
I suggest adding one another type of generated method: boolean isSomething()
some_rule ::= SOME_KEYWORD? something
{
methods = [ something="" SOME_KEYWORD?="isKeyword"]
}
Which should generate following PSI:
class PsiSomeRuleImpl extends Psi{
PsiSomething getSomething() { return findNonNullChildOfType(TypeSomething.class); }
boolean isKeyword() { return findChildOfType(TypeSomeKeyword.class) != null; }
}
Hmm... I wonder, would that be very useful to you?
I suspect that maybe what you're really trying to work around is that GK might be labeling something as @NotNull (and thus uses exception-throwing findNotNull-* functions) when in truth that something can be null. Is that what's happening?
No, I am not trying to ensure findNonNullChildOfType() will actually find and item.
I am talking about the situation where there is no need to get the item itself, but only to check if it is present. It may be useful for keywords.
Imagine a grammar for parsing following file:
{ horse } { dog } { magical unicorn } { magical carp } { fish }
Grammar:
{ tokens = [
MAGICAL = 'magical'
IDENTIFIER = 'IDENTIFIER'
] }
file ::= item*
item ::= '{' MAGICAL? IDENTIFIER '}' {
methods = [ MAGICAL='' IDENTIFIER='getName' ]
}
I want to get all magical items using lambdas. Suppose I have a method
List<T> ContainerUtil.find(List<T> original, Predicate<T> filter) where
interface Predicate<T> {
boolean check(T item);
}
Then I use generated PSI in following code:
MyPsiFile file;
List<MyPsiItems> magicalItems = ContainerUtil.find(file.getItems(), {
item -> item.getMagical() != null
});
But I want it to be used in more concise way:
MyPsiFile file;
List<MyPsiItems> magicalItems = ContainerUtil.find(file.getItems(), MyPsiItem::isMagical);
Ah, I see: you just have a very specific personal preference over the style of your checking code. Well, maybe you'll get it. Anything that makes development more straightforward, right? (I'm not the developer, so I don't know.)
If it were implemented, the natural language name for such a generated method would be hasMagical, however, so I suppose you would want to rename the generated checker anyway.
If has-* method generation does not get implemented (for me, personally, simply comparing against null is fine for my needs at the moment), however, there are any number of ways that you could replicate it. (It sounds like you probably know that though, so I won't distract from your request with them.)