Class CodeFormatter
- java.lang.Object
-
- org.eclipse.jdt.core.formatter.CodeFormatter
-
public abstract class CodeFormatter extends Object
Specification for a generic source code formatter.- Since:
- 3.0
-
-
Field Summary
Fields Modifier and Type Field Description static intF_INCLUDE_COMMENTSFlag used to include the comments during the formatting of the code snippet.static intK_CLASS_BODY_DECLARATIONSKind used to format a set of class body declarationsstatic intK_COMPILATION_UNITKind used to format a compilation unitstatic intK_EXPRESSIONKind used to format an expressionstatic intK_JAVA_DOCKind used to format a Javadoc commentstatic intK_MODULE_INFOKind used to format a module description (a module-info.java file).static intK_MULTI_LINE_COMMENTKind used to format a multi-line commentstatic intK_SINGLE_LINE_COMMENTKind used to format a single-line commentstatic intK_STATEMENTSKind used to format a set of statementsstatic intK_UNKNOWNUnknown kind
-
Constructor Summary
Constructors Constructor Description CodeFormatter()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description StringcreateIndentationString(int indentationLevel)Answers the string that corresponds to the indentation to the given indentation level or an empty string if the indentation cannot be computed.abstract TextEditformat(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator)Formatsource, and returns a text edit that correspond to the difference between the given string and the formatted string.abstract TextEditformat(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator)Formatsource, and returns a text edit that correspond to the difference between the given string and the formatted string.voidsetOptions(Map<String,String> options)Sets the formatting options for this formatter.
-
-
-
Field Detail
-
K_UNKNOWN
public static final int K_UNKNOWN
Unknown kindSince 3.6, if the corresponding comment options are set to
truethen it is also possible to format the comments on the fly by adding theF_INCLUDE_COMMENTSflag to this kind of format.- See Also:
- Constant Field Values
-
K_EXPRESSION
public static final int K_EXPRESSION
Kind used to format an expressionThis kind is not applicable to module descriptions.
Note that using this constant, the comments encountered while formatting the expression may be shifted to match the correct indentation but are not formatted.
Since 3.6, if the corresponding comment options are set to
truethen it is also possible to format the comments on the fly by adding theF_INCLUDE_COMMENTSflag to this kind of format.- See Also:
- Constant Field Values
-
K_STATEMENTS
public static final int K_STATEMENTS
Kind used to format a set of statementsThis kind is not applicable to module descriptions.
Note that using this constant, the comments encountered while formatting the statements may be shifted to match the correct indentation but are not formatted.
Since 3.6, if the corresponding comment options are set to
truethen it is also possible to format the comments on the fly by adding theF_INCLUDE_COMMENTSflag to this kind of format.- See Also:
- Constant Field Values
-
K_CLASS_BODY_DECLARATIONS
public static final int K_CLASS_BODY_DECLARATIONS
Kind used to format a set of class body declarationsThis kind is not applicable to module descriptions.
Note that using this constant, the comments encountered while formatting the body declarations may be shifted to match the correct indentation but are not formatted.
Since 3.6, if the corresponding comment options are set to
truethen it is also possible to format the comments on the fly by adding theF_INCLUDE_COMMENTSflag to this kind of format.- See Also:
- Constant Field Values
-
K_COMPILATION_UNIT
public static final int K_COMPILATION_UNIT
Kind used to format a compilation unitNote: since 3.14, if the formatted compilation unit is a module description (i.e. it's named module-info.java), the
K_MODULE_INFOkind must be used instead.Since 3.4, if the corresponding comment option is set to
truethen it is also possible to format the comments on the fly by adding theF_INCLUDE_COMMENTSflag to this kind of format.- See Also:
- Constant Field Values
-
K_SINGLE_LINE_COMMENT
public static final int K_SINGLE_LINE_COMMENT
Kind used to format a single-line comment- Since:
- 3.1
- See Also:
- Constant Field Values
-
K_MULTI_LINE_COMMENT
public static final int K_MULTI_LINE_COMMENT
Kind used to format a multi-line comment- Since:
- 3.1
- See Also:
- Constant Field Values
-
K_JAVA_DOC
public static final int K_JAVA_DOC
Kind used to format a Javadoc comment- Since:
- 3.1
- See Also:
- Constant Field Values
-
K_MODULE_INFO
public static final int K_MODULE_INFO
Kind used to format a module description (a module-info.java file).If the corresponding comment option is set to
truethen it is also possible to format the comments on the fly by adding theF_INCLUDE_COMMENTSflag to this kind of format.- Since:
- 3.14
- See Also:
- Constant Field Values
-
F_INCLUDE_COMMENTS
public static final int F_INCLUDE_COMMENTS
Flag used to include the comments during the formatting of the code snippet.This flag can be combined with the following kinds:
K_COMPILATION_UNITK_UNKNOWNK_CLASS_BODY_DECLARATIONS(since 3.6)K_EXPRESSION(since 3.6)K_STATEMENTS(since 3.6)K_MODULE_INFO(since 3.14)
Note also that it has an effect only when one or several format comments options for
javadoc,blockorlineare set totruewhile callingformat(int, String, int, int, int, String)orformat(int, String, IRegion[], int, String)methodsFor example, with the Eclipse default formatter options, the formatting of the following code snippet using
K_COMPILATION_UNIT:public class X { /** * This is just a simple example to show that comments will be formatted while processing a compilation unit only if the constant flagwill produce the following output:F_INCLUDE_COMMENTflag is set. * @param str The input string */ void foo(String str){}}public class X { /** * This is just a simple example to show that comments will be formatted while processing a compilation unit only if the constant flagAdding this flavor to the kind given while formatting the same source (e.g.F_INCLUDE_COMMENTflag is set. * * @param str The input string */ void foo(String str){ } }K_COMPILATION_UNIT|F_INCLUDE_COMMENTS) will produce the following output instead:public class X { /** * This is just a simple example to show that comments will be formatted * while processing a compilation unit only if the constant flag *F_INCLUDE_COMMENTflag is set. * * @param str * The input string */ void foo(String str){ } }Note: Although we're convinced that the formatter should always include the comments while processing a
kind of compilation unit, we have decided to add a specific flag to fix this formatter incorrect behavior. This will prevent all existing clients (e.g. 3.3 and previous versions) using theK_COMPILATION_UNITkind to be broken while formatting.- Since:
- 3.4
- See Also:
- Constant Field Values
-
-
Method Detail
-
format
public abstract TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator)
Formatsource, and returns a text edit that correspond to the difference between the given string and the formatted string.It returns null if the given string cannot be formatted.
If the offset position is matching a whitespace, the result can include whitespaces. It would be up to the caller to get rid of preceding whitespaces.
- Parameters:
kind- Use to specify the kind of the code snippet to format. It can be any of these:K_EXPRESSIONK_STATEMENTSK_CLASS_BODY_DECLARATIONSK_COMPILATION_UNITK_MODULE_INFOK_UNKNOWNK_SINGLE_LINE_COMMENTK_MULTI_LINE_COMMENTK_JAVA_DOC
K_COMPILATION_UNITand since 3.6 for other kinds unrelated to comments, theF_INCLUDE_COMMENTSflag can be used to format comments on the fly (see the flag documentation for more detailed explanation).source- the source to formatoffset- the given offset to start recording the edits (inclusive).length- the given length to stop recording the edits (exclusive).indentationLevel- the initial indentation level, used to shift left/right the entire source fragment. An initial indentation level of zero or below has no effect.lineSeparator- the line separator to use in formatted source, if set tonull, then the platform default one will be used.- Returns:
- the text edit
- Throws:
IllegalArgumentException- if offset is lower than 0, length is lower than 0 or length is greater than source length.
-
format
public abstract TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator)
Formatsource, and returns a text edit that correspond to the difference between the given string and the formatted string.It returns null if the given string cannot be formatted.
If an offset position is matching a whitespace, the result can include whitespaces. It would be up to the caller to get rid of preceding whitespaces.
No region in
regionsmust overlap with any other region inregions. Each region must be within source. There must be at least one region. Regions must be sorted by their offsets, smaller offset first.- Parameters:
kind- Use to specify the kind of the code snippet to format. It can be any of these:K_EXPRESSIONK_STATEMENTSK_CLASS_BODY_DECLARATIONSK_COMPILATION_UNITK_MODULE_INFOK_UNKNOWNK_SINGLE_LINE_COMMENTK_MULTI_LINE_COMMENTK_JAVA_DOC
K_COMPILATION_UNITand since 3.6 for other kinds unrelated to comments, theF_INCLUDE_COMMENTSflag can be used to format comments on the fly (see the flag documentation for more detailed explanation).source- the source to formatregions- a set of regions in source to formatindentationLevel- the initial indentation level, used to shift left/right the entire source fragment. An initial indentation level of zero or below has no effect.lineSeparator- the line separator to use in formatted source, if set tonull, then the platform default one will be used.- Returns:
- the text edit
- Throws:
IllegalArgumentException- if there is no region, a region overlaps with another region, or the regions are not sorted in the ascending order.- Since:
- 3.4
-
createIndentationString
public String createIndentationString(int indentationLevel)
Answers the string that corresponds to the indentation to the given indentation level or an empty string if the indentation cannot be computed.This method needs to be overridden in a subclass.
The default implementation returns an empty string.
- Parameters:
indentationLevel- the given indentation level- Returns:
- the string corresponding to the right indentation level
- Throws:
IllegalArgumentException- if the given indentation level is lower than zero- Since:
- 3.2
-
-