ReformatCodeProcessor with custom settings

I create a file and want it to be formatted. For example, so that there are no empty lines. How can I use ReformatCodeProcessor with custom settings? Something like that:

CodeStyleSettings settings = CodeStyle.getSettings(project);
XmlCodeStyleSettings xmlCodeStyleSettings = settings.getCustomSettings(XmlCodeStyleSettings.class);
int xml_keep_blank_lines = xmlCodeStyleSettings.XML_KEEP_BLANK_LINES;
xmlCodeStyleSettings.XML_KEEP_BLANK_LINES = 0;
ReformatCodeProcessor reformatCodeProcessor = new ReformatCodeProcessor(project, xmlFile, null, false);;
xmlCodeStyleSettings.XML_KEEP_BLANK_LINES = xml_keep_blank_lines;

but it doesn't work and looks scary.

1 comment

The right way to reformat a file is to use the following method:

CodeStyleManager.getInstance(project).reformatText(file, 0, file.getTextLength())

The method will use the current project settings. If you want to use some temporary settings for a specific operation, you can use

CodeStyle.doWithTemporarySettings(project, tempSettings, <reformat code>)

Please sign in to leave a comment.