1 12 package org.eclipse.jdt.internal.ui.text; 13 14 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.StringTokenizer ; 20 21 import org.eclipse.core.runtime.Assert; 22 import org.eclipse.core.runtime.Preferences; 23 24 import org.eclipse.jface.preference.IPreferenceStore; 25 import org.eclipse.jface.util.PropertyChangeEvent; 26 27 import org.eclipse.jface.text.rules.ICharacterScanner; 28 import org.eclipse.jface.text.rules.IToken; 29 import org.eclipse.jface.text.rules.IWordDetector; 30 import org.eclipse.jface.text.rules.Token; 31 32 import org.eclipse.jdt.core.JavaCore; 33 34 import org.eclipse.jdt.ui.text.IColorManager; 35 import org.eclipse.jdt.ui.text.IJavaColorConstants; 36 37 import org.eclipse.jdt.internal.ui.text.CombinedWordRule.WordMatcher; 38 39 42 public class JavaCommentScanner extends AbstractJavaScanner{ 43 44 private static class AtJavaIdentifierDetector implements IWordDetector { 45 46 public boolean isWordStart(char c) { 47 return c == '@' || Character.isJavaIdentifierStart(c); 48 } 49 50 public boolean isWordPart(char c) { 51 return c == '.' || Character.isJavaIdentifierPart(c); 52 } 53 } 54 55 private class TaskTagMatcher extends CombinedWordRule.WordMatcher { 56 57 private IToken fToken; 58 62 private Map fUppercaseWords= new HashMap (); 63 67 private boolean fCaseSensitive= true; 68 72 private CombinedWordRule.CharacterBuffer fBuffer= new CombinedWordRule.CharacterBuffer(16); 73 74 public TaskTagMatcher(IToken token) { 75 fToken= token; 76 } 77 78 82 public synchronized void clearWords() { 83 super.clearWords(); 84 fUppercaseWords.clear(); 85 } 86 87 public synchronized void addTaskTags(String value) { 88 String [] tasks= split(value, ","); for (int i= 0; i < tasks.length; i++) { 90 if (tasks[i].length() > 0) { 91 addWord(tasks[i], fToken); 92 } 93 } 94 } 95 96 private String [] split(String value, String delimiters) { 97 StringTokenizer tokenizer= new StringTokenizer (value, delimiters); 98 int size= tokenizer.countTokens(); 99 String [] tokens= new String [size]; 100 int i= 0; 101 while (i < size) 102 tokens[i++]= tokenizer.nextToken(); 103 return tokens; 104 } 105 106 110 public synchronized void addWord(String word, IToken token) { 111 Assert.isNotNull(word); 112 Assert.isNotNull(token); 113 114 super.addWord(word, token); 115 fUppercaseWords.put(new CombinedWordRule.CharacterBuffer(word.toUpperCase()), token); 116 } 117 118 122 public synchronized IToken evaluate(ICharacterScanner scanner, CombinedWordRule.CharacterBuffer word) { 123 if (fCaseSensitive) 124 return super.evaluate(scanner, word); 125 126 fBuffer.clear(); 127 for (int i= 0, n= word.length(); i < n; i++) 128 fBuffer.append(Character.toUpperCase(word.charAt(i))); 129 130 IToken token= (IToken) fUppercaseWords.get(fBuffer); 131 if (token != null) 132 return token; 133 return Token.UNDEFINED; 134 } 135 136 142 public boolean isCaseSensitive() { 143 return fCaseSensitive; 144 } 145 146 152 public void setCaseSensitive(boolean caseSensitive) { 153 fCaseSensitive= caseSensitive; 154 } 155 } 156 157 private static final String COMPILER_TASK_TAGS= JavaCore.COMPILER_TASK_TAGS; 158 protected static final String TASK_TAG= IJavaColorConstants.TASK_TAG; 159 163 private static final String COMPILER_TASK_CASE_SENSITIVE= JavaCore.COMPILER_TASK_CASE_SENSITIVE; 164 168 private static final String ENABLED= JavaCore.ENABLED; 169 170 private TaskTagMatcher fTaskTagMatcher; 171 private Preferences fCorePreferenceStore; 172 private String fDefaultTokenProperty; 173 private String [] fTokenProperties; 174 175 public JavaCommentScanner(IColorManager manager, IPreferenceStore store, Preferences coreStore, String defaultTokenProperty) { 176 this(manager, store, coreStore, defaultTokenProperty, new String [] { defaultTokenProperty, TASK_TAG }); 177 } 178 179 public JavaCommentScanner(IColorManager manager, IPreferenceStore store, Preferences coreStore, String defaultTokenProperty, String [] tokenProperties) { 180 super(manager, store); 181 182 fCorePreferenceStore= coreStore; 183 fDefaultTokenProperty= defaultTokenProperty; 184 fTokenProperties= tokenProperties; 185 186 initialize(); 187 } 188 189 198 public JavaCommentScanner(IColorManager manager, IPreferenceStore store, String defaultTokenProperty) { 199 this(manager, store, null, defaultTokenProperty, new String [] { defaultTokenProperty, TASK_TAG }); 200 } 201 202 212 public JavaCommentScanner(IColorManager manager, IPreferenceStore store, String defaultTokenProperty, String [] tokenProperties) { 213 this(manager, store, null, defaultTokenProperty, tokenProperties); 214 } 215 216 219 protected List createRules() { 220 List list= new ArrayList (); 221 Token defaultToken= getToken(fDefaultTokenProperty); 222 223 List matchers= createMatchers(); 224 if (matchers.size() > 0) { 225 CombinedWordRule combinedWordRule= new CombinedWordRule(new AtJavaIdentifierDetector(), defaultToken); 226 for (int i= 0, n= matchers.size(); i < n; i++) 227 combinedWordRule.addWordMatcher((WordMatcher) matchers.get(i)); 228 list.add(combinedWordRule); 229 } 230 231 setDefaultReturnToken(defaultToken); 232 233 return list; 234 } 235 236 241 protected List createMatchers() { 242 List list= new ArrayList (); 243 244 boolean isCaseSensitive= true; 246 String tasks= null; 247 if (getPreferenceStore().contains(COMPILER_TASK_TAGS)) { 248 tasks= getPreferenceStore().getString(COMPILER_TASK_TAGS); 249 isCaseSensitive= ENABLED.equals(getPreferenceStore().getString(COMPILER_TASK_CASE_SENSITIVE)); 250 } else if (fCorePreferenceStore != null) { 251 tasks= fCorePreferenceStore.getString(COMPILER_TASK_TAGS); 252 isCaseSensitive= ENABLED.equals(fCorePreferenceStore.getString(COMPILER_TASK_CASE_SENSITIVE)); 253 } 254 if (tasks != null) { 255 fTaskTagMatcher= new TaskTagMatcher(getToken(TASK_TAG)); 256 fTaskTagMatcher.addTaskTags(tasks); 257 fTaskTagMatcher.setCaseSensitive(isCaseSensitive); 258 list.add(fTaskTagMatcher); 259 } 260 261 return list; 262 } 263 264 267 public boolean affectsBehavior(PropertyChangeEvent event) { 268 return event.getProperty().equals(COMPILER_TASK_TAGS) || event.getProperty().equals(COMPILER_TASK_CASE_SENSITIVE) || super.affectsBehavior(event); 269 } 270 271 274 public void adaptToPreferenceChange(PropertyChangeEvent event) { 275 if (fTaskTagMatcher != null && event.getProperty().equals(COMPILER_TASK_TAGS)) { 276 Object value= event.getNewValue(); 277 if (value instanceof String ) { 278 synchronized (fTaskTagMatcher) { 279 fTaskTagMatcher.clearWords(); 280 fTaskTagMatcher.addTaskTags((String ) value); 281 } 282 } 283 } else if (fTaskTagMatcher != null && event.getProperty().equals(COMPILER_TASK_CASE_SENSITIVE)) { 284 Object value= event.getNewValue(); 285 if (value instanceof String ) 286 fTaskTagMatcher.setCaseSensitive(ENABLED.equals(value)); 287 } else if (super.affectsBehavior(event)) 288 super.adaptToPreferenceChange(event); 289 } 290 291 294 protected String [] getTokenProperties() { 295 return fTokenProperties; 296 } 297 298 } 299 300 | Popular Tags |