Reputation: 10290
I'm working a plugin that generates method within a class.
The problem is that the code I generate gets inserted randomly between other fields and methods. EG. I have 4 fields and my method gets inserted between the second and the third field breaking the field section in two parts.
Question: How to insert the new code after fields section?
Here is the code I'm using:
MyMembersHandlerBase extends GenerateMembersHandlerBase {
@Override
protected List<? extends GenerationInfo> generateMemberPrototypes(
PsiClass psiClass, ClassMember[] members) {
PsiMethod method1 = // method generation logic
PsiMethod method2 = // ...
return asList(
new PsiGenerationInfo(method1),
new PsiGenerationInfo(method2),
...
);
}
@Override
protected ClassMember[] getAllOriginalMembers(PsiClass psiClass) {
// ...
}
}
Update: I've found "Rearrange Code" feature in UI. Probably invoking it programmatically would solve my problem, but I haven't found so far how to do that.
Upvotes: 2
Views: 376
Reputation: 1405
The action can be called this way:
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
ActionManager actionManager = ActionManager.getInstance();
AnAction rearrangeAction = actionManager.getAction("RearrangeCode");
DataContext dataContext = DataManager.getInstance()
.getDataContext(editor.getContentComponent());
Presentation presentation = rearrangeAction.getTemplatePresentation();
rearrangeAction.actionPerformed(
AnActionEvent.createFromDataContext("", presentation, dataContext)
);
But IMHO more correct is to search AST for first method or end of declaration and insert method in right place.
Upvotes: 1