KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > definitions > indent > Indenter


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.definitions.indent;
35
36 import edu.rice.cs.drjava.model.AbstractDJDocument;
37 import edu.rice.cs.drjava.DrJava;
38 import edu.rice.cs.drjava.config.OptionConstants;
39
40 /** Singleton class to construct and use the indentation decision tree.
41   * @version $Id: Indenter.java 3993 2006-09-02 20:23:38Z rcartwright $
42   */

43 public class Indenter {
44
45   public Indenter(int indentLevel) { buildTree(indentLevel); }
46   
47   /* Indenting Reasons */
48
49   /** Indicates that an enter key press caused the indentation. This is important for some rules dealing with stars
50     * at the line start in multiline comments
51     */

52   public static final int ENTER_KEY_PRESS = 1;
53
54   /** Indicates that indentation was started for some other reason. This is important for some rules dealing with stars
55     * at the line start in multiline comments
56     */

57   public static final int OTHER = 0;
58
59   /** Root of decision tree. */
60   protected IndentRule _topRule;
61
62   /** Builds the decision tree for indentation.
63     * For now, this method needs to be called every time the size of one indent level is being changed!
64     */

65   public void buildTree(int indentLevel) {
66     char[] indent = new char[indentLevel];
67     java.util.Arrays.fill(indent,' ');
68     final String JavaDoc oneLevel = new String JavaDoc(indent);
69
70     boolean autoCloseComments = DrJava.getConfig().getSetting(OptionConstants.AUTO_CLOSE_COMMENTS).booleanValue();
71     
72     IndentRule
73       // Main tree
74
rule37 = new ActionStartCurrStmtPlus(oneLevel),
75       rule36 = new ActionStartStmtOfBracePlus(oneLevel),
76       rule35 = rule37,
77       rule34 = new QuestionExistsCharInStmt('?', ':', rule35, rule36),
78       rule33 = new QuestionLineContains(':', rule34, rule37),
79       rule32 = new ActionStartCurrStmtPlus(""),
80       rule31 = new QuestionCurrLineStartsWithSkipComments("{", rule32, rule33),
81       rule39 = new ActionStartPrevStmtPlus("", true),
82       rule29 = rule36,
83       rule28 = new ActionStartPrevStmtPlus("", false),
84       rule40 = rule28,
85       rule30 = new QuestionExistsCharInPrevStmt('?', rule40, rule39),
86       rule27 = new QuestionExistsCharInStmt('?', ':', rule28, rule29),
87       rule26 = new QuestionLineContains(':', rule27, rule30),
88       rule25 = new QuestionStartingNewStmt(rule26, rule31), // no preceding open brace
89
rule24 = rule25,
90       rule23 = rule36,
91       rule22 = new QuestionHasCharPrecedingOpenBrace(new char[] {'=',',','{'},rule23,rule24),
92       rule21 = rule36,
93       rule20 = new QuestionStartAfterOpenBrace(rule21, rule22),
94       rule19 = new ActionStartStmtOfBracePlus(""),
95       rule18 = new QuestionCurrLineStartsWithSkipComments("}", rule19, rule20), // ANONYMOUS inner class formatting breaks here
96
rule17 = new QuestionBraceIsCurly(rule18, rule25), // enclosing block/expr-list opens with '{'?
97
rule16 = new ActionBracePlus(" " + oneLevel),
98       rule15 = new ActionBracePlus(" "),
99       rule38 = new QuestionCurrLineStartsWith(")", rule30, rule15), // does current line start with ')'?
100
rule14 = new QuestionNewParenPhrase(rule38, rule16), // is current line new phrase after open paren?
101
rule13 = new QuestionBraceIsParenOrBracket(rule14, rule17), // enclosing block/expr-list opens with "(" or "["?
102

103       // Comment tree
104
rule12 = new ActionStartPrevLinePlus(""),
105       rule11 = rule12,
106       rule10 = new ActionStartPrevLinePlus("* "),
107       rule09 = new QuestionCurrLineEmptyOrEnterPress(rule10, rule11),
108       rule08 = rule12,
109       rule07 = new QuestionCurrLineStartsWith("*", rule08, rule09),
110       rule06 = new QuestionPrevLineStartsWith("*", rule07, rule12),
111       rule05 = new ActionStartPrevLinePlus(" "), // padding prefix for interior of ordinary block comment
112
rule04 = new ActionStartPrevLinePlus(" * "), // padding prefix for new line within ordinary block comment
113
rule46 = new ActionStartPrevLinePlus(" * "), // padding prefix for new line within special javadoc block comment
114
rule47 = new ActionStartPrevLinePlus(" "), // padding prefix for interior of special javadoc block comment
115
rule45 = new QuestionPrevLineStartsJavaDocWithText(rule46, rule04), // Prev line begins special javadoc comment?
116
rule48 = new QuestionPrevLineStartsJavaDocWithText(rule47, rule05), // Prev line begins special javadoc comment?
117
rule41 = new ActionStartPrevLinePlusMultilinePreserve(new String JavaDoc[] { " * \n", " */" }, 0, 3, 0, 3),
118       rule49 = new ActionStartPrevLinePlusMultilinePreserve(new String JavaDoc[] { " * \n", " */"}, 0, 4, 0, 4),
119       rule50 = new QuestionPrevLineStartsJavaDocWithText(rule49, rule41),
120
121       rule03 = new QuestionCurrLineEmptyOrEnterPress(rule45, rule48),
122       rule42 = new QuestionFollowedByStar(rule04, rule41),
123 // rule49 = new ActionStartPrevLinePlusMultilinePreserve(new String[] {" */" }, 0, 4, 0, 4),
124
// rule50 = new QuestionFollowedByStar(rule46, rule49),
125
// rule51 = new QuestionPrevLineStartsJavaDocWithText(rule50, rule42),
126
rule51 = new QuestionCurrLineEmpty(rule50, rule03), // autoClose: rule03 unnecessarily retests CurrentLineEmpty
127
rule02 = new QuestionPrevLineStartsComment(autoCloseComments ? rule51 : rule03, rule06),
128       rule43 = new ActionDoNothing(),
129       rule44 = new QuestionCurrLineIsWingComment(rule43, rule13),
130       rule01 = new QuestionInsideComment(rule02, rule44);
131
132     _topRule = rule01;
133   }
134
135   /** Indents the current line based on a decision tree which determines the indent based on context.
136     * @param doc document containing line to be indented
137     * @return true if the condition tested by the top rule holds, false otherwise
138     */

139   public boolean indent(AbstractDJDocument doc, int reason) {
140 // Utilities.showDebug("Indenter.indent called on doc " + doc);
141
return _topRule.indentLine(doc, reason);
142   }
143 }
144
145
146
147
Popular Tags