Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#20 add complex search infrastructure (without any search logic)
- Loading branch information
Showing
6 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
de.nittka.tooling.xarchive.ui/src/de/nittka/tooling/xarchive/ui/search/XarchiveSearch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package de.nittka.tooling.xarchive.ui.search; | ||
|
||
import org.eclipse.core.runtime.IProgressMonitor; | ||
import org.eclipse.emf.ecore.resource.Resource; | ||
import org.eclipse.xtext.resource.IResourceDescription; | ||
|
||
import de.nittka.tooling.xarchive.xarchive.Search; | ||
|
||
//this class implements the actual logic of checking whether a given resource description matches the search | ||
class XarchiveSearch { | ||
|
||
private Search search; | ||
|
||
public XarchiveSearch(Search search) { | ||
this.search=search; | ||
} | ||
|
||
public Resource getResource(){ | ||
return search.eResource(); | ||
} | ||
|
||
public boolean matches(IResourceDescription description, IProgressMonitor monitor){ | ||
//this is where the logic goes | ||
//make sure to check whether the monitor has been cancelled | ||
return true; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...a.tooling.xarchive.ui/src/de/nittka/tooling/xarchive/ui/search/XarchiveSearchHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package de.nittka.tooling.xarchive.ui.search; | ||
|
||
import javax.inject.Provider; | ||
|
||
import org.eclipse.core.commands.AbstractHandler; | ||
import org.eclipse.core.commands.ExecutionEvent; | ||
import org.eclipse.core.commands.ExecutionException; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.jface.text.ITextSelection; | ||
import org.eclipse.search.ui.NewSearchUI; | ||
import org.eclipse.xtext.EcoreUtil2; | ||
import org.eclipse.xtext.resource.EObjectAtOffsetHelper; | ||
import org.eclipse.xtext.resource.XtextResource; | ||
import org.eclipse.xtext.ui.editor.XtextEditor; | ||
import org.eclipse.xtext.ui.editor.utils.EditorUtils; | ||
import org.eclipse.xtext.util.concurrent.IUnitOfWork; | ||
|
||
import com.google.common.base.Predicates; | ||
import com.google.inject.Inject; | ||
|
||
import de.nittka.tooling.xarchive.xarchive.Search; | ||
|
||
public class XarchiveSearchHandler extends AbstractHandler { | ||
|
||
@Inject | ||
private EObjectAtOffsetHelper eObjectAtOffsetHelper; | ||
@Inject | ||
private Provider<XarchiveSearchQuery> queryProvider; | ||
|
||
//entry point adapted from FindReferencesHandler | ||
@Override | ||
public Object execute(ExecutionEvent event) throws ExecutionException { | ||
try { | ||
XtextEditor editor = EditorUtils.getActiveXtextEditor(event); | ||
if (editor != null) { | ||
final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); | ||
editor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() { | ||
@Override | ||
public void process(XtextResource state) throws Exception { | ||
EObject target = eObjectAtOffsetHelper.resolveContainedElementAt(state, selection.getOffset()); | ||
//at this point we just make sure that seach is only executed on search objects | ||
Search search=EcoreUtil2.getContainerOfType(target, Search.class); | ||
if(search !=null){ | ||
execute(search); | ||
} | ||
} | ||
}); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
//the essential parts from ReferenceQueryExecutor | ||
public void execute(Search search) { | ||
XarchiveSearchQuery query=queryProvider.get(); | ||
String searchName=search.getId()!=null?search.getId():"unnamed Xarchive search"; | ||
//dummy initialization of ReferenceQuery | ||
query.init(null, Predicates.alwaysTrue(), searchName); | ||
//the essential initialization of XarchiveSearchQuery (the actual search context) | ||
query.setSearch(search); | ||
NewSearchUI.activateSearchResultView(); | ||
NewSearchUI.runQueryInBackground(query); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...tka.tooling.xarchive.ui/src/de/nittka/tooling/xarchive/ui/search/XarchiveSearchQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package de.nittka.tooling.xarchive.ui.search; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.eclipse.core.runtime.IProgressMonitor; | ||
import org.eclipse.core.runtime.IStatus; | ||
import org.eclipse.core.runtime.OperationCanceledException; | ||
import org.eclipse.core.runtime.Status; | ||
import org.eclipse.xtext.resource.IContainer; | ||
import org.eclipse.xtext.resource.IContainer.Manager; | ||
import org.eclipse.xtext.resource.IEObjectDescription; | ||
import org.eclipse.xtext.resource.IReferenceDescription; | ||
import org.eclipse.xtext.resource.IResourceDescription; | ||
import org.eclipse.xtext.resource.IResourceDescriptions; | ||
import org.eclipse.xtext.resource.IResourceServiceProvider; | ||
import org.eclipse.xtext.resource.impl.DefaultReferenceDescription; | ||
import org.eclipse.xtext.resource.impl.ResourceDescriptionsProvider; | ||
import org.eclipse.xtext.ui.editor.findrefs.ReferenceQuery; | ||
import org.eclipse.xtext.ui.editor.findrefs.ReferenceSearchResult; | ||
import org.eclipse.xtext.util.IAcceptor; | ||
|
||
import de.nittka.tooling.xarchive.xarchive.Search; | ||
import de.nittka.tooling.xarchive.xarchive.XarchivePackage; | ||
|
||
//we derive from ReferenceQuery in order to reuse much of Xtext's search infrastructure | ||
//in particular result presentation in the Search View, which is bound to the | ||
//ISearchResult-implementation ReferenceSearchResult | ||
public class XarchiveSearchQuery extends ReferenceQuery { | ||
|
||
@Inject | ||
private ResourceDescriptionsProvider indexProvider; | ||
@Inject | ||
private IResourceServiceProvider serviceProvider; | ||
|
||
private XarchiveSearch search; | ||
|
||
void setSearch(Search search){ | ||
this.search=new XarchiveSearch(search); | ||
} | ||
|
||
//we override this method in order to provider our own search results | ||
//note that ReferenceSearchResult implements IAcceptor<IReferenceDescription> | ||
//so we just have to create suitable IReferenceDescriptions for our own | ||
//search results | ||
@Override | ||
public IStatus run(IProgressMonitor monitor) throws OperationCanceledException { | ||
ReferenceSearchResult result=(ReferenceSearchResult)getSearchResult(); | ||
result.reset(); | ||
internalRun(monitor, result); | ||
result.finish(); | ||
return (monitor.isCanceled()) ? Status.CANCEL_STATUS : Status.OK_STATUS; | ||
} | ||
|
||
private void internalRun(IProgressMonitor monitor, IAcceptor<IReferenceDescription> acceptor){ | ||
//our implementation searches the index, matching each visible IResourceDescription against the actual search logic | ||
IResourceDescriptions index = indexProvider.getResourceDescriptions(search.getResource()); | ||
Manager containerManager = serviceProvider.getContainerManager(); | ||
List<IContainer> visibleContainer = containerManager.getVisibleContainers(serviceProvider.getResourceDescriptionManager().getResourceDescription(search.getResource()), index); | ||
for (IContainer container : visibleContainer) { | ||
if(!monitor.isCanceled()){ | ||
Iterator<IResourceDescription> descs = container.getResourceDescriptions().iterator(); | ||
while(descs.hasNext() && !monitor.isCanceled()){ | ||
IResourceDescription desc = descs.next(); | ||
if(search.matches(desc, monitor)){ | ||
IReferenceDescription referenceDescription=getReferenceDescriptionForMatch(desc); | ||
//this will cause the reference description to be presented in the search view | ||
acceptor.accept(referenceDescription); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private IReferenceDescription getReferenceDescriptionForMatch(IResourceDescription desc){ | ||
//a dummy ReferenceDescription for the match is created - in our case one pointing to the text region associated to the Document object | ||
//we know that there is a corresponding EObjectDescription - we put it there; | ||
//the URI of the resource (desc.getURI()) would work, but would not provide a nice label for the "referencing" object | ||
Iterator<IEObjectDescription> potentialDoc = desc.getExportedObjectsByType(XarchivePackage.Literals.DOCUMENT).iterator(); | ||
if(potentialDoc.hasNext()){ | ||
IEObjectDescription doc = potentialDoc.next(); | ||
return new DefaultReferenceDescription(doc.getEObjectURI(), doc.getEObjectURI(), null, -1, null); | ||
}else{ | ||
throw new IllegalStateException(desc.getURI()+" does not expose an Xarchive document; this is an implementation error"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters