From: jsimomaa Date: Thu, 22 Mar 2018 13:38:34 +0000 (+0200) Subject: Ctrl+Shift+G support for finding also expressions from properties X-Git-Tag: v1.43.0~136^2~525 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=4d31ea9ef9aa6e2d5dc188b64dc76bbefc50bb88;p=simantics%2Fplatform.git Ctrl+Shift+G support for finding also expressions from properties refs #7834 Change-Id: I24ba4188bbe3b2ba412a9e42fc762f21f76aca55 --- diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionItemFilter.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionItemFilter.java index bcc8b02e6..b644ac96b 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionItemFilter.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionItemFilter.java @@ -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 { diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionView.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionView.java index eeea9d586..245796c55 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionView.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionView.java @@ -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 index 4e62af237..000000000 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/UsedSCLExpressionsRequest.java +++ /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> { - - @Override - public Collection perform(ReadGraph graph) throws DatabaseException { - Collection result = new ArrayList<>(); - Layer0 L0 = Layer0.getInstance(graph); - - Set indexRoots = new TreeSet(); - 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 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 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.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionTableEntry.java b/bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLExpressionTableEntry.java similarity index 93% rename from bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionTableEntry.java rename to bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLExpressionTableEntry.java index 6cfb46023..fa5f0e434 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/expressions/SCLExpressionTableEntry.java +++ b/bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLExpressionTableEntry.java @@ -1,4 +1,4 @@ -package org.simantics.modeling.ui.scl.expressions; +package org.simantics.scl.db; import org.simantics.db.Resource; 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 index 000000000..60d42fe31 --- /dev/null +++ b/bundles/org.simantics.scl.db/src/org/simantics/scl/db/UsedSCLExpressionsRequest.java @@ -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> { + + 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 perform(ReadGraph graph) throws DatabaseException { + Collection result = new ArrayList<>(); + Layer0 L0 = Layer0.getInstance(graph); + + Set indexRoots = new TreeSet(); + 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 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 execute() { + try { + return Simantics.getSession().syncRequest(new UsedSCLExpressionsRequest(), TransientCacheListener.instance()); + } catch (DatabaseException e) { + e.printStackTrace(); + return Collections.emptyList(); + } + } + +} diff --git a/bundles/org.simantics.scl.ui/META-INF/MANIFEST.MF b/bundles/org.simantics.scl.ui/META-INF/MANIFEST.MF index deaefad4d..913c75d52 100644 --- a/bundles/org.simantics.scl.ui/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.scl.ui/META-INF/MANIFEST.MF @@ -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, diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/search/SCLSearchQuery.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/search/SCLSearchQuery.java index 9f8ef5a24..264cbc57a 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/search/SCLSearchQuery.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/search/SCLSearchQuery.java @@ -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 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; } diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/search/SCLSearchResultPage.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/search/SCLSearchResultPage.java index 8a8c5059c..bed4fb123 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/search/SCLSearchResultPage.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/search/SCLSearchResultPage.java @@ -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); + } } }