1 11 package org.eclipse.jdt.internal.ui.text; 12 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.core.runtime.Assert; 20 21 import org.eclipse.jface.text.rules.ICharacterScanner; 22 import org.eclipse.jface.text.rules.IRule; 23 import org.eclipse.jface.text.rules.IToken; 24 import org.eclipse.jface.text.rules.IWordDetector; 25 import org.eclipse.jface.text.rules.Token; 26 27 28 44 public class CombinedWordRule implements IRule { 45 46 49 public static class WordMatcher { 50 51 52 private Map fWords= new HashMap (); 53 54 60 public void addWord(String word, IToken token) { 61 Assert.isNotNull(word); 62 Assert.isNotNull(token); 63 64 fWords.put(new CharacterBuffer(word), token); 65 } 66 67 74 public IToken evaluate(ICharacterScanner scanner, CharacterBuffer word) { 75 IToken token= (IToken) fWords.get(word); 76 if (token != null) 77 return token; 78 return Token.UNDEFINED; 79 } 80 81 84 public void clearWords() { 85 fWords.clear(); 86 } 87 } 88 89 92 public static class CharacterBuffer { 93 94 95 private char[] fContent; 96 97 private int fLength= 0; 98 99 100 private boolean fIsHashCached= false; 101 102 private int fHashCode; 103 104 109 public CharacterBuffer(int capacity) { 110 fContent= new char[capacity]; 111 } 112 113 118 public CharacterBuffer(String content) { 119 fContent= content.toCharArray(); 120 fLength= content.length(); 121 } 122 123 126 public void clear() { 127 fIsHashCached= false; 128 fLength= 0; 129 } 130 131 136 public void append(char c) { 137 fIsHashCached= false; 138 if (fLength == fContent.length) { 139 char[] old= fContent; 140 fContent= new char[old.length << 1]; 141 System.arraycopy(old, 0, fContent, 0, old.length); 142 } 143 fContent[fLength++]= c; 144 } 145 146 151 public int length() { 152 return fLength; 153 } 154 155 160 public String toString() { 161 return new String (fContent, 0, fLength); 162 } 163 164 170 public char charAt(int i) { 171 return fContent[i]; 172 } 173 174 177 public int hashCode() { 178 if (fIsHashCached) 179 return fHashCode; 180 181 int hash= 0; 182 for (int i= 0, n= fLength; i < n; i++) 183 hash= 29*hash + fContent[i]; 184 fHashCode= hash; 185 fIsHashCached= true; 186 return hash; 187 } 188 189 190 193 public boolean equals(Object obj) { 194 if (obj == this) 195 return true; 196 if (!(obj instanceof CharacterBuffer)) 197 return false; 198 CharacterBuffer buffer= (CharacterBuffer) obj; 199 int length= buffer.length(); 200 if (length != fLength) 201 return false; 202 for (int i= 0; i < length; i++) 203 if (buffer.charAt(i) != fContent[i]) 204 return false; 205 return true; 206 } 207 208 214 public boolean equals(String string) { 215 int length= string.length(); 216 if (length != fLength) 217 return false; 218 for (int i= 0; i < length; i++) 219 if (string.charAt(i) != fContent[i]) 220 return false; 221 return true; 222 } 223 } 224 225 226 private static final int UNDEFINED= -1; 227 228 229 private IWordDetector fDetector; 230 231 private IToken fDefaultToken; 232 233 private int fColumn= UNDEFINED; 234 235 private CharacterBuffer fBuffer= new CharacterBuffer(16); 236 237 238 private List fMatchers= new ArrayList (); 239 240 250 public CombinedWordRule(IWordDetector detector) { 251 this(detector, null, Token.UNDEFINED); 252 } 253 254 265 public CombinedWordRule(IWordDetector detector, IToken defaultToken) { 266 this(detector, null, defaultToken); 267 } 268 269 280 public CombinedWordRule(IWordDetector detector, WordMatcher matcher) { 281 this(detector, matcher, Token.UNDEFINED); 282 } 283 284 296 public CombinedWordRule(IWordDetector detector, WordMatcher matcher, IToken defaultToken) { 297 298 Assert.isNotNull(detector); 299 Assert.isNotNull(defaultToken); 300 301 fDetector= detector; 302 fDefaultToken= defaultToken; 303 if (matcher != null) 304 addWordMatcher(matcher); 305 } 306 307 308 313 public void addWordMatcher(WordMatcher matcher) { 314 fMatchers.add(matcher); 315 } 316 317 325 public void setColumnConstraint(int column) { 326 if (column < 0) 327 column= UNDEFINED; 328 fColumn= column; 329 } 330 331 334 public IToken evaluate(ICharacterScanner scanner) { 335 int c= scanner.read(); 336 if (fDetector.isWordStart((char) c)) { 337 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) { 338 339 fBuffer.clear(); 340 do { 341 fBuffer.append((char) c); 342 c= scanner.read(); 343 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c)); 344 scanner.unread(); 345 346 for (int i= 0, n= fMatchers.size(); i < n; i++) { 347 IToken token= ((WordMatcher) fMatchers.get(i)).evaluate(scanner, fBuffer); 348 if (!token.isUndefined()) 349 return token; 350 } 351 352 if (fDefaultToken.isUndefined()) 353 unreadBuffer(scanner); 354 355 return fDefaultToken; 356 } 357 } 358 359 scanner.unread(); 360 return Token.UNDEFINED; 361 } 362 363 368 private void unreadBuffer(ICharacterScanner scanner) { 369 for (int i= fBuffer.length() - 1; i >= 0; i--) 370 scanner.unread(); 371 } 372 } 373 | Popular Tags |