How to call formatting api after inserting code?

Answered

I used Velocity to generate some code snippets that will be inserted where the cursor is.
At present I am manually typing Option + Command + L to format my code. I have also tried Robot to simulate key combinations but it is not stable.
I guess there should be relevant APIs that can be called, I hope everyone can tell me, thank you very much
--English is not my native language, the above information comes from Google Translate

0
3 comments

You can use ReformatCodeProcessor to apply the proper formatting via code.

1
Avatar
Permanently deleted user

Thank you very much for answering me,
  ReformatCodeProcessor does format the current code, but every time it is executed before the code is inserted,
I try to create a new thread and set it to sleep for 1 second, but it will report an error,
I am new to java and hope to answer it for me again, thank you

0
Avatar
Permanently deleted user

Seems to be resolved!

private void reformatCode(Project project) {
try {
Robot robot = new Robot();
robot.delay(1000);
ReformatCodeProcessor reformatCodeProcessor = new ReformatCodeProcessor(project, true);
reformatCodeProcessor.run();
} catch (Exception e) {
System.out.println(e.toString());
}
}
0

Please sign in to leave a comment.