]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Ctrl+Shift+G support for finding also expressions from properties 28/1628/2
authorjsimomaa <jani.simomaa@gmail.com>
Thu, 22 Mar 2018 13:38:34 +0000 (15:38 +0200)
committerJani Simomaa <jani.simomaa@semantum.fi>
Thu, 22 Mar 2018 17:24:10 +0000 (19:24 +0200)
refs #7834

Change-Id: I24ba4188bbe3b2ba412a9e42fc762f21f76aca55

bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionItemFilter.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionView.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/UsedSCLExpressionsRequest.java [deleted file]
bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLExpressionTableEntry.java [moved from bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionTableEntry.java with 93% similarity]
bundles/org.simantics.scl.db/src/org/simantics/scl/db/UsedSCLExpressionsRequest.java [new file with mode: 0644]
bundles/org.simantics.scl.ui/META-INF/MANIFEST.MF
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/search/SCLSearchQuery.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/search/SCLSearchResultPage.java

index bcc8b02e637ff7bda34279c06e155ad1873cde6f..b644ac96b021592d7f531056dec5f4eab6ba87ed 100644 (file)
@@ -2,6 +2,7 @@ package org.simantics.modeling.ui.scl.expressions;
 
 import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.jface.viewers.ViewerFilter;
+import org.simantics.scl.db.SCLExpressionTableEntry;
 
 public class SCLExpressionItemFilter extends ViewerFilter {
 
index eeea9d586b895a5cd1f2af7a86b2001061605ded..245796c551ad76d629566772195b27edd2a5136c 100644 (file)
@@ -39,10 +39,16 @@ import org.simantics.Simantics;
 import org.simantics.db.Resource;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.modeling.ui.Activator;
+import org.simantics.scl.db.SCLExpressionTableEntry;
+import org.simantics.scl.db.UsedSCLExpressionsRequest;
 import org.simantics.ui.workbench.action.DefaultActions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class SCLExpressionView extends ViewPart {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(SCLExpressionView.class);
+
     private TableViewer tableViewer;
     private TableViewerColumn expressionColumn;
     private TableViewerColumn locationColumn;
@@ -158,7 +164,7 @@ public class SCLExpressionView extends ViewPart {
                     });
                     return Status.OK_STATUS;
                 } catch (DatabaseException e) {
-                    e.printStackTrace();
+                    LOGGER.error("Could not update SCL expressions", e);
                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not update SCL expressions", e);
                 }
             }
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/UsedSCLExpressionsRequest.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/UsedSCLExpressionsRequest.java
deleted file mode 100644 (file)
index 4e62af2..0000000
+++ /dev/null
@@ -1,90 +0,0 @@
-package org.simantics.modeling.ui.scl.expressions;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Set;
-import java.util.TreeSet;
-
-import org.simantics.Simantics;
-import org.simantics.db.ReadGraph;
-import org.simantics.db.Resource;
-import org.simantics.db.Statement;
-import org.simantics.db.common.request.UniqueRead;
-import org.simantics.db.exception.DatabaseException;
-import org.simantics.db.layer0.util.Layer0Utils;
-import org.simantics.layer0.Layer0;
-import org.simantics.modeling.ModelingUtils;
-import org.simantics.structural.stubs.StructuralResource2;
-
-public class UsedSCLExpressionsRequest extends UniqueRead<Collection<SCLExpressionTableEntry>> {
-
-    @Override
-    public Collection<SCLExpressionTableEntry> perform(ReadGraph graph) throws DatabaseException {
-        Collection<SCLExpressionTableEntry> result = new ArrayList<>();
-        Layer0 L0 = Layer0.getInstance(graph);
-        
-        Set<Resource> indexRoots = new TreeSet<Resource>();
-        for(Resource ontology : Layer0Utils.listOntologies(graph)) {
-            if (graph.isInstanceOf(ontology, L0.SharedOntology)) {
-                indexRoots.add(ontology);
-            }
-        }
-        
-        for(Resource child : graph.getObjects(Simantics.getProjectResource(), L0.ConsistsOf)) {
-            if (graph.isInstanceOf(child, L0.IndexRoot)) {
-                indexRoots.add(child);
-            }
-        }
-
-        for (Resource ontology : indexRoots) {
-//            List<Resource> modules = ModelingUtils.searchByTypeShallow(graph, ontology, L0.SCLModule);
-//            for (Resource module : modules) {
-//                String definition = graph.getPossibleRelatedValue2(module, L0.SCLModule_definition);
-//                Matcher matcher = pattern.matcher(definition);
-//                while (matcher.find()) {
-//                    int index = matcher.start();
-//                    int line = 1;
-//                    int pos = 0;
-//                    while ((pos = definition.indexOf("\n", pos) + 1) > 0 && pos <= index) {
-//                        line++;
-//                    }
-//                    String position = graph.getPossibleURI(module) + ":" + line;
-//                    int lineBeginIndex = definition.lastIndexOf("\n", index);
-//                    if (lineBeginIndex == -1) lineBeginIndex = 0;
-//                    int lineEndIndex = definition.indexOf("\n", index);
-//                    if (lineEndIndex <= 0) lineEndIndex = definition.length();
-//                    String content = definition.substring(lineBeginIndex, lineEndIndex).trim();
-//
-//                    result.add(new SCLExpressionTableEntry(content, position));
-//                }
-//            }
-            
-            StructuralResource2 STR = StructuralResource2.getInstance(graph);
-            
-            List<Resource> components = ModelingUtils.searchByTypeShallow(graph, ontology, STR.Component);
-            for (Resource component : components) {
-                for (Statement propertyStatement : graph.getStatements(component, L0.HasProperty)) {
-                    if (graph.isInstanceOf(propertyStatement.getObject(), L0.SCLValue)) {
-                        Resource sclValue = propertyStatement.getObject();
-                        String expression = graph.getPossibleRelatedValue2(sclValue, L0.SCLValue_expression);
-                        Resource source = graph.getPossibleObject(sclValue, L0.PropertyOf);
-                        if (source != null) {
-                            String uri = graph.getPossibleURI(source);
-                            String pred = graph.getRelatedValue2(propertyStatement.getPredicate(), L0.HasName);
-
-                            if (uri != null) {
-//                                SCLReporting.print();
-//                                SCLReporting.printError(expression + "\n");
-                                
-                                result.add(new SCLExpressionTableEntry(expression, uri + "#" + pred, source));
-                            }
-                        }
-                    }
-                }
-            }
-        }
-        return result;
-    }
-
-}
diff --git a/bundles/org.simantics.scl.db/src/org/simantics/scl/db/UsedSCLExpressionsRequest.java b/bundles/org.simantics.scl.db/src/org/simantics/scl/db/UsedSCLExpressionsRequest.java
new file mode 100644 (file)
index 0000000..60d42fe
--- /dev/null
@@ -0,0 +1,83 @@
+package org.simantics.scl.db;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Statement;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.QueryIndexUtils;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.layer0.Layer0;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class UsedSCLExpressionsRequest extends UniqueRead<Collection<SCLExpressionTableEntry>> {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(UsedSCLExpressionsRequest.class);
+
+    private static final String structuralResourceComponentURI = "http://www.simantics.org/Structural-1.2/Component";
+
+    @Override
+    public Collection<SCLExpressionTableEntry> perform(ReadGraph graph) throws DatabaseException {
+        Collection<SCLExpressionTableEntry> result = new ArrayList<>();
+        Layer0 L0 = Layer0.getInstance(graph);
+        
+        Set<Resource> indexRoots = new TreeSet<Resource>();
+        for(Resource ontology : Layer0Utils.listOntologies(graph)) {
+            if (graph.isInstanceOf(ontology, L0.SharedOntology)) {
+                indexRoots.add(ontology);
+            }
+        }
+        for(Resource child : graph.getObjects(Simantics.getProjectResource(), L0.ConsistsOf)) {
+            if (graph.isInstanceOf(child, L0.IndexRoot)) {
+                indexRoots.add(child);
+            }
+        }
+
+        Resource componentResource = graph.getPossibleResource(structuralResourceComponentURI);
+        if (componentResource != null) {
+            for (Resource ontology : indexRoots) {
+                List<Resource> components = QueryIndexUtils.searchByTypeShallow(graph, ontology, componentResource);
+                for (Resource component : components) {
+                    for (Statement propertyStatement : graph.getStatements(component, L0.HasProperty)) {
+                        if (graph.isInstanceOf(propertyStatement.getObject(), L0.SCLValue)) {
+                            Resource sclValue = propertyStatement.getObject();
+                            String expression = graph.getPossibleRelatedValue2(sclValue, L0.SCLValue_expression);
+                            Resource source = graph.getPossibleObject(sclValue, L0.PropertyOf);
+                            if (source != null) {
+                                String uri = graph.getPossibleURI(source);
+                                String pred = graph.getRelatedValue2(propertyStatement.getPredicate(), L0.HasName);
+    
+                                if (uri != null) {
+                                    result.add(new SCLExpressionTableEntry(expression, uri + "#" + pred, source));
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        } else {
+            LOGGER.info("{} is not available for finding expressions in component properties", structuralResourceComponentURI);
+        }
+        return result;
+    }
+
+    public static Collection<SCLExpressionTableEntry> execute() {
+        try {
+            return Simantics.getSession().syncRequest(new UsedSCLExpressionsRequest(), TransientCacheListener.instance());
+        } catch (DatabaseException e) {
+            e.printStackTrace();
+            return Collections.emptyList();
+        }
+    }
+
+}
index deaefad4d36dd6d93adbaca2a221476415f46f41..913c75d52867705bae39bdbe066eee2c85ef26de 100644 (file)
@@ -17,7 +17,9 @@ Require-Bundle: org.eclipse.ui.editors;bundle-version="3.6.0",
  com.ibm.icu,
  org.slf4j.api,
  org.eclipse.search;bundle-version="3.11.100",
- org.eclipse.ui.views
+ org.eclipse.ui.views,
+ org.simantics.scl.db;bundle-version="0.1.3",
+ org.simantics.ui
 Export-Package: org.simantics.scl.ui.console,
  org.simantics.scl.ui.editor,
  org.simantics.scl.ui.editor2,
index 9f8ef5a2430f7106e018ebf3ba861b0a4e380cd0..264cbc57a201098be5ea0e3249bca1d9a37eff92 100644 (file)
@@ -1,6 +1,7 @@
 package org.simantics.scl.ui.search;
 
 import java.util.ArrayList;
+import java.util.Collection;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
@@ -16,6 +17,8 @@ import org.simantics.scl.compiler.module.Module;
 import org.simantics.scl.compiler.module.debug.ModuleDebugInfo;
 import org.simantics.scl.compiler.module.debug.SymbolReference;
 import org.simantics.scl.compiler.module.repository.ModuleRepository;
+import org.simantics.scl.db.SCLExpressionTableEntry;
+import org.simantics.scl.db.UsedSCLExpressionsRequest;
 import org.simantics.scl.osgi.SCLOsgi;
 
 import gnu.trove.procedure.TObjectProcedure;
@@ -57,6 +60,14 @@ public class SCLSearchQuery implements ISearchQuery {
                 return true;
             }
         });
+        
+        Collection<SCLExpressionTableEntry> results = UsedSCLExpressionsRequest.execute();
+        for (SCLExpressionTableEntry entry : results) {
+            if (entry.getContent().contains(valueName.name)) {
+                result.addMatch(new Match(entry, Match.UNIT_LINE, -1, 1));
+            }
+        }
+        
         return Status.OK_STATUS;
     }
 
index 8a8c5059cd852834a63ffa5437724b00ed0bdcd0..bed4fb123591a2c98df61d3e6bb14618e7d8a4e0 100644 (file)
@@ -32,7 +32,9 @@ import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
 import org.simantics.scl.compiler.errors.Locations;
 import org.simantics.scl.compiler.module.debug.SymbolReference;
+import org.simantics.scl.db.SCLExpressionTableEntry;
 import org.simantics.scl.ui.editor2.OpenSCLDefinition;
+import org.simantics.ui.workbench.action.DefaultActions;
 
 public class SCLSearchResultPage extends AbstractTextSearchViewPage {
 
@@ -55,13 +57,23 @@ public class SCLSearchResultPage extends AbstractTextSearchViewPage {
     private static final ViewerComparator comparator = new ViewerComparator() {
         @Override
         public int compare(Viewer viewer, Object e1, Object e2) {
-            SymbolReference r1 = (SymbolReference)e1;
-            SymbolReference r2 = (SymbolReference)e2;
-            int c = r1.referrer.toString().compareTo(r2.referrer.toString());
-            if (c != 0) {
-                return c;
+            if (e1 instanceof SymbolReference && e2 instanceof SymbolReference) {
+                SymbolReference r1 = (SymbolReference)e1;
+                SymbolReference r2 = (SymbolReference)e2;
+                int c = r1.referrer.toString().compareTo(r2.referrer.toString());
+                if (c != 0) {
+                    return c;
+                } else {
+                    return Integer.compare(Locations.beginOf(r1.referenceLocation), Locations.beginOf(r2.referenceLocation));
+                }
+            } else if (e1 instanceof SymbolReference && e2 instanceof SCLExpressionTableEntry) {
+                return 1;
+            } else if (e2 instanceof SymbolReference && e1 instanceof SCLExpressionTableEntry) {
+                return -1;
             } else {
-                return Integer.compare(Locations.beginOf(r1.referenceLocation), Locations.beginOf(r2.referenceLocation));
+                SCLExpressionTableEntry r1 = (SCLExpressionTableEntry)e1;
+                SCLExpressionTableEntry r2 = (SCLExpressionTableEntry)e2;
+                return r1.getContent().compareTo(r2.getContent());
             }
         }
     };
@@ -88,15 +100,23 @@ public class SCLSearchResultPage extends AbstractTextSearchViewPage {
     protected void handleOpen(OpenEvent event) {
         Object selection = ((StructuredSelection)event.getSelection()).getFirstElement();
         if (selection != null) {
-            SymbolReference reference = (SymbolReference) selection;
-            OpenSCLDefinition.scheduleOpenDefinition(reference.referrer.module, reference.referenceLocation);
+            open(selection);
         }
     }
     
     @Override
     protected void showMatch(Match match, int currentOffset, int currentLength) throws PartInitException {
-        SymbolReference reference = (SymbolReference) match.getElement();
-        OpenSCLDefinition.openDefinition(reference.referrer.module, reference.referenceLocation);
+        open(match.getElement());
+    }
+    
+    private void open(Object selection) {
+        if (selection instanceof SymbolReference) {
+            SymbolReference reference = (SymbolReference) selection;
+            OpenSCLDefinition.scheduleOpenDefinition(reference.referrer.module, reference.referenceLocation);
+        } else if (selection instanceof SCLExpressionTableEntry) {
+            SCLExpressionTableEntry entry = (SCLExpressionTableEntry) selection;
+            DefaultActions.performDefaultAction(getControl().getDisplay().getActiveShell(), new StructuredSelection(entry.getResource()));
+        }
     }
 
     public static class SCLSearchResultContentProvider extends DecoratingStyledCellLabelProvider implements ITreeContentProvider, ILabelProvider {
@@ -289,8 +309,15 @@ public class SCLSearchResultPage extends AbstractTextSearchViewPage {
 
         @Override
         public String getText(Object element) {
-            SymbolReference ref = (SymbolReference) element;
-            return ref.referrer.toString();
+            if (element instanceof SymbolReference) {
+                SymbolReference ref = (SymbolReference) element;
+                return ref.referrer.toString();
+            } else if (element instanceof SCLExpressionTableEntry) {
+                SCLExpressionTableEntry entry = (SCLExpressionTableEntry) element;
+                return entry.getContent();
+            } else {
+                return "unsupported element " + String.valueOf(element);
+            }
         }
 
     }
@@ -319,8 +346,7 @@ public class SCLSearchResultPage extends AbstractTextSearchViewPage {
 
         @Override
         public StyledString getStyledText(Object element) {
-            SymbolReference ref = (SymbolReference) element;
-            return new StyledString(ref.referrer.toString()); //+ " " + ref.referred + " " + ref.referenceLocation);
+            return new StyledString(getText(element)); //+ " " + ref.referred + " " + ref.referenceLocation);
         }
 
         @Override
@@ -340,7 +366,15 @@ public class SCLSearchResultPage extends AbstractTextSearchViewPage {
 
         @Override
         public String getText(Object element) {
-            return null;
+            if (element instanceof SymbolReference) {
+                SymbolReference ref = (SymbolReference) element;
+                return ref.referrer.toString();
+            } else if (element instanceof SCLExpressionTableEntry) {
+                SCLExpressionTableEntry entry = (SCLExpressionTableEntry) element;
+                return entry.getContent();
+            } else {
+                return "unsupported element " + String.valueOf(element);
+            }
         }
         
     }