]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/search/SCLSearchQuery.java
9f8ef5a2430f7106e018ebf3ba861b0a4e380cd0
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / search / SCLSearchQuery.java
1 package org.simantics.scl.ui.search;
2
3 import java.util.ArrayList;
4
5 import org.eclipse.core.runtime.IProgressMonitor;
6 import org.eclipse.core.runtime.IStatus;
7 import org.eclipse.core.runtime.OperationCanceledException;
8 import org.eclipse.core.runtime.Status;
9 import org.eclipse.search.internal.ui.text.SearchResultUpdater;
10 import org.eclipse.search.ui.ISearchQuery;
11 import org.eclipse.search.ui.ISearchResult;
12 import org.eclipse.search.ui.text.Match;
13 import org.simantics.scl.compiler.common.names.Name;
14 import org.simantics.scl.compiler.errors.Failable;
15 import org.simantics.scl.compiler.module.Module;
16 import org.simantics.scl.compiler.module.debug.ModuleDebugInfo;
17 import org.simantics.scl.compiler.module.debug.SymbolReference;
18 import org.simantics.scl.compiler.module.repository.ModuleRepository;
19 import org.simantics.scl.osgi.SCLOsgi;
20
21 import gnu.trove.procedure.TObjectProcedure;
22
23 public class SCLSearchQuery implements ISearchQuery {
24
25     private SCLSearchResult result;
26     private String moduleName;
27     private Name valueName;
28
29     public SCLSearchQuery(Name valueName, String localModuleName) {
30         this.valueName = valueName;
31         this.moduleName = localModuleName;
32     }
33     
34     @Override
35     public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
36         SCLSearchResult current = (SCLSearchResult) getSearchResult();
37         current.removeAll();
38         ModuleRepository repo = SCLOsgi.MODULE_REPOSITORY;
39         Name localName = Name.create(moduleName, valueName.name);
40         repo.getSourceRepository().forAllModules(new TObjectProcedure<String>() {
41             
42             @Override
43             public boolean execute(String moduleName) {
44                 Failable<Module> failableModule = repo.getModule(moduleName);
45                 if (failableModule.didSucceed()) {
46                     Module module = failableModule.getResult();
47                     ModuleDebugInfo info = module.getModuleDebugInfo();
48                     if (info != null) {
49                         ArrayList<SymbolReference> results = info.symbolReferences;
50                         for (SymbolReference ref : results) {
51                             if (ref.referred.equals(valueName) || ref.referred.equals(localName)) {
52                                 result.addMatch(new Match(ref, Match.UNIT_LINE, -1, 1));
53                             }
54                         }
55                     }
56                 }
57                 return true;
58             }
59         });
60         return Status.OK_STATUS;
61     }
62
63     @Override
64     public String getLabel() {
65         return "Search references for ";
66     }
67
68     @Override
69     public boolean canRerun() {
70         return true;
71     }
72
73     @Override
74     public boolean canRunInBackground() {
75         return true;
76     }
77
78     @Override
79     public ISearchResult getSearchResult() {
80         if (result == null) {
81             result = new SCLSearchResult(this);
82             new SearchResultUpdater(result);
83         }
84         return result;
85     }
86
87     public String getValueName() {
88         return valueName.toString();
89     }
90
91 }