1 19 20 package org.netbeans.modules.languages.features; 21 22 import org.netbeans.api.languages.ASTPath; 23 import org.netbeans.api.languages.LanguagesManager; 24 import org.netbeans.api.languages.ParseException; 25 import org.netbeans.api.languages.ASTPath; 26 import org.netbeans.api.languages.SyntaxContext; 27 import javax.swing.JEditorPane ; 28 import org.netbeans.api.lexer.Token; 29 import org.netbeans.api.lexer.TokenHierarchy; 30 import org.netbeans.api.lexer.TokenSequence; 31 import org.netbeans.modules.editor.NbEditorDocument; 32 import org.netbeans.api.languages.Context; 33 import org.netbeans.modules.languages.Feature; 34 import org.netbeans.modules.languages.Language; 35 import org.netbeans.modules.languages.LanguagesManagerImpl; 36 import org.netbeans.modules.languages.ParserManagerImpl; 37 import org.netbeans.api.languages.SyntaxContext; 38 import org.netbeans.api.languages.ASTNode; 39 import org.netbeans.api.languages.ParseException; 40 import org.openide.cookies.EditorCookie; 41 import org.openide.loaders.DataObject; 42 import org.openide.text.Annotation; 43 import org.openide.text.DataEditorSupport; 44 import org.openide.text.Line; 45 import org.openide.text.Line.Part; 46 import org.openide.text.NbDocument; 47 48 49 53 public class ToolTipAnnotation extends Annotation { 54 55 56 public String getShortDescription () { 57 try { 58 Part lp = (Part) getAttachedAnnotatable(); 59 Line line = lp.getLine (); 60 DataObject dob = DataEditorSupport.findDataObject (line); 61 EditorCookie ec = (EditorCookie) dob.getCookie (EditorCookie.class); 62 NbEditorDocument doc = (NbEditorDocument) ec.getDocument (); 63 String mimeType = (String ) doc.getProperty ("mimeType"); 64 int offset = NbDocument.findLineOffset ( 65 ec.getDocument (), 66 lp.getLine ().getLineNumber () 67 ) + lp.getColumn (); 68 TokenHierarchy tokenHierarchy = TokenHierarchy.get (doc); 69 TokenSequence tokenSequence = tokenHierarchy.tokenSequence (); 70 tokenSequence.move (offset); 71 if (!tokenSequence.moveNext() && !tokenSequence.movePrevious()) return null; 72 Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).getLanguage 73 (mimeType); 74 Token token = tokenSequence.token (); 75 Feature tooltip = l.getFeature (Language.TOOLTIP, token.id ().name ()); 76 if (tooltip != null) { 77 String s = c ((String ) tooltip.getValue (Context.create (doc, tokenSequence))); 78 return s; 79 } 80 ASTNode ast = null; 81 try { 82 ast = ParserManagerImpl.get (doc).getAST (); 83 } catch (ParseException ex) { 84 ast = ex.getASTNode (); 85 } 86 if (ast == null) return null; 87 ASTPath path = ast.findPath (offset); 88 if (path == null) return null; 89 int i, k = path.size (); 90 for (i = 0; i < k; i++) { 91 ASTPath p = path.subPath (i); 92 tooltip = l.getFeature (Language.TOOLTIP, p); 93 if (tooltip == null) continue; 94 String s = c ((String ) tooltip.getValue (SyntaxContext.create (doc, p))); 95 return s; 96 } 97 } catch (ParseException ex) { 98 } 99 return null; 100 } 101 102 public String getAnnotationType () { 103 return null; } 105 106 private static String c (String s) { 107 if (s == null) return null; 108 s = s.replace ("\\n", "\n"); 109 s = s.replace ("\\r", "\r"); 110 s = s.replace ("\\t", "\t"); 111 return s; 112 } 113 } 114 115 | Popular Tags |