How to create Surround with Null Check Live Template
I am trying to create a Surround with Null Check Live Template.
It takes a Statement like this:
<selection start>obj.getProperty()<selection end>.setValue(5);
and wraps it in a Null Check expression:
if (obj.getPropery() != null) {
obj.getProperty().setValue(5);
}
In words: I want to select an expression, extract that expression into a null check if-statement, and then leave the entire statement in the body.
Is that possible in Live Templates?
请先登录再写评论。
Check "surround" live templates section
looking at your usecase, I am not sure that is possible
That's only possible if you create a plugin that defines a new macro function, then it would look like this
if ($SELECTION$ != null) {
$STMT$
}
where $STMT$=containingStatement($SELECTION$).
It would be perhaps better to have a quickfix that does it, when you have a possible NPE warning. You could file a request or even contribute such a quick fix :)
Created an issue for this:
http://youtrack.jetbrains.net/issue/IDEA-81079