1 19 package org.netbeans.modules.retouche.editor.semantic; 20 21 import java.io.IOException ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.HashSet ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import javax.swing.text.Document ; 30 import org.netbeans.api.gsf.OffsetRange; 31 import org.netbeans.api.gsf.Parser; 32 import org.netbeans.api.gsf.ColoringAttributes; 33 import org.netbeans.api.gsf.SemanticAnalyzer; 34 import org.netbeans.api.retouche.source.CompilationInfo; 35 import org.netbeans.modules.editor.highlights.spi.Highlight; 37 import org.netbeans.modules.editor.highlights.spi.Highlighter; 38 import org.openide.ErrorManager; 39 import org.openide.cookies.EditorCookie; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileUtil; 42 import org.openide.loaders.DataObject; 43 44 45 54 public class SemanticHighlighter extends ScanningCancellableTask<CompilationInfo> { 55 56 private FileObject file; 57 58 59 SemanticHighlighter(FileObject file) { 60 this.file = file; 61 } 62 63 public Document getDocument() { 64 try { 65 DataObject d = DataObject.find(file); 66 EditorCookie ec = (EditorCookie) d.getCookie(EditorCookie.class); 67 68 if (ec == null) 69 return null; 70 71 return ec.getDocument(); 72 } catch (IOException e) { 73 Logger.global.log(Level.INFO, "SemanticHighlighter: Cannot find DataObject for file: " + FileUtil.getFileDisplayName(file), e); 74 return null; 75 } 76 } 77 78 public @Override void run(CompilationInfo info) { 79 resume(); 80 81 Document doc = getDocument(); 82 83 if (doc == null) { 84 Logger.global.log(Level.INFO, "SemanticHighlighter: Cannot get document!"); 85 return; 86 } 87 88 Set <Highlight> highlights = process(info, doc); 89 90 if (isCancelled()) 91 return; 92 93 Highlighter.getDefault().setHighlights(file, "semantic", highlights); 94 OccurrencesMarkProvider.get(doc).setSematic(highlights); 95 } 96 97 98 Set <Highlight> process(CompilationInfo info, final Document doc) { 99 if (isCancelled()) 100 return Collections.emptySet(); 101 102 long start = System.currentTimeMillis(); 103 Set <Highlight> result = new HashSet (); 104 105 Parser parser = info.getParser(); 107 if (parser != null) { 108 SemanticAnalyzer task = parser.getSemanticAnalysisTask(); 109 if (task != null) { 110 try { 111 task.run(info); 112 } catch (Exception ex) { 113 ErrorManager.getDefault().notify(ex); 114 } 115 116 if (isCancelled()) { 117 task.cancel(); 118 return Collections.emptySet(); 119 } 120 121 Map <OffsetRange,ColoringAttributes> highlights = task.getHighlights(); 122 if (highlights != null) { 123 124 for (OffsetRange range : highlights.keySet()) { 125 if (isCancelled()) 126 return result; 127 128 ColoringAttributes colors = highlights.get(range); 129 Collection <ColoringAttributes> c = Collections.singletonList(colors); 130 Highlight h = Utilities.createHighlight(doc, range.getStart(), range.getEnd(), c, null); 131 132 if (h != null) { 133 result.add(h); 134 } 135 } 136 } 137 } 138 } 139 140 142 return result; 143 } 144 } 145 | Popular Tags |