1 19 20 package org.netbeans.modules.languages.features; 21 22 import org.netbeans.modules.languages.parser.TokenInput; 23 import org.netbeans.api.languages.ASTToken; 24 import org.netbeans.api.lexer.Token; 25 import org.netbeans.api.lexer.TokenHierarchy; 26 import org.netbeans.api.lexer.TokenSequence; 27 import org.netbeans.api.languages.ASTToken; 28 import org.openide.text.NbDocument; 29 30 import javax.swing.text.StyledDocument ; 31 import java.util.ArrayList ; 32 import java.util.List ; 33 import java.util.Set ; 34 35 36 40 public class EditorTokenInput extends TokenInput { 41 42 private TokenSequence tokenSequence; 43 private List tokens = new ArrayList (); 44 private int index = 0; 45 private Set filter; 46 private StyledDocument doc; 47 private String mimeType; 48 49 50 public static EditorTokenInput create ( 51 Set filter, 52 StyledDocument doc 53 ) { 54 return new EditorTokenInput (filter, doc); 55 } 56 57 private EditorTokenInput ( 58 Set filter, 59 StyledDocument doc 60 ) { 61 tokenSequence = TokenHierarchy.get (doc).tokenSequence (); 62 this.filter = filter; 63 this.doc = doc; 64 mimeType = tokenSequence.language ().mimeType (); 65 } 66 67 public ASTToken next (int i) { 68 while (index + i - 1 >= tokens.size ()) { 69 ASTToken token = nextToken (); 70 if (token == null) return null; 71 tokens.add (token); 72 } 73 return (ASTToken) tokens.get (index + i - 1); 74 } 75 76 private ASTToken nextToken () { 77 do { 78 if (!tokenSequence.moveNext ()) return null; 79 } while ( 80 filter.contains ( 81 tokenSequence.token ().id ().name () 82 ) 83 ); 84 Token token = tokenSequence.token (); 85 return ASTToken.create ( 86 tokenSequence.language ().mimeType (), 87 token.id ().name (), 88 token.text ().toString (), 89 tokenSequence.offset () 90 ); 91 } 92 93 public boolean eof () { 94 return next (1) == null; 95 } 96 97 public int getIndex () { 98 return index; 99 } 100 101 public int getOffset () { 102 ASTToken t = null; 103 if (eof ()) { 104 if (getIndex () == 0) return 0; 105 t = ((ASTToken) tokens.get (tokens.size () - 1)); 106 return t.getOffset () + t.getLength (); 107 } else { 108 t = (ASTToken) next (1); 109 return t.getOffset (); 110 } 111 } 112 113 public ASTToken read () { 114 ASTToken next = next (1); 115 index++; 116 return next; 117 } 118 119 public void setIndex (int index) { 120 this.index = index; 121 } 122 123 public String getString (int from) { 124 throw new InternalError (); 125 } 126 127 public String toString () { 128 int offset = next (1) == null ? 129 doc.getLength () : next (1).getOffset (); 130 int lineNumber = NbDocument.findLineNumber (doc, offset); 131 return (String ) doc.getProperty ("title") + ":" + 132 (lineNumber + 1) + "," + 133 (offset - NbDocument.findLineOffset (doc, lineNumber) + 1); 134 } 151 } 152 | Popular Tags |