1 19 20 package org.netbeans.api.languages; 21 22 import javax.swing.text.Document ; 23 import javax.swing.text.JTextComponent ; 24 import org.netbeans.api.languages.ASTToken; 25 import org.netbeans.api.lexer.TokenHierarchy; 26 import org.netbeans.api.lexer.TokenSequence; 27 import org.netbeans.modules.editor.NbEditorUtilities; 28 import org.openide.cookies.EditorCookie; 29 import org.openide.loaders.DataObject; 30 31 32 38 public abstract class SyntaxContext extends Context { 39 40 41 46 public abstract ASTPath getASTPath (); 47 48 53 public static SyntaxContext create (Document doc, ASTPath path) { 54 return new CookieImpl (doc, path); 55 } 56 57 private static class CookieImpl extends SyntaxContext { 58 59 private Document doc; 60 private ASTPath path; 61 private JTextComponent component; 62 private TokenSequence tokenSequence; 63 64 CookieImpl ( 65 Document doc, 66 ASTPath path 67 ) { 68 this.doc = doc; 69 this.path = path; 70 } 71 72 public JTextComponent getJTextComponent () { 73 if (component == null) { 74 DataObject dob = NbEditorUtilities.getDataObject (doc); 75 EditorCookie ec = (EditorCookie) dob.getLookup ().lookup (EditorCookie.class); 76 if (ec.getOpenedPanes ().length > 0) 77 component = ec.getOpenedPanes () [0]; 78 } 79 return component; 80 } 81 82 public ASTPath getASTPath () { 83 return path; 84 } 85 86 public Document getDocument () { 87 return doc; 88 } 89 90 public TokenSequence getTokenSequence () { 91 if (tokenSequence == null) { 92 TokenHierarchy th = TokenHierarchy.get (doc); 93 tokenSequence = th.tokenSequence (); 94 } 95 Object leaf = path.getLeaf (); 96 if (leaf instanceof ASTToken) 97 tokenSequence.move (((ASTToken) leaf).getOffset ()); 98 else 99 tokenSequence.move (((ASTNode) leaf).getOffset ()); 100 tokenSequence.moveNext(); 101 return tokenSequence; 102 } 103 } 104 } 105 106 107 | Popular Tags |