public abstract class CodeFormatter extends Object
Modifier and Type | Field and Description |
---|---|
static int |
F_INCLUDE_COMMENTS
Flag used to include the comments during the formatting of the code
snippet.
|
static int |
K_CLASS_BODY_DECLARATIONS
Kind used to format a set of class body declarations
This kind is not applicable to module descriptions.
|
static int |
K_COMPILATION_UNIT
Kind used to format a compilation unit
Note: 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. |
static int |
K_EXPRESSION
Kind used to format an expression
This kind is not applicable to module descriptions.
|
static int |
K_JAVA_DOC
Kind used to format a Javadoc comment
|
static int |
K_MODULE_INFO
Kind used to format a module description (a module-info.java file).
|
static int |
K_MULTI_LINE_COMMENT
Kind used to format a multi-line comment
|
static int |
K_SINGLE_LINE_COMMENT
Kind used to format a single-line comment
|
static int |
K_STATEMENTS
Kind used to format a set of statements
This kind is not applicable to module descriptions.
|
static int |
K_UNKNOWN
Unknown kind
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 the F_INCLUDE_COMMENTS flag to this kind of format. |
Constructor and Description |
---|
CodeFormatter() |
Modifier and Type | Method and Description |
---|---|
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.
|
abstract TextEdit |
format(int kind,
String source,
int offset,
int length,
int indentationLevel,
String lineSeparator)
Format
source ,
and returns a text edit that correspond to the difference between the given
string and the formatted string. |
abstract TextEdit |
format(int kind,
String source,
IRegion[] regions,
int indentationLevel,
String lineSeparator)
Format
source ,
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.
|
public static final int K_UNKNOWN
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 the F_INCLUDE_COMMENTS
flag to this kind of format.
public static final int K_EXPRESSION
This 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 the F_INCLUDE_COMMENTS
flag to this kind of format.
public static final int K_STATEMENTS
This 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 the F_INCLUDE_COMMENTS
flag to this kind of format.
public static final int K_CLASS_BODY_DECLARATIONS
This 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 the F_INCLUDE_COMMENTS
flag to this kind of format.
public static final int K_COMPILATION_UNIT
Note: 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 the F_INCLUDE_COMMENTS
flag to this kind of format.
public static final int K_SINGLE_LINE_COMMENT
public static final int K_MULTI_LINE_COMMENT
public static final int K_JAVA_DOC
public static final int K_MODULE_INFO
If the corresponding comment option is set to true
then it is
also possible to format the comments on the fly by adding the
F_INCLUDE_COMMENTS
flag to this kind of format.
public static final int F_INCLUDE_COMMENTS
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
or
line
are set to true
while calling
format(int, String, int, int, int, String)
or
format(int, String, IRegion[], int, String)
methods
For 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 F_INCLUDE_COMMENT
flag is set.
* @param str The input string
*/
void foo(String str){}}
will produce the following output:
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){
}
}
Adding this flavor to the kind given while formatting the same source
(e.g. 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
the K_COMPILATION_UNIT
kind to be broken while formatting.
public abstract TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator)
source
,
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.
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, the F_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 to null
, then the platform default one will be used.IllegalArgumentException
- if offset is lower than 0, length is lower than 0 or
length is greater than source length.public abstract TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator)
source
,
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 in regions
.
Each region must be within source. There must be at least one region. Regions must be sorted
by their offsets, smaller offset first.
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, the F_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 to null
, then the platform default one will be used.IllegalArgumentException
- if there is no region, a region overlaps with another region, or the regions are not
sorted in the ascending order.public String createIndentationString(int indentationLevel)
This method needs to be overridden in a subclass.
The default implementation returns an empty string.
indentationLevel
- the given indentation levelIllegalArgumentException
- if the given indentation level is lower than zero
Copyright (c) 2000, 2017 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.