rewrite the method for finding references [message #550104] |
Thu, 29 July 2010 03:24  |
Eclipse User |
|
|
|
I try for my grammar to re-write the method for finding references.
package org.xtext.example.mydsl.ui;
import java.util.Iterator;
import org.apache.log4j.Logger;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.IResourceDescription;
import org.eclipse.xtext.resource.IResourceDescriptions;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.ui.editor.findrefs.FindReferencesHandler;
import org.eclipse.xtext.ui.editor.findrefs.Messages;
import org.eclipse.xtext.ui.editor.findrefs.ReferenceQuery;
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
import org.eclipse.xtext.ui.editor.utils.EditorUtils;
import org.eclipse.xtext.ui.resource.IStorage2UriMapper;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
import org.xtext.example.mydsl.myDsc.QualifiedName;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import com.google.inject.Provider;
public class MyDscFindReferencesHandler extends org.eclipse.xtext.ui.editor.findrefs.FindReferencesHandler
{
static
{
//System.out.println("MyDscFindReferencesHandler loaded");
}
private class EObjectResolver1 implements IUnitOfWork<IEObjectDescription, XtextResource> {
private final ITextSelection selection;
private EObjectResolver1(ITextSelection selection) {
this.selection = selection;
}
public IEObjectDescription exec(XtextResource state) throws Exception {
EObject element = EObjectAtOffsetHelper.resolveElementAt(state, selection.getOffset(), null);
if (element != null) {
final URI eObjectURI = EcoreUtil.getURI(element);
IResourceDescription resourceDescription = resourceDescriptions.getResourceDescription(eObjectURI
.trimFragment());
if (resourceDescription != null) {
System.out.println("res description ="+resourceDescription);
Iterator<IEObjectDescription> eObjectDescriptions = Iterables.filter(
resourceDescription.getExportedObjects(), new Predicate<IEObjectDescription>() {
public boolean apply(IEObjectDescription input) {
return input.getEObjectURI().equals(eObjectURI);
}
}).iterator();
if (eObjectDescriptions.hasNext()) {
return eObjectDescriptions.next();
}
}
}
return null;
}
}
@Inject
private IStorage2UriMapper storage2UriMapper;
@Inject
private Provider<ReferenceQuery> queryProvider;
@Inject
private IResourceDescriptions resourceDescriptions;
private static final Logger LOG = Logger.getLogger(FindReferencesHandler.class);
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
EObjectResolver1 er=new EObjectResolver1(selection);
IEObjectDescription eObjectDescription = editor.getDocument().readOnly(new EObjectResolver1(selection));
System.out.println(eObjectDescription);
} catch (Exception e) {
//LOG.error(Messages.FindReferencesHandler_3, e);
}
return null;
}
}
After running it i get this:
res description =org.eclipse.xtext.builder.builderState.impl.ResourceDescriptionImpl@13ba66d (URI: platform:/resource/DscProject/nice_try.dsc, importedNames: null)
because obviously
resourceDescription.getExportedObjects() returns null.
My grammar looks something like this:
Config: ( property+=Property | rule+=Rule | importpack+=ImportPackage)* ;
Property: selec1=Selector Force? ':' (herit=inheritage | expr1=Expr );
Rule: selec2=Selector '{' (prop1+=Property)* '}';
ImportPackage: 'import' namepackage+=ID* ;
Selector
:
id_selector1=QualifiedName
|
id_selector2=QualifiedName1
;
Expr
: value1+=Value (OPERATOR value1+=Value)*
And the text from editor :
blBorder : lineBorder(black, 1)
noBorder : emptyBorder()
uiPackage : "com.dalim.skin.component."
//-------------- CONST
Base
{
bg : rgb(45, 45, 45) //background color
fg : white //foreground color usually used for text
ctrlBg : Base.bg //background color of control components (Button, CheckBox, ComboBox, ect...)
ctrlFg : Base.fg //background color of control components (Button, CheckBox, ComboBox, ect...)
light : Base.bg + 27 //alternate color light
dark : Base.bg + 19 //alternate color dark
focus : black
fontSize : 10
select : #239fff //selection background color
font : font("Lucida Grande", Base.fontSize)
disabled : midColor(Base.fg, Base.bg) - 30
disabledOpacity : 45 // percentage
}
Panel
{
bezelBackground : Base.bg - 27
bezelArcSize : 20
}
PanelUI : uiPackage + "PanelUI"
let's say i wanna find references for uiPackage.
Someone can give me an idea of what i should modify/ rewrite in the exec /execute functions to get all places where uiPackage is ?
|
|
|
|
Re: rewrite the method for finding references [message #550122 is a reply to message #550118] |
Thu, 29 July 2010 04:37   |
Eclipse User |
|
|
|
Hi Iuliana,
I'ld strongly recommend to take a step back and revise your grammar. If
you use cross links and implement a proper scoping for them, everything
else will work out of the box.
Furthermore please note, that each and every default implementation in
Xtext relies on a structural feature 'name' to mark an semantic object
as identifyable (read: exported, named, whatever).
Please refer to the docs for detailed information about linking, scoping
and qualified names.
Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Am 29.07.10 10:22, schrieb iuliana_1985@personal.ro:
> I advanced a little bit.
> But it's still not pointing to all references in the same text file.
>
>
> package org.xtext.example.mydsl.ui;
>
> import j......
>
> public class MyDscFindReferencesHandler extends
> org.eclipse.xtext.ui.editor.findrefs.FindReferencesHandler
> {
>
>
> @Inject
> private IStorage2UriMapper storage2UriMapper;
>
> @Inject
> private Provider<ReferenceQuery> queryProvider;
>
> private static final Logger LOG =
> Logger.getLogger(FindReferencesHandler.class);
>
>
> @Override
> public Object execute(ExecutionEvent event) throws ExecutionException {
>
> try {
> XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
>
> ITextSelection selection = (ITextSelection)
> editor.getSelectionProvider().getSelection();
>
> IXtextDocument ix = ((XtextEditor) editor).getDocument();
> XtextResource xr = ix.readOnly(new MyUnitWorker());
> IResource resource = ((XtextEditor) editor).getResource();
> EObject model = xr.getContents().get(0);
> List<IEObjectDescription> elementList = new
> ArrayList<IEObjectDescription>();
> for (Iterator<EObject> iterator = xr.getAllContents(); iterator
> .hasNext();) { EObject element = iterator.next(); if(element instanceof
> QualifiedName)
>
> {
> if
> (((QualifiedName)element).getId_inst().get(0).equals(selecti on.getText()))
> {
> elementList.add(EObjectDescription .create(selection.getText(), element));
> }
> } } ReferenceQuery referenceQuery = queryProvider.get();
> for(int i=0; i<1;i++)
> {
>
> //ReferenceQuery referenceQuery = queryProvider.get();
> String label = Messages.FindReferencesHandler_labelPrefix +
> elementList.get(i).toString();
>
> Iterator<IStorage> storages =
> storage2UriMapper.getStorages(elementList.get(i).getEObjectU RI())
> .iterator();
>
> if (storages.hasNext()) {
> label += Messages.FindReferencesHandler_1 +
> storages.next().getFullPath().toString() +
> Messages.FindReferencesHandler_2;
> }
>
>
>
> referenceQuery.init(elementList.get(i).getEObjectURI(), label);
> NewSearchUI.activateSearchResultView();
> NewSearchUI.runQueryInBackground(referenceQuery);
>
>
> System.out.println(i+" "+label);
>
> }
>
>
> } catch (Exception e) {
> //LOG.error(Messages.FindReferencesHandler_3, e);
> }
> return null;
>
> }
>
>
> class MyUnitWorker implements IUnitOfWork<XtextResource, XtextResource> {
> public XtextResource exec(XtextResource state) throws Exception {
> return state;
> }
> }
>
>
>
>
> }
>
>
>
>
>
>
> I get something like
>
> Xtext References to uiPackage (/DscProject/nice_try.dsc)
>
> in "Search" tab.
|
|
|
|
|
|
Re: rewrite the method for finding references [message #552201 is a reply to message #551989] |
Wed, 11 August 2010 04:41  |
Eclipse User |
|
|
|
Hi!
You're trying to implement this functionality which already exists out-of-the-box with vanilla use of Xtext and which the Xtext team have worked so hard on to abstract away from you.
That means that the majority of the people doesn't know and, more importantly, even doesn't want to know how to be able to do it more-or-less from scratch and the rest wouldn't be interested to re-do that, but for one particular instance.
|
|
|
Powered by
FUDForum. Page generated in 0.27775 seconds