How to add statement to method with formatting
Googled a bit, followed this post: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206116659-How-to-add-statements-to-a-PsiMethod-object, asked in gitter channel, no luck yet.
So I've a pain point, every time I want to register a saga handler, I've to do 3 things: write a method, add that to my listener registration, and bind this method to constructor, so I decided to write a plugin for that.
As of now, I'm simply trying to add a statement into a method body. Which works as of now (kinda sorta)
class MySaga extends BaseSaga {
constructor() {
super();
// should have constructor all the time.
this.listen = this.listen.bind(this);
}
listen() {
}
public registerListeners() {
console.info("something");
}
}
That's now what I want to do is when I do alt+insert I want to add a statement to registerListeners method so it'll have `takeLatest(SOMETHING, this.listen)` in that method after console line.
Which I can already achieve, but it doesn't take care of formatting, it simply adds the text, and there's no formatting, it doesn't add semicolons, or anything.
which looks like this:
public registerListeners() {
console.info("something");
takeLatest($KEY$, this.$METHOD$, $END$)
}
According to few post, I shouldn't try to reformat code from write action, and idea takes care of that itself, no luck, I tried with and without using `CodeStyleManager`, but no luck. I can however use `ctrl + shift + L` and reformat, it just works, also adds semicolons as expected.
The way I'm trying is:
TypeScriptFunction listener = this.getListenerMethod(e);
ASTNode node = JSChangeUtil.createExpressionFromText(project, "takeLatest($KEY$, this.$METHOD$, $END$);");
ASTNode rbrace = listener.getLastChild().getLastChild().getNode();
if (rbrace == null) {
return;
}
listener.addBefore(node.getPsi(), rbrace.getPsi());
Did I miss something? (also the $KEY$ are supposed to act like live templates, but that'll be different thread after I get the formatting correctly.
If you want to follow the actual project which is in github:
While testing it, simply create a typescript file with my intial class on it. and try to invoke action using alt + insert menu
Please sign in to leave a comment.