KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > formatter > CodeFormatter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.core.formatter;
12
13 import org.eclipse.jdt.internal.compiler.util.Util;
14 import org.eclipse.text.edits.TextEdit;
15
16 /**
17  * Specification for a generic source code formatter.
18  *
19  * @since 3.0
20  */

21 public abstract class CodeFormatter {
22
23     /**
24      * Unknown kind
25      */

26     public static final int K_UNKNOWN = 0x00;
27
28     /**
29      * Kind used to format an expression
30      */

31     public static final int K_EXPRESSION = 0x01;
32     
33     /**
34      * Kind used to format a set of statements
35      */

36     public static final int K_STATEMENTS = 0x02;
37     
38     /**
39      * Kind used to format a set of class body declarations
40      */

41     public static final int K_CLASS_BODY_DECLARATIONS = 0x04;
42     
43     /**
44      * Kind used to format a compilation unit
45      */

46     public static final int K_COMPILATION_UNIT = 0x08;
47
48     /**
49      * Kind used to format a single-line comment
50      * @since 3.1
51      */

52     public static final int K_SINGLE_LINE_COMMENT = 0x10;
53     /**
54      * Kind used to format a multi-line comment
55      * @since 3.1
56      */

57     public static final int K_MULTI_LINE_COMMENT = 0x20;
58     /**
59      * Kind used to format a Javadoc comment
60      * @since 3.1
61      */

62     public static final int K_JAVA_DOC = 0x40;
63
64     /**
65      * Format <code>source</code>,
66      * and returns a text edit that correspond to the difference between the given string and the formatted string.
67      * <p>It returns null if the given string cannot be formatted.</p>
68      *
69      * <p>If the offset position is matching a whitespace, the result can include whitespaces. It would be up to the
70      * caller to get rid of preceeding whitespaces.</p>
71      *
72      * @param kind Use to specify the kind of the code snippet to format. It can be any of these:
73      * K_EXPRESSION, K_STATEMENTS, K_CLASS_BODY_DECLARATIONS, K_COMPILATION_UNIT, K_UNKNOWN,
74      * K_SINGLE_LINE_COMMENT, K_MULTI_LINE_COMMENT, K_JAVA_DOC
75      * @param source the source to format
76      * @param offset the given offset to start recording the edits (inclusive).
77      * @param length the given length to stop recording the edits (exclusive).
78      * @param indentationLevel the initial indentation level, used
79      * to shift left/right the entire source fragment. An initial indentation
80      * level of zero or below has no effect.
81      * @param lineSeparator the line separator to use in formatted source,
82      * if set to <code>null</code>, then the platform default one will be used.
83      * @return the text edit
84      * @throws IllegalArgumentException if offset is lower than 0, length is lower than 0 or
85      * length is greater than source length.
86      */

87     public abstract TextEdit format(int kind, String JavaDoc source, int offset, int length, int indentationLevel, String JavaDoc lineSeparator);
88     
89     /**
90      * Answers the string that corresponds to the indentation to the given indentation level or an empty string
91      * if the indentation cannot be computed.
92      * <p>This method needs to be overriden in a subclass.</p>
93      *
94      * <p>The default implementation returns an empty string.</p>
95      *
96      * @param indentationLevel the given indentation level
97      * @return the string corresponding to the right indentation level
98      * @exception IllegalArgumentException if the given indentation level is lower than zero
99      * @since 3.2
100      */

101     public String JavaDoc createIndentationString(int indentationLevel) {
102         return Util.EMPTY_STRING;
103     }
104 }
105
Popular Tags