2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.languages.features;
44
import org.netbeans.api.languages.ASTPath;
45
import org.netbeans.api.languages.ParseException;
46
import org.netbeans.api.languages.ASTPath;
47
import org.netbeans.api.languages.ParserManager;
48
import org.netbeans.api.languages.SyntaxContext;
49
import org.netbeans.api.lexer.Token;
50
import org.netbeans.api.lexer.TokenHierarchy;
51
import org.netbeans.api.lexer.TokenSequence;
52
import org.netbeans.modules.editor.NbEditorDocument;
53
import org.netbeans.api.languages.Context;
54
import org.netbeans.modules.languages.Feature;
55
import org.netbeans.modules.languages.Language;
56
import org.netbeans.modules.languages.LanguagesManager;
57
import org.netbeans.modules.languages.ParserManagerImpl;
58
import org.netbeans.api.languages.SyntaxContext;
59
import org.netbeans.api.languages.ASTNode;
60
import org.netbeans.api.languages.ParseException;
61
import org.openide.cookies.EditorCookie;
62
import org.openide.loaders.DataObject;
63
import org.openide.text.Annotation;
64
import org.openide.text.DataEditorSupport;
65
import org.openide.text.Line;
66
import org.openide.text.Line.Part;
67
import org.openide.text.NbDocument;
74
public class ToolTipAnnotation extends Annotation {
76
static final String TOOLTIP = "TOOLTIP";
78
public String getShortDescription () {
80
Part lp = (Part) getAttachedAnnotatable();
81
if (lp == null) return null;
82
Line line = lp.getLine ();
83
DataObject dob = DataEditorSupport.findDataObject (line);
84
EditorCookie ec = dob.getCookie (EditorCookie.class);
85
NbEditorDocument document = (NbEditorDocument) ec.getDocument ();
86
String mimeType = (String) document.getProperty ("mimeType");
87
int offset = NbDocument.findLineOffset (
89
lp.getLine ().getLineNumber ()
91
TokenHierarchy tokenHierarchy = TokenHierarchy.get (document);
92
if (tokenHierarchy == null) return null;
93
Language l = LanguagesManager.getDefault ().getLanguage (mimeType);
96
TokenSequence tokenSequence = tokenHierarchy.tokenSequence ();
97
tokenSequence.move (offset);
98
if (!tokenSequence.moveNext() && !tokenSequence.movePrevious()) return null;
99
Token token = tokenSequence.token ();
100
Feature tooltip = l.getFeature (TOOLTIP, token.id ().name ());
101
if (tooltip != null) {
102
String s = c ((String) tooltip.getValue (Context.create (document, offset)));
106
document.readUnlock ();
110
ParserManager parserManager = ParserManagerImpl.get (document);
111
if (parserManager == null) {
114
ast = parserManager.getAST ();
115
} catch (ParseException ex) {
116
ast = ex.getASTNode ();
118
if (ast == null) return null;
119
ASTPath path = ast.findPath (offset);
120
if (path == null) return null;
121
int i, k = path.size ();
122
for (i = 0; i < k; i++) {
123
ASTPath p = path.subPath (i);
124
Feature tooltip = l.getFeature (TOOLTIP, p);
125
if (tooltip == null) continue;
126
String s = c ((String) tooltip.getValue (SyntaxContext.create (document, p)));
129
} catch (ParseException ex) {
134
public String getAnnotationType () {
135
return null; // Currently return null annotation type
138
private static String c (String s) {
139
if (s == null) return null;
140
s = s.replace ("\\n", "\n");
141
s = s.replace ("\\r", "\r");
142
s = s.replace ("\\t", "\t");