Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » rewrite the method for finding references
rewrite the method for finding references [message #550104] Thu, 29 July 2010 07:24 Go to next message
No real name is currently offline No real name
Messages: 36
Registered: July 2010
Member
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 #550118 is a reply to message #550104] Thu, 29 July 2010 08:22 Go to previous messageGo to next message
No real name is currently offline No real name
Messages: 36
Registered: July 2010
Member
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(selection.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).getEObjectURI())
						.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 #550122 is a reply to message #550118] Thu, 29 July 2010 08:37 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2908
Registered: July 2009
Senior Member
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 #550134 is a reply to message #550104] Thu, 29 July 2010 09:01 Go to previous messageGo to next message
No real name is currently offline No real name
Messages: 36
Registered: July 2010
Member
I don't use cross references.

I did the linking, high-lighting and auto-completion without them and i'm sure this feature can be also done. i just need some little help.

I have the good URI for each reference of a certain name in file ...

			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(selection.getText()))
                    	{
                    		elementList.add(EObjectDescription .create(selection.getText(), element));
                    		
                    		//System.out.println(elementList.get(elementList.size()-1).getEObjectURI());
                    		
                    	}                    	
                    }    
            } 
            
            
            
            for(int i=0; i<2;i++)
            {           	

            	ReferenceQuery referenceQuery = queryProvider.get();
				String label = Messages.FindReferencesHandler_labelPrefix + elementList.get(i).toString();
				
				Iterator<IStorage> storages = storage2UriMapper.getStorages(elementList.get(i).getEObjectURI())
						.iterator();
						
				if (storages.hasNext()) {
					label += Messages.FindReferencesHandler_1 + storages.next().getFullPath().toString() + Messages.FindReferencesHandler_2;

				
				}

				System.out.println(elementList.get(i).getEObjectURI());
				referenceQuery.init(elementList.get(i).getEObjectURI(), label);
				
				System.out.println(referenceQuery.toString());
				
				NewSearchUI.activateSearchResultView();

				NewSearchUI.runQueryInBackground(referenceQuery);
				
				
				System.out.println(i+"  "+label);

            }
			
			
		



platform:/resource/DscProject/nice_try.dsc#//@property.9/@expr1/@value1.0/@func/@expr4/@value1.0/@i_d
org.eclipse.xtext.ui.editor.findrefs.ReferenceQuery@1e976fd
0  Xtext References to Base (/DscProject/nice_try.dsc)
platform:/resource/DscProject/nice_try.dsc#//@property.32/@expr1/@value1.0/@i_d
org.eclipse.xtext.ui.editor.findrefs.ReferenceQuery@7be610
1  Xtext References to Base (/DscProject/nice_try.dsc)
Re: rewrite the method for finding references [message #550397 is a reply to message #550104] Fri, 30 July 2010 09:25 Go to previous messageGo to next message
No real name is currently offline No real name
Messages: 36
Registered: July 2010
Member
package org.xtext.example.mydsl.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;

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.action.IMenuManager;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.search.ui.IQueryListener;
import org.eclipse.search.ui.ISearchResultViewPart;
import org.eclipse.search.ui.NewSearchUI;


import org.eclipse.xtext.builder.builderState.impl.ReferenceDescriptionImpl;
import org.eclipse.xtext.parsetree.CompositeNode;
import org.eclipse.xtext.parsetree.NodeAdapter;
import org.eclipse.xtext.parsetree.NodeUtil;
import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
import org.eclipse.xtext.resource.EObjectDescription;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.IResourceDescription;
import org.eclipse.xtext.resource.IResourceDescriptions;
import org.eclipse.xtext.resource.XtextResourceSet;

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.findrefs.ReferenceSearchResult;
import org.eclipse.xtext.ui.editor.findrefs.ReferenceSearchResultContentProvider;
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
{

	@Inject
	private IStorage2UriMapper storage2UriMapper;

	@Inject
	private Provider<ReferenceQuery> queryProvider;

	@Inject
	private IResourceDescriptions resourceDescriptions;



	private static final Logger LOG = Logger.getLogger(FindReferencesHandler.class);
	
	
	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) {
					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;
		}
	}
	
	
	@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());

			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(selection.getText()))
                    	{
                    		elementList.add(EObjectDescription .create(selection.getText(), element));
        	
                    	}                    	
                    }    
            } 
            
            ReferenceQuery referenceQuery = queryProvider.get();
           
           NewSearchUI.activateSearchResultView();
           
           String label = Messages.FindReferencesHandler_labelPrefix + elementList.get(0).toString();
			
			Iterator<IStorage> storages = storage2UriMapper.getStorages(elementList.get(0).getEObjectURI())
					.iterator();
					
			if (storages.hasNext()) {
				label += Messages.FindReferencesHandler_1 + storages.next().getFullPath().toString() + Messages.FindReferencesHandler_2;
			
			         }
    
          referenceQuery.init(elementList.get(0).getEObjectURI().trimFragment(), label);

		  NewSearchUI.runQueryInBackground(referenceQuery);          
         
		  ReferenceSearchResult ref_result=(ReferenceSearchResult) referenceQuery.getSearchResult();	
		  
		  ReferenceDescriptionImpl ref = new ReferenceDescriptionImpl(){};
		  
		   ref.setSourceEObjectUri(elementList.get(0).getEObjectURI());

		   ref_result.addMatchingReference(ref);
  
		  
           int i=1;
            
           
           for( i=1; i<elementList.size();i++)
            {           	
        	   
        	 ReferenceDescriptionImpl ref1 = new ReferenceDescriptionImpl(){};

        	   
        	  ref1.setSourceEObjectUri(elementList.get(i).getEObjectURI());
        	  

              ref_result.addMatchingReference(ref1);
				
		  
            }

			
		} 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;
	}
	}


	
	
}





Some help please? I don't succeed to fill in the search tab with the good things (i guess instances of ReferenceDescriptionImpl) Neutral
Re: rewrite the method for finding references [message #551989 is a reply to message #550397] Tue, 10 August 2010 12:30 Go to previous messageGo to next message
No real name is currently offline No real name
Messages: 36
Registered: July 2010
Member
up!

Really.....there is no way i can re-write this method? (as i did for the other functionalities)
Re: rewrite the method for finding references [message #552201 is a reply to message #551989] Wed, 11 August 2010 08:41 Go to previous message
Meinte Boersma is currently offline Meinte Boersma
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
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.


Previous Topic:How to overrule WorkspaceProjectsStateHelper
Next Topic:change text hover style
Goto Forum:
  


Current Time: Fri Oct 24 21:32:40 GMT 2014

Powered by FUDForum. Page generated in 0.03253 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software