1 19 20 package org.netbeans.editor; 21 22 import java.util.Collections ; 23 import java.util.List ; 24 import javax.swing.event.DocumentEvent ; 25 import javax.swing.text.AbstractDocument ; 26 import javax.swing.text.BadLocationException ; 27 import javax.swing.text.Document ; 28 import javax.swing.text.Element ; 29 import javax.swing.undo.AbstractUndoableEdit ; 30 import org.openide.ErrorManager; 31 32 45 public abstract class SyntaxUpdateTokens { 46 47 55 public static List getTokenInfoList(DocumentEvent evt) { 56 if (!(evt instanceof BaseDocumentEvent)) { 57 return Collections.EMPTY_LIST; 58 } 59 60 return ((BaseDocumentEvent)evt).getSyntaxUpdateTokenList(); 61 } 62 63 72 public static List getTokenInfoList(Document doc) { 73 SyntaxUpdateTokens suTokens = (SyntaxUpdateTokens)doc.getProperty(SyntaxUpdateTokens.class); 74 75 if (suTokens == null || !(doc instanceof BaseDocument)) { 76 return Collections.EMPTY_LIST; 77 } 78 79 List tokenList; 80 BaseDocument bdoc = (BaseDocument)doc; 81 bdoc.readLock(); 82 try { 83 suTokens.syntaxUpdateStart(); 84 try { 85 bdoc.getSyntaxSupport().tokenizeText( 86 new AllTokensProcessor(suTokens), 0, bdoc.getLength(), true); 87 } catch (BadLocationException e) { 88 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 89 } finally { 90 tokenList = suTokens.syntaxUpdateEnd(); 91 } 92 93 } finally { 94 bdoc.readUnlock(); 95 } 96 return tokenList; 97 } 98 99 105 public abstract void syntaxUpdateStart(); 106 107 118 public abstract List syntaxUpdateEnd(); 119 120 126 public abstract void syntaxUpdateToken(TokenID id, TokenContextPath contextPath, int offset, int length); 127 128 129 public class TokenInfo { 130 131 private final TokenID id; 132 133 private final TokenContextPath contextPath; 134 135 private final int offset; 136 137 private final int length; 138 139 public TokenInfo(TokenID id, TokenContextPath contextPath, int offset, int length) { 140 this.id = id; 141 this.contextPath = contextPath; 142 this.offset = offset; 143 this.length = length; 144 } 145 146 public final TokenID getID() { 147 return id; 148 } 149 150 public final TokenContextPath getContextPath() { 151 return contextPath; 152 } 153 154 public final int getOffset() { 155 return offset; 156 } 157 158 public int getLength() { 159 return length; 160 } 161 162 public String toString() { 163 return "id=" + id + ", ctx=" + contextPath + ", off=" + offset + ", len=" + length; } 165 166 } 167 168 static final class AllTokensProcessor implements TokenProcessor { 169 170 private SyntaxUpdateTokens suTokens; 171 172 private int bufferStartOffset; 173 174 AllTokensProcessor(SyntaxUpdateTokens suTokens) { 175 this.suTokens = suTokens; 176 } 177 178 public void nextBuffer(char[] buffer, int offset, int len, int startPos, int preScan, boolean lastBuffer) { 179 bufferStartOffset = startPos - offset; 180 } 181 182 public boolean token(TokenID tokenID, TokenContextPath tokenContextPath, int tokenBufferOffset, int tokenLength) { 183 suTokens.syntaxUpdateToken(tokenID, tokenContextPath, tokenBufferOffset, tokenLength); 184 return true; 185 } 186 187 public int eot(int offset) { 188 return 0; 189 } 190 191 } 192 193 194 195 } 196 | Popular Tags |