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
Please sign in to leave a comment.
You can use ReformatCodeProcessor to apply the proper formatting via code.
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
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());
}
}