1 19 package org.netbeans.modules.retouche.editor.hyperlink; 20 21 import java.awt.Toolkit ; 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.util.Arrays ; 25 import java.util.HashSet ; 26 import java.util.Set ; 27 import javax.swing.text.Document ; 28 import org.netbeans.api.gsf.DeclarationFinder; 29 import org.netbeans.api.gsf.DeclarationFinder.DeclarationLocation; 30 import org.netbeans.api.gsf.OffsetRange; 31 import org.netbeans.api.gsf.GsfTokenId; 32 import org.netbeans.api.gsf.CancellableTask; 33 import org.netbeans.api.gsf.ElementHandle; 34 import org.netbeans.api.lexer.Token; 35 import org.netbeans.api.lexer.TokenHierarchy; 36 import org.netbeans.api.lexer.TokenId; 37 import org.netbeans.api.lexer.TokenSequence; 38 import org.netbeans.api.retouche.source.CompilationController; 39 import org.netbeans.api.retouche.source.Phase; 40 import org.netbeans.api.retouche.source.Source; 41 import org.netbeans.api.retouche.source.UiUtils; 42 import org.netbeans.modules.gsf.Language; 43 import org.netbeans.modules.gsf.LanguageRegistry; 44 import org.openide.awt.HtmlBrowser; 45 import org.openide.filesystems.FileObject; 46 import org.openide.loaders.DataObject; 47 48 49 52 public class GoToSupport { 53 private static final Set <GsfTokenId> USABLE_TOKEN_IDS = 54 new HashSet (Arrays.asList(GsfTokenId.IDENTIFIER )); 55 56 57 private GoToSupport() { 58 } 59 60 public static String performGoTo(final Document doc, final int offset) { 61 try { 62 final FileObject fo = getFileObject(doc); 63 64 if (fo == null) { 65 return null; 66 } 67 68 Source js = Source.forFileObject(fo); 69 final String [] result = new String [1]; 70 71 js.runUserActionTask(new CancellableTask<CompilationController>() { 72 public void cancel() { 73 } 74 75 public void run(CompilationController controller) 76 throws Exception { 77 if (controller.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { 78 return; 79 } 80 81 Language language = controller.getLanguage(); 82 83 if (language != null) { 84 DeclarationFinder finder = language.getDeclarationFinder(); 85 86 if (finder != null) { 87 Token[] tokens = new Token[1]; 88 getIdentifierSpan(doc, offset); 89 90 DeclarationLocation location = 91 finder.findDeclaration(controller, offset); 92 93 if (location != DeclarationLocation.NONE) { 94 URL url = location.getUrl(); 95 if (url != null) { 96 HtmlBrowser.URLDisplayer.getDefault().showURL(url); 97 } else { 98 UiUtils.open(location.getFileObject(), location.getOffset()); 99 100 String desc = "Description not yet implemented"; 101 result[0] = "<html><body>" + desc; 102 } 103 104 return; 105 } 106 } 107 } 108 109 Toolkit.getDefaultToolkit().beep(); 110 result[0] = null; 111 112 return; 113 } 114 }, true); 115 116 return result[0]; 117 } catch (IOException ioe) { 118 throw new IllegalStateException (ioe); 119 } 120 } 121 122 private static FileObject getFileObject(Document doc) { 123 DataObject od = (DataObject)doc.getProperty(Document.StreamDescriptionProperty); 124 125 return (od != null) ? od.getPrimaryFile() : null; 126 } 127 128 public int[] getHyperlinkSpan(Document doc, int offset) { 129 return GoToSupport.getIdentifierSpan(doc, offset); 130 } 131 132 public static int[] getIdentifierSpan(Document doc, int offset) { 133 FileObject fo = getFileObject(doc); 134 135 if (fo == null) { 136 return null; 138 } 139 140 Language language = LanguageRegistry.getInstance().getLanguageByMimeType(fo.getMIMEType()); 141 142 if (language == null) { 143 return null; 144 } 145 146 DeclarationFinder finder = language.getDeclarationFinder(); 147 148 if (finder == null) { 149 return null; 150 } 151 152 OffsetRange range = finder.getReferenceSpan(doc, offset); 153 if (range != OffsetRange.NONE) { 154 return new int[] { range.getStart(), range.getEnd() }; 155 } 156 157 return null; 158 } 159 } 160 | Popular Tags |