1 33 34 package edu.rice.cs.drjava.model.definitions.indent; 35 36 import javax.swing.text.*; 37 import edu.rice.cs.util.UnexpectedException; 38 39 import edu.rice.cs.drjava.model.AbstractDJDocument; 40 import edu.rice.cs.drjava.model.definitions.reducedmodel.*; 41 42 47 public class QuestionCurrLineStartsWithSkipComments extends IndentRuleQuestion { 48 51 private String _prefix; 52 53 56 public QuestionCurrLineStartsWithSkipComments(String prefix, IndentRule yesRule, IndentRule noRule) { 57 super(yesRule, noRule); 58 _prefix = prefix; 59 } 60 61 68 boolean applyRule(AbstractDJDocument doc, int reason) { 69 try { 70 72 int currentPos = doc.getCurrentLocation(), 73 startPos = doc.getLineFirstCharPos(currentPos), 74 endPos = doc.getLineEndPos(currentPos), 75 lineLength = endPos - startPos; 76 77 char currentChar, previousChar = '\0'; 78 String text = doc.getText(startPos, lineLength); 79 80 for (int i = 0; i < lineLength; i++) { 81 84 doc.move( startPos - currentPos + i); 85 ReducedModelState state = doc.getStateAtCurrent(); 86 doc.move(-startPos + currentPos - i); 87 88 89 currentChar = text.charAt(i); 90 91 if (state.equals(ReducedModelState.INSIDE_LINE_COMMENT)) return false; 92 if (state.equals(ReducedModelState.INSIDE_BLOCK_COMMENT)) { previousChar = '\0'; 94 continue; 95 } 96 if (state.equals(ReducedModelState.FREE)) { if (_prefix.length() > lineLength - i) return false; 98 else if (text.substring(i, i+_prefix.length()).equals(_prefix) && previousChar != '/') { 99 return true; 103 } 104 else if (currentChar == '/') { 105 if (previousChar == '/') return false; 106 } 107 else if (currentChar == ' ' || currentChar == '\t') { } 108 else if (!(currentChar == '*' && previousChar == '/')) return false; 109 } 110 if (previousChar == '/' && currentChar != '*') return false; 111 previousChar = currentChar; 112 } 113 return false; 114 } 115 catch (BadLocationException e) { 116 throw new UnexpectedException(new RuntimeException ("Bug in QuestionCurrLineStartsWithSkipComments")); 118 } 119 } 120 } 121 | Popular Tags |