1 11 package org.eclipse.jdt.internal.ui.text.javadoc; 12 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.Preferences; 18 19 import org.eclipse.jface.preference.IPreferenceStore; 20 21 import org.eclipse.jface.text.BadLocationException; 22 import org.eclipse.jface.text.IDocument; 23 import org.eclipse.jface.text.rules.ICharacterScanner; 24 import org.eclipse.jface.text.rules.IToken; 25 import org.eclipse.jface.text.rules.IWordDetector; 26 import org.eclipse.jface.text.rules.SingleLineRule; 27 import org.eclipse.jface.text.rules.Token; 28 import org.eclipse.jface.text.rules.WhitespaceRule; 29 import org.eclipse.jface.text.rules.WordRule; 30 31 import org.eclipse.jdt.ui.text.IColorManager; 32 import org.eclipse.jdt.ui.text.IJavaColorConstants; 33 34 import org.eclipse.jdt.internal.ui.text.CombinedWordRule; 35 import org.eclipse.jdt.internal.ui.text.JavaCommentScanner; 36 import org.eclipse.jdt.internal.ui.text.JavaWhitespaceDetector; 37 import org.eclipse.jdt.internal.ui.text.CombinedWordRule.CharacterBuffer; 38 import org.eclipse.jdt.internal.ui.text.CombinedWordRule.WordMatcher; 39 40 43 public final class JavaDocScanner extends JavaCommentScanner { 44 45 46 49 static class HTMLCommentDetector implements IWordDetector { 50 51 54 public boolean isWordStart(char c) { 55 return (c == '<' || c == '-'); 56 } 57 58 61 public boolean isWordPart(char c) { 62 return (c == '-' || c == '!' || c == '>'); 63 } 64 } 65 66 class TagRule extends SingleLineRule { 67 68 71 public TagRule(IToken token) { 72 super("<", ">", token, (char) 0); } 74 75 78 public TagRule(IToken token, char escapeCharacter) { 79 super("<", ">", token, escapeCharacter); } 81 82 private IToken evaluateToken() { 83 try { 84 final String token= getDocument().get(getTokenOffset(), getTokenLength()) + "."; 86 int offset= 0; 87 char character= token.charAt(++offset); 88 89 if (character == '/') 90 character= token.charAt(++offset); 91 92 while (Character.isWhitespace(character)) 93 character= token.charAt(++offset); 94 95 while (Character.isLetterOrDigit(character)) 96 character= token.charAt(++offset); 97 98 while (Character.isWhitespace(character)) 99 character= token.charAt(++offset); 100 101 if (offset >= 2 && token.charAt(offset) == fEndSequence[0]) 102 return fToken; 103 104 } catch (BadLocationException exception) { 105 } 107 return getToken(IJavaColorConstants.JAVADOC_DEFAULT); 108 } 109 110 113 public IToken evaluate(ICharacterScanner scanner) { 114 IToken result= super.evaluate(scanner); 115 if (result == fToken) 116 return evaluateToken(); 117 return result; 118 } 119 } 120 121 private static String [] fgTokenProperties= { 122 IJavaColorConstants.JAVADOC_KEYWORD, 123 IJavaColorConstants.JAVADOC_TAG, 124 IJavaColorConstants.JAVADOC_LINK, 125 IJavaColorConstants.JAVADOC_DEFAULT, 126 TASK_TAG 127 }; 128 129 130 public JavaDocScanner(IColorManager manager, IPreferenceStore store, Preferences coreStore) { 131 super(manager, store, coreStore, IJavaColorConstants.JAVADOC_DEFAULT, fgTokenProperties); 132 } 133 134 141 public JavaDocScanner(IColorManager manager, IPreferenceStore store) { 142 this(manager, store, null); 143 } 144 145 public IDocument getDocument() { 146 return fDocument; 147 } 148 149 152 protected List createRules() { 153 154 List list= new ArrayList (); 155 156 Token token= getToken(IJavaColorConstants.JAVADOC_TAG); 158 list.add(new TagRule(token)); 159 160 161 WordRule wordRule= new WordRule(new HTMLCommentDetector(), token); 163 wordRule.addWord("<!--", token); wordRule.addWord("--!>", token); list.add(wordRule); 166 167 168 token= getToken(IJavaColorConstants.JAVADOC_LINK); 170 list.add(new SingleLineRule("{@link", "}", token)); list.add(new SingleLineRule("{@value", "}", token)); 173 174 list.add(new WhitespaceRule(new JavaWhitespaceDetector())); 176 177 178 list.addAll(super.createRules()); 179 return list; 180 } 181 182 185 protected List createMatchers() { 186 List list= super.createMatchers(); 187 188 final IToken token= getToken(IJavaColorConstants.JAVADOC_KEYWORD); 190 WordMatcher matcher= new CombinedWordRule.WordMatcher() { 191 public IToken evaluate(ICharacterScanner scanner, CharacterBuffer word) { 192 int length= word.length(); 193 if (length > 1 && word.charAt(0) == '@') { 194 int i= 0; 195 try { 196 for (; i <= length; i++) 197 scanner.unread(); 198 int c= scanner.read(); 199 i--; 200 if (c == '*' || Character.isWhitespace((char)c)) { 201 scanner.unread(); 202 return token; 203 } 204 } finally { 205 for (; i >= 0; i--) 206 scanner.read(); 207 } 208 } 209 return Token.UNDEFINED; 210 } 211 }; 212 list.add(matcher); 213 214 return list; 215 } 216 } 217 218 219 | Popular Tags |