[Plugin][In development] Plugin that expands Builder setters automatically for you and moves cursor to each setter

I am currently writing a plugin that I hope will make my day to day development slightly faster.

 

Background/Intention:

Without going into too much detail, the point of the plugin is to automatically expand all Builder methods and move the user's cursor to each setter as they provide values and hit "enter".

 

As with most things it's clearer with an example. Consider the following class:

public class Foo {
private final String bar1;
private final String bar2;

public Foo(Builder builder) {
this.bar1 = builder.bar1;
this.bar2 = builder.bar2;
}

public static class Builder {
String bar1;
String bar2;

public Builder() {}

public void bar1(String bar1) {
this.bar1 = bar1;
}

public void bar2(String bar2) {
this.bar2 = bar2;
}
}
}

 

If the user has a Builder variable/field, and they have wrriten "mBuilder" I want my plugin to then execute and expand into:

 

mBuilder.bar1(<first_cursor_position>)
.bar2(<second_cursor_position>);

Where <first_cursor_position> is where the user's cursor is placed immediately after the plugin has executed, and <second_cursor_position> is where the user's cursor is placed after they have provided a value for bar1 and hit enter.

I've gotten most of the way through this plugin by defining a custom intent action but I'm stuck on the cursor position stuff.

Question:

Does anyone have a pointer to some code or docs that describe how to move the user's cursor to different positions as they provide values?

 

I'm thinking something very similar to the "create method" prompt that moves you through setting a method's return value and parameter names. I unfortunately can't seem to find the code for this part of the code.

0
4 comments
Avatar
Permanently deleted user

Looks like I might be looking for Templates. Digging into this now

0

Definitely sounds like live templates. You may well not even need a plugin. Docs are https://www.jetbrains.com/help/idea/2017.1/live-templates.html.

If you do need a plugin sounds like you should find that part of the codebase...

0
Avatar
Permanently deleted user

TemplateBuilder with a plugin seems to do the job I want. Live templates *almost* handle this use case but I need it be a little more dynamic. Strictly speaking there isn't a single template. The template of what I want the code to expand out into depends on the available fields for the Builder.

0

I guess you can create a live template on the fly (at runtime). This way you can adapt how many fields will be in the live template.

0

Please sign in to leave a comment.