Class CodeFormatter
- Since:
- 3.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Flag used to include the comments during the formatting of the code snippet.static final int
Kind used to format a set of class body declarationsstatic final int
Kind used to format a compilation unitstatic final int
Kind used to format an expressionstatic final int
Kind used to format a Javadoc commentstatic final int
Kind used to format a module description (a module-info.java file).static final int
Kind used to format a multi-line commentstatic final int
Kind used to format a single-line commentstatic final int
Kind used to format a set of statementsstatic final int
Unknown kind -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreateIndentationString
(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 org.eclipse.text.edits.TextEdit
Formatsource
, and returns a text edit that correspond to the difference between the given string and the formatted string.abstract org.eclipse.text.edits.TextEdit
format
(int kind, String source, org.eclipse.jface.text.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.void
setOptions
(Map<String, String> options) Sets the formatting options for this formatter.
-
Field Details
-
K_UNKNOWN
public static final int K_UNKNOWNUnknown kindSince 3.6, if the corresponding comment options are set to
true
then it is also possible to format the comments on the fly by adding theF_INCLUDE_COMMENTS
flag to this kind of format.- See Also:
-
K_EXPRESSION
public static final int K_EXPRESSIONKind 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
true
then it is also possible to format the comments on the fly by adding theF_INCLUDE_COMMENTS
flag to this kind of format.- See Also:
-
K_STATEMENTS
public static final int K_STATEMENTSKind 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
true
then it is also possible to format the comments on the fly by adding theF_INCLUDE_COMMENTS
flag to this kind of format.- See Also:
-
K_CLASS_BODY_DECLARATIONS
public static final int K_CLASS_BODY_DECLARATIONSKind 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
true
then it is also possible to format the comments on the fly by adding theF_INCLUDE_COMMENTS
flag to this kind of format.- See Also:
-
K_COMPILATION_UNIT
public static final int K_COMPILATION_UNITKind 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_INFO
kind must be used instead.Since 3.4, if the corresponding comment option is set to
true
then it is also possible to format the comments on the fly by adding theF_INCLUDE_COMMENTS
flag to this kind of format.- See Also:
-
K_SINGLE_LINE_COMMENT
public static final int K_SINGLE_LINE_COMMENTKind used to format a single-line comment- Since:
- 3.1
- See Also:
-
K_MULTI_LINE_COMMENT
public static final int K_MULTI_LINE_COMMENTKind used to format a multi-line comment- Since:
- 3.1
- See Also:
-
K_JAVA_DOC
public static final int K_JAVA_DOCKind used to format a Javadoc comment- Since:
- 3.1
- See Also:
-
K_MODULE_INFO
public static final int K_MODULE_INFOKind used to format a module description (a module-info.java file).If the corresponding comment option is set to
true
then it is also possible to format the comments on the fly by adding theF_INCLUDE_COMMENTS
flag to this kind of format.- Since:
- 3.14
- See Also:
-
F_INCLUDE_COMMENTS
public static final int F_INCLUDE_COMMENTSFlag used to include the comments during the formatting of the code snippet.This flag can be combined with the following kinds:
K_COMPILATION_UNIT
K_UNKNOWN
K_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
,block
orline
are set totrue
while 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 flag
will produce the following output:F_INCLUDE_COMMENT
flag 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 flag
Adding this flavor to the kind given while formatting the same source (e.g.F_INCLUDE_COMMENT
flag 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_COMMENT
flag 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_UNIT
kind to be broken while formatting.- Since:
- 3.4
- See Also:
-
-
Constructor Details
-
CodeFormatter
public CodeFormatter()
-
-
Method Details
-
format
public abstract org.eclipse.text.edits.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_EXPRESSION
K_STATEMENTS
K_CLASS_BODY_DECLARATIONS
K_COMPILATION_UNIT
K_MODULE_INFO
K_UNKNOWN
K_SINGLE_LINE_COMMENT
K_MULTI_LINE_COMMENT
K_JAVA_DOC
K_COMPILATION_UNIT
and since 3.6 for other kinds unrelated to comments, theF_INCLUDE_COMMENTS
flag 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 org.eclipse.text.edits.TextEdit format(int kind, String source, org.eclipse.jface.text.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
regions
must 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_EXPRESSION
K_STATEMENTS
K_CLASS_BODY_DECLARATIONS
K_COMPILATION_UNIT
K_MODULE_INFO
K_UNKNOWN
K_SINGLE_LINE_COMMENT
K_MULTI_LINE_COMMENT
K_JAVA_DOC
K_COMPILATION_UNIT
and since 3.6 for other kinds unrelated to comments, theF_INCLUDE_COMMENTS
flag 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
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
-
setOptions
Sets the formatting options for this formatter.The default implementation ignores the options.
- Parameters:
options
- the options for the formatter- Since:
- 3.11
-