Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 


Eclipse JDT
Release 3.5

org.eclipse.jdt.core.formatter
Class CodeFormatter

java.lang.Object
  extended by 
org.eclipse.jdt.core.formatter.CodeFormatter

public abstract class CodeFormatter
extends Object

Specification for a generic source code formatter.

Since:
3.0
Restriction:
This class is not intended to be subclassed by clients.

Field Summary
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
static int K_COMPILATION_UNIT
          Kind used to format a compilation unit Note that using this constant, the comments are only indented while processing the compilation unit.
static int K_EXPRESSION
          Kind used to format an expression
static int K_JAVA_DOC
          Kind used to format a Javadoc comment
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
static int K_UNKNOWN
          Unknown kind Note that since 3.4, the F_INCLUDE_COMMENTS flag can be added to this constant in order to get the comments formatted if a compilation unit is processed.
 
Constructor Summary
CodeFormatter ()
           
 
Method Summary
  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.
 
Methods inherited from class java.lang. Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

K_UNKNOWN

public static final int K_UNKNOWN
Unknown kind Note that since 3.4, the F_INCLUDE_COMMENTS flag can be added to this constant in order to get the comments formatted if a compilation unit is processed.

See Also:
Constant Field Values

K_EXPRESSION

public static final int K_EXPRESSION
Kind used to format an expression

See Also:
Constant Field Values

K_STATEMENTS

public static final int K_STATEMENTS
Kind used to format a set of statements

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 declarations

See Also:
Constant Field Values

K_COMPILATION_UNIT

public static final int K_COMPILATION_UNIT
Kind used to format a compilation unit

Note that using this constant, the comments are only indented while processing the compilation unit.

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:
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

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 only be combined with K_COMPILATION_UNIT and K_UNKNOWN kinds for now but might be extended to other ones in future versions.

Note also that it has an effect only when the option DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_JAVADOC_COMMENT is set to DefaultCodeFormatterConstants.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:
Constant Field Values
Constructor Detail

CodeFormatter

public CodeFormatter()
Method Detail

format

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

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:
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 
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.

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:
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

Eclipse JDT
Release 3.5

Copyright (c) IBM Corp. and others 2000, 2008. All Rights Reserved.

 
 
  Published under the terms of the Eclipse Public License Version 1.0 ("EPL") Design by Interspire