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 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 declarations
    static final int
    Kind used to format a compilation unit
    static final int
    Kind used to format an expression
    static final int
    Kind used to format a Javadoc comment
    static final int
    Kind used to format a module description (a module-info.java file).
    static final int
    Kind used to format a multi-line comment
    static final int
    Kind used to format a single-line comment
    static final int
    Kind used to format a set of statements
    static final int
    Unknown kind
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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 org.eclipse.text.edits.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 org.eclipse.text.edits.TextEdit
    format(int kind, String source, org.eclipse.jface.text.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
    Sets the formatting options for this formatter.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • K_UNKNOWN

      public static final 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.

      See Also:
    • K_EXPRESSION

      public static final int K_EXPRESSION
      Kind used to format an 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.

      See Also:
    • K_STATEMENTS

      public static final int K_STATEMENTS
      Kind used to format a set of 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.

      See Also:
    • K_CLASS_BODY_DECLARATIONS

      public static final int K_CLASS_BODY_DECLARATIONS
      Kind used to format a set of 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.

      See Also:
    • K_COMPILATION_UNIT

      public static final 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.

      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.

      See Also:
    • 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:
    • 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:
    • K_JAVA_DOC

      public static final int K_JAVA_DOC
      Kind used to format a Javadoc comment
      Since:
      3.1
      See Also:
    • 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 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.

      Since:
      3.14
      See Also:
    • 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:

      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.

      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)
      Format 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.

      Parameters:
      kind - Use to specify the kind of the code snippet to format. It can be any of these: Since 3.4 for 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 format
      offset - 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.
      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)
      Format 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.

      Parameters:
      kind - Use to specify the kind of the code snippet to format. It can be any of these: Since 3.4 for 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 format
      regions - a set of regions in source to format
      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.
      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
    • setOptions

      public void setOptions(Map<String,String> options)
      Sets the formatting options for this formatter.

      The default implementation ignores the options.

      Parameters:
      options - the options for the formatter
      Since:
      3.11