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 javax.swing.text.BadLocationException;
040:
041: import edu.rice.cs.util.UnexpectedException;
042: import edu.rice.cs.drjava.model.AbstractDJDocument;
043:
044: /**
045: * Determines if the current line is starting a new statement by
046: * searching backwards to see if the previous line was the end
047: * of a statement. Specifically, checks if the previous
048: * non-whitespace character not on this line is one of the
049: * following: ';', '{', '}', or DOCSTART.
050: * <p>
051: * Note that characters in comments and quotes are disregarded.
052: *
053: * @version $Id: QuestionStartingNewStmt.java 4255 2007-08-28 19:17:37Z mgricken $
054: */
055: public class QuestionStartingNewStmt extends IndentRuleQuestion {
056:
057: /**
058: * Constructs a new rule to determine if the current line is
059: * the start of a new statement.
060: * @param yesRule Rule to use if this rule holds
061: * @param noRule Rule to use if this rule does not hold
062: */
063: public QuestionStartingNewStmt(IndentRule yesRule, IndentRule noRule) {
064: super (yesRule, noRule);
065: }
066:
067: /**
068: * Determines if the previous non-whitespace character not on
069: * this line was one of the following: ';', '{', '}' or DOCSTART.
070: * Ignores characters in quotes and comments.
071: * @param doc AbstractDJDocument containing the line to be indented.
072: * @return true if this node's rule holds.
073: */
074: boolean applyRule(AbstractDJDocument doc,
075: Indenter.IndentReason reason) {
076:
077: char[] delims = { ';', '{', '}' };
078: int lineStart = doc.getLineStartPos(doc.getCurrentLocation());
079: int prevDelimiterPos;
080:
081: try {
082: prevDelimiterPos = doc.findPrevDelimiter(lineStart, delims);
083: } catch (BadLocationException e) {
084: // Should not happen
085: throw new UnexpectedException(e);
086: }
087:
088: // For DOCSTART, imaginary delimiter at position -1
089: if (prevDelimiterPos == AbstractDJDocument.ERROR_INDEX) {
090: prevDelimiterPos = -1;
091: }
092:
093: // Delimiter must be at the end of its line (ignoring whitespace & comments)
094: int firstNonWSAfterDelimiter;
095: try {
096: firstNonWSAfterDelimiter = doc
097: .getFirstNonWSCharPos(prevDelimiterPos + 1);
098: // will return ERROR_INDEX if we hit the end of the document
099: } catch (BadLocationException e) {
100: throw new UnexpectedException(e);
101: }
102:
103: // If the first non-WS character is after the beginning of the line
104: // or we reached the end of the document, then we are starting a new statement.
105: return (firstNonWSAfterDelimiter >= lineStart || firstNonWSAfterDelimiter == AbstractDJDocument.ERROR_INDEX);
106: }
107: }
|