|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| Indenter.java | 50% | 90.9% | 75% | 82.4% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.model.definitions.indent; | |
| 38 | ||
| 39 | import edu.rice.cs.drjava.model.AbstractDJDocument; | |
| 40 | import edu.rice.cs.drjava.DrJava; | |
| 41 | import edu.rice.cs.drjava.config.OptionConstants; | |
| 42 | ||
| 43 | /** Singleton class to construct and use the indentation decision tree. | |
| 44 | * @version $Id: Indenter.java 5175 2010-01-20 08:46:32Z mgricken $ | |
| 45 | */ | |
| 46 | public class Indenter { | |
| 47 | ||
| 48 | 1350 | public Indenter(int indentLevel) { |
| 49 | 1350 | _indentLevel = indentLevel; |
| 50 | 1350 | buildTree(indentLevel); |
| 51 | } | |
| 52 | ||
| 53 | protected int _indentLevel; | |
| 54 | ||
| 55 | /** Enumeration of reasons why indentation may be preformed. */ | |
| 56 | public enum IndentReason { | |
| 57 | /** Indicates that an enter key press caused the indentation. This is important for some rules dealing with stars | |
| 58 | * at the line start in multiline comments | |
| 59 | */ | |
| 60 | ENTER_KEY_PRESS, | |
| 61 | /** Indicates that indentation was started for some other reason. This is important for some rules dealing with stars | |
| 62 | * at the line start in multiline comments | |
| 63 | */ | |
| 64 | OTHER | |
| 65 | } | |
| 66 | ||
| 67 | /** Root of decision tree. */ | |
| 68 | protected IndentRule _topRule; | |
| 69 | ||
| 70 | 0 | public int getIndentLevel() { return _indentLevel; } |
| 71 | ||
| 72 | /** Builds the decision tree for indentation. | |
| 73 | * For now, this method needs to be called every time the size of one indent level is being changed! | |
| 74 | */ | |
| 75 | 1474 | public void buildTree(int indentLevel) { |
| 76 | 1474 | char[] indent = new char[indentLevel]; |
| 77 | 1474 | java.util.Arrays.fill(indent,' '); |
| 78 | ||
| 79 | 1474 | boolean autoCloseComments = false; |
| 80 | 1474 | try { autoCloseComments = DrJava.getConfig().getSetting(OptionConstants.AUTO_CLOSE_COMMENTS).booleanValue(); } |
| 81 | catch(Exception e) { /* ignore */ } // some unit tests produce NullPointer exceptions in preceding line | |
| 82 | ||
| 83 | 1474 | IndentRule |
| 84 | // Main tree | |
| 85 | rule60 = new ActionStartPrevLinePlus(""), | |
| 86 | rule37 = new ActionStartCurrStmtPlus(indentLevel), | |
| 87 | rule36 = new ActionStartStmtOfBracePlus(indentLevel), | |
| 88 | // the following two rules should be inserted after the instantiated classed have been implemented | |
| 89 | // rule40 = new ActionStartOfAnonInnerClass(indentLevel), | |
| 90 | // rule35 = new QuestionAnonInnerClassPrefix(rule40, rule36); | |
| 91 | rule34 = new QuestionExistsCharInStmt('?', ':', rule37, rule36), | |
| 92 | rule33 = new QuestionLineContains(':', rule34, rule37), | |
| 93 | rule32 = new ActionStartCurrStmtPlus(0), | |
| 94 | rule31 = new QuestionCurrLineStartsWithSkipComments("{", rule32, rule33), | |
| 95 | rule39 = new ActionStartPrevStmtPlus(0, true), // Indent line that starts new statement | |
| 96 | // rule29 = rule36, | |
| 97 | rule28 = new ActionStartPrevStmtPlus(0, false), | |
| 98 | rule30 = new QuestionExistsCharInPrevStmt('?', rule28, rule39), | |
| 99 | rule27 = new QuestionExistsCharInStmt('?', ':', rule28, rule36), | |
| 100 | rule26 = new QuestionLineContains(':', rule27, rule30), | |
| 101 | rule25 = new QuestionStartingNewStmt(rule26, rule31), // Is this line the start of a new statement? | |
| 102 | rule24 = new QuestionPrevLineStartsWith("@", rule60, rule25), // Does this line follow an annotation? ?? | |
| 103 | // Is this line an element of an array initializer? | |
| 104 | rule22 = new QuestionHasCharPrecedingOpenBrace(new char[] {'=',',','{'}, rule36, rule24), | |
| 105 | rule20 = new QuestionStartAfterOpenBrace(rule36, rule22), // Does the preceding line contain an open curly brace? | |
| 106 | rule19 = new ActionStartStmtOfBracePlus(0), // indents the line to match whitespace preceding the line enclosing brace | |
| 107 | // ANONYMOUS inner class formatting breaks here? | |
| 108 | // Does current line begin with '}' ignoring comment text, WS | |
| 109 | rule18 = new QuestionCurrLineStartsWithSkipComments("}", rule19, rule20), | |
| 110 | // Is brace enclosing this line '{' (as opposed to quotes, etc.); '(', '[' already excluded | |
| 111 | rule17 = new QuestionBraceIsCurly(rule18, rule24), | |
| 112 | rule16 = new ActionBracePlus(1 + indentLevel), | |
| 113 | rule15 = new ActionBracePlus(1), | |
| 114 | ||
| 115 | rule38 = new ActionBracePlus(0), | |
| 116 | rule14 = new QuestionNewParenPhrase(rule15, rule16), // is current non ) line a new phrase after open paren? | |
| 117 | rule23 = new QuestionNewParenPhrase(rule30, rule38), // is current ) line a new phrase after open paren? | |
| 118 | rule21 = new QuestionCurrLineStartsWith(")", rule23, rule14), // does current line start with ')'? | |
| 119 | // root of non-comment indent tree: is brace enclosing start of this line in { '(', '['}? | |
| 120 | rule13 = new QuestionBraceIsParenOrBracket(rule21, rule17), | |
| 121 | ||
| 122 | // Comment tree | |
| 123 | rule12 = new ActionStartPrevLinePlus(""), | |
| 124 | // rule11 = rule12, | |
| 125 | rule10 = new ActionStartPrevLinePlus("* "), | |
| 126 | rule09 = new QuestionCurrLineEmptyOrEnterPress(rule10, rule12), | |
| 127 | // rule08 = rule12, | |
| 128 | rule07 = new QuestionCurrLineStartsWith("*", rule12, rule09), | |
| 129 | rule06 = new QuestionPrevLineStartsWith("*", rule07, rule12), | |
| 130 | rule05 = new ActionStartPrevLinePlus(" "), // padding prefix for interior of ordinary block comment | |
| 131 | rule04 = new ActionStartPrevLinePlus(" * "), // padding prefix for new line within ordinary block comment | |
| 132 | rule46 = new ActionStartPrevLinePlus(" * "), // padding prefix for new line within special javadoc block comment | |
| 133 | rule47 = new ActionStartPrevLinePlus(" "), // padding prefix for interior of special javadoc block comment | |
| 134 | rule45 = new QuestionPrevLineStartsJavaDocWithText(rule46, rule04), // Prev line begins special javadoc comment? | |
| 135 | rule48 = new QuestionPrevLineStartsJavaDocWithText(rule47, rule05), // Prev line begins special javadoc comment? | |
| 136 | rule41 = new ActionStartPrevLinePlusMultilinePreserve(new String[] { " * \n", " */" }, 0, 3, 0, 3), | |
| 137 | rule49 = new ActionStartPrevLinePlusMultilinePreserve(new String[] { " * \n", " */"}, 0, 4, 0, 4), | |
| 138 | rule50 = new QuestionPrevLineStartsJavaDocWithText(rule49, rule41), | |
| 139 | ||
| 140 | rule03 = new QuestionCurrLineEmptyOrEnterPress(rule45, rule48), | |
| 141 | // rule42 = new QuestionFollowedByStar(rule04, rule41), | |
| 142 | // rule49 = new ActionStartPrevLinePlusMultilinePreserve(new String[] {" */" }, 0, 4, 0, 4), | |
| 143 | // rule50 = new QuestionFollowedByStar(rule46, rule49), | |
| 144 | // rule51 = new QuestionPrevLineStartsJavaDocWithText(rule50, rule42), | |
| 145 | rule51 = new QuestionCurrLineEmpty(rule50, rule03), // autoClose: rule03 unnecessarily retests CurrentLineEmpty | |
| 146 | 1473 | rule02 = new QuestionPrevLineStartsComment(autoCloseComments ? rule51 : rule03, rule06), |
| 147 | rule43 = new ActionDoNothing(), | |
| 148 | rule44 = new QuestionCurrLineIsWingComment(rule43, rule13), | |
| 149 | rule01 = new QuestionInsideComment(rule02, rule44); | |
| 150 | ||
| 151 | 1473 | _topRule = rule01; |
| 152 | } | |
| 153 | ||
| 154 | /** Indents the current line based on a decision tree which determines the indent based on context. | |
| 155 | * @param doc document containing line to be indented Assumes that reduced lock is already held. | |
| 156 | * @return true if the condition tested by the top rule holds, false otherwise | |
| 157 | */ | |
| 158 | 236 | public boolean indent(AbstractDJDocument doc, Indenter.IndentReason reason) { |
| 159 | // Utilities.showDebug("Indenter.indent called on doc " + doc); | |
| 160 | 236 | return _topRule.indentLine(doc, reason); |
| 161 | } | |
| 162 | } | |
| 163 | ||
| 164 | ||
| 165 |
|
||||||||||