The JDT API allows other plug-ins to use the default code formatter to format source code.
The two methods to consider are: Note: The CodeFormatter class is not intended to be subclassed by clients.The factory methods on ToolFactory can be invoked to create a new instance of the default code formatter. Before invoking one of those, you need to define a map that contains the code formatter options. In order to create such a map, you can use the methods defined in the class DefaultCodeFormatterConstants like DefaultCodeFormatterConstants.getEclipseDefaultSettings()
NOTE: These predefined maps contain only the code formatter specific options. In order to invoke the code formatter, you also need to specify what kind of source the code formatter will format. In order to do so, specify the three options:
The possible values of these options are given by the constants:
If you want to modify the default maps, it is recommended that you use the methods defined on DefaultCodeFormatterConstants to create the values of the corresponding options. This is especially true for the options relative to code wrapping.
Use the newly created code formatter to format code snippets.
The default code formatter allows you to format different kind of code snippets.
These kinds are specified in the documentation of the format method.
The returned value of this method is a text edit. This text edit then needs to be applied to an IDocument instance
in order to get the formatted result.
// take default Eclipse formatting options Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); // initialize the compiler settings to be able to format 1.5 code options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5); // change the option to wrap each enum constant on a new line options.put( DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, DefaultCodeFormatterConstants.createAlignmentValue( true, DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE, DefaultCodeFormatterConstants.INDENT_ON_COLUMN)); // instantiate the default code formatter with the given options final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options); // retrieve the source to format String source = null; try { source = ...; // retrieve the source } catch (IOException e) { System.err.println("Could not retrieve the source"); //$NON-NLS-1$ e.printStackTrace(); return; } final TextEdit edit = codeFormatter.format( CodeFormatter.K_COMPILATION_UNIT, // format a compilation unit source, // source to format 0, // starting position source.length(), // length 0, // initial indentation System.getProperty("line.separator") // line separator ); IDocument document = new Document(source); try { edit.apply(document); } catch (MalformedTreeException e) { e.printStackTrace(); } catch (BadLocationException e) { e.printStackTrace(); } // display the formatted string on the System out System.out.println(document.get());
On this example,
public enum X { A,B,C,D,E,F}the result would be:
public enum X { A, B, C, D, E, F }
/** * This is just a simple example to show how javadoc comments can be formatted . * @param str The input string * @return The resulting string */should be formatted as follows:
/** * This is just a simple example to show how javadoc comments can be formatted . * * @param str * The input string * @return The resulting string */using command:
(...) final TextEdit edit = codeFormatter.format( CodeFormatter.K_JAVA_DOC, // specify the kind: javadoc source, // source to format (as per the above example) 0, // starting position source.length(), // length 0, // initial indentation System.getProperty("line.separator") // line separator ); (...)
/* * This is just an example of multi- line comment intended to demonstrate the default code formatter ability to format multi-line comments . * * These possibilities include: Formatting of javadoc comments, * formatting of multi-line comments */should be formatted as follows:
/* * This is just an example of multi- line comment intended to demonstrate * the default code formatter ability to format multi-line comments . * * These possibilities include: Formatting of javadoc comments, formatting * of multi-line comments */using command:
(...) final TextEdit edit = codeFormatter.format( CodeFormatter.K_MULTI_LINE_COMMENT, // specify the kind: multi-line comments source, // source to format (as per the above example) 0, // starting position source.length(), // length 0, // initial indentation System.getProperty("line.separator") // line separator ); (...)
// This is a long comment that should be split in multiple line comments in case the line comment formatting is enabledshould be formatted as follows:
// This is a long comment that should be split in multiple line comments in // case the line comment formatting is enabledusing command:
(...) final TextEdit edit = codeFormatter.format( CodeFormatter.K_SINGLE_LINE_COMMENT, // specify the kind: single-line comments source, // source to format (as per the above example) 0, // starting position source.length(), // length 0, // initial indentation System.getProperty("line.separator") // line separator ); (...)