|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| QuestionStartingNewStmt.java | - | 83.3% | 100% | 85.7% |
|
||||||||||||||
| 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 javax.swing.text.BadLocationException; | |
| 40 | ||
| 41 | import edu.rice.cs.util.UnexpectedException; | |
| 42 | import edu.rice.cs.drjava.model.AbstractDJDocument; | |
| 43 | ||
| 44 | /** | |
| 45 | * Determines if the current line is starting a new statement by | |
| 46 | * searching backwards to see if the previous line was the end | |
| 47 | * of a statement. Specifically, checks if the previous | |
| 48 | * non-whitespace character not on this line is one of the | |
| 49 | * following: ';', '{', '}', or 0. | |
| 50 | * <p> | |
| 51 | * Note that characters in comments and quotes are disregarded. | |
| 52 | * | |
| 53 | * @version $Id: QuestionStartingNewStmt.java 5175 2010-01-20 08:46:32Z mgricken $ | |
| 54 | */ | |
| 55 | public class QuestionStartingNewStmt extends IndentRuleQuestion { | |
| 56 | ||
| 57 | /** Constructs a new rule to determine if the current line is the start of a new statement. | |
| 58 | * @param yesRule Rule to use if this rule holds | |
| 59 | * @param noRule Rule to use if this rule does not hold | |
| 60 | */ | |
| 61 | 1476 | public QuestionStartingNewStmt(IndentRule yesRule, IndentRule noRule) { |
| 62 | 1476 | super(yesRule, noRule); |
| 63 | } | |
| 64 | ||
| 65 | /** Determines if the previous non-whitespace character not on this line was one of the following: ';', '{', '}' | |
| 66 | * or nothing. Ignores characters in quotes and comments. | |
| 67 | * @param doc AbstractDJDocument containing the line to be indented. | |
| 68 | * @return true if this node's rule holds. | |
| 69 | */ | |
| 70 | 117 | boolean applyRule(AbstractDJDocument doc, Indenter.IndentReason reason) { |
| 71 | ||
| 72 | 117 | char[] delims = {';', '{', '}'}; |
| 73 | 117 | int lineStart = doc._getLineStartPos(doc.getCurrentLocation()); |
| 74 | 117 | int prevDelimiterPos; |
| 75 | ||
| 76 | 117 | try { |
| 77 | 117 | prevDelimiterPos = doc.findPrevDelimiter(lineStart, delims); |
| 78 | } catch (BadLocationException e) { | |
| 79 | // Should not happen | |
| 80 | 0 | throw new UnexpectedException(e); |
| 81 | } | |
| 82 | ||
| 83 | // If no previous delimited exists, imaginary delimiter at position -1 | |
| 84 | ||
| 85 | // Delimiter must be at the end of its line (ignoring whitespace & comments) | |
| 86 | 117 | int firstNonWSAfterDelimiter; |
| 87 | 117 | try { |
| 88 | 117 | firstNonWSAfterDelimiter = doc.getFirstNonWSCharPos(prevDelimiterPos + 1); |
| 89 | // will return ERROR_INDEX (-1) if we hit the end of the document | |
| 90 | } | |
| 91 | 0 | catch (BadLocationException e) { throw new UnexpectedException(e); } |
| 92 | ||
| 93 | // If the first non-WS character is after the beginning of the line | |
| 94 | // or we reached the end of the document, then we are starting a new statement. | |
| 95 | 117 | return (firstNonWSAfterDelimiter >= lineStart || firstNonWSAfterDelimiter == -1); |
| 96 | } | |
| 97 | } | |
| 98 |
|
||||||||||