001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.definitions.indent;
038:
039: import edu.rice.cs.drjava.model.AbstractDJDocument;
040:
041: /** Determines if the given search character is found between the start of the current statement and the end character.
042: * Accomplishes this by searching backwards from the end character, for the search character until one of the
043: * following characters is found: '}', '{', ';', DOCSTART.
044: * <b>The given end character must exist on the current line and not be part of a quote or comment.</b> If there is
045: * more than end character on the given line, then the first end character is used.
046: * <p>This question is useful for determining if, when a colon is found on a line, it is part of a ternary operator
047: * or not (construct this question with '?' for search character and ':' for end character).
048: * <p>It can also be used to determine if a statement contains a particular character by constructing it with the
049: * desired character as a search character and the end character as ';'.
050: * <p>Note that characters in comments and quotes are disregarded.
051: *
052: * @version $Id: QuestionExistsCharInStmt.java 4255 2007-08-28 19:17:37Z mgricken $
053: */
054: public class QuestionExistsCharInStmt extends IndentRuleQuestion {
055: /**
056: * The character to search for
057: */
058: private char _findChar;
059:
060: /**
061: * The character which marks the end of the search
062: * space. i.e. search from the start of the statment
063: * to the end char.
064: */
065: private char _endChar;
066:
067: /**
068: * Constructs a rule to determine if findChar exists
069: * between the start of the current statement and endChar.
070: *
071: * @param findChar Character to search for from the start of the
072: * statement to endChar
073: * @param endChar Character that marks the end of the search space. Must
074: * exist on the current line and not be in quotes or comments.
075: * @param yesRule Rule to use if this rule holds
076: * @param noRule Rule to use if this rule does not hold
077: */
078: public QuestionExistsCharInStmt(char findChar, char endChar,
079: IndentRule yesRule, IndentRule noRule) {
080: super (yesRule, noRule);
081: _findChar = findChar;
082: _endChar = endChar;
083: }
084:
085: /** Searches backwards from endChar to the start of the statement looking for findChar. Ignores characters in
086: * comments and quotes. Start of the statement is the point right after when one of the following characters
087: * is found: ';', '{', '}', DOCSTART.
088: *
089: * @param doc AbstractDJDocument containing the line to be indented.
090: * @return true if this node's rule holds.
091: */
092: boolean applyRule(AbstractDJDocument doc,
093: Indenter.IndentReason reason) {
094:
095: // Find the position of endChar on the current line
096: int endCharPos = doc.findCharOnLine(doc.getCurrentLocation(),
097: _endChar);
098: return doc.findCharInStmtBeforePos(_findChar, endCharPos);
099: }
100: }
|