Reformat Code/Optimize Imports programmatically

Answered

Hi, I am creating a plugin where I have some string that I will eventually save into a java file. Before saving I wanted to basically reformat and optimize imports in the string (taking into account editorConfig also if enabled). I am doing it like this

val dummyFile = PsiFileFactory.getInstance(project).createFileFromText("Dummy.java", JavaFileType.INSTANCE, content)
ReformatCodeProcessor(dummyFile, false).run()
OptimizeImportsProcessor(project, dummyFile).run()
val newContent = dummyFile.text

But this isn't working for me. 

What am I doing wrong and what is the correct way of solving the use case?

I have tried using CodeStyleManager.reformat() but it doesn't take editorConfig into consideration.

0
1 comment

Please share your project, com.intellij.psi.codeStyle.CodeStyleManager should work.

0

Please sign in to leave a comment.