Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Cross reference to duplicate items in the model(Cross reference to duplicate items in the model)
Cross reference to duplicate items in the model [message #1803647] Wed, 06 March 2019 06:49 Go to next message
Virag Purnam is currently offline Virag PurnamFriend
Messages: 107
Registered: June 2014
Senior Member
Hello,

When the model has duplicate items, cross reference always goes to the first one or randomly.
I want to achieve cross reference to a particular item based on condition. I have created a sample xtext project.

Grammar:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	conts+=Cont* procs+=Proc*;
	
Cont:
	'Cont' name=ID '{' items+=Item* '}' ;

Item: 'Item' name=ID;


Proc:
	'Link' item1=[Item|ID] 'OF' con1=[Cont|ID] 'TO' item2=[Item|ID] 'OF' con2=[Cont|ID]
;


MyDslScopeProvider:
class MyDslScopeProvider extends AbstractMyDslScopeProvider {
	
	override getScope(EObject context, EReference reference) {
		if(context instanceof Proc){
			if(reference.featureID == MyDslPackage.PROC__ITEM1 || reference.featureID == MyDslPackage.PROC__ITEM2){
				val rootElement = EcoreUtil2.getContainerOfType(context, Model);
				val candidates = getItems(rootElement)				
				return Scopes.scopeFor(candidates)
			}
		}
		return super.getScope(context, reference);
	}
	
	def List<EObject> getItems(Model model) {
		var retList = newArrayList
		for(cont : model.conts){
			for(item : cont.items){
				retList.add(item)
			}
		}
		retList		
	}

}


Sample Model:
Cont container1 {
	Item item1
}

Cont container2 {
	Item item1
}  

Link item1 OF container1 TO item1 OF container2   


In the sample model, I want the cross reference on "item1" to go to correct definition based on container. That means "item1 OF container1" should point to item1 defined in container1 and " item1 OF container2" should point to item1 defined in container2.

Is it possible to achieve?
Kindly help me with this regard.


Thanks and regards,
Virag Purnam
Re: Cross reference to duplicate items in the model [message #1803686 is a reply to message #1803647] Wed, 06 March 2019 18:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13068
Registered: July 2009
Senior Member
Hi what about simply implement the scoping?
This is basically the same as reference to a class and a field


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Cross reference to duplicate items in the model [message #1803701 is a reply to message #1803686] Thu, 07 March 2019 06:07 Go to previous messageGo to next message
Virag Purnam is currently offline Virag PurnamFriend
Messages: 107
Registered: June 2014
Senior Member
Hi Mr. Christian Dietrich,

I will give a try.
I will read the "Link" and find the corresponding container and will restrict the scoping to the container.

Thanks and regards,
Virag Purnam
Re: Cross reference to duplicate items in the model [message #1803838 is a reply to message #1803701] Tue, 12 March 2019 09:25 Go to previous messageGo to next message
Virag Purnam is currently offline Virag PurnamFriend
Messages: 107
Registered: June 2014
Senior Member
Works fine with below changes in MyDslScopeProvider
class MyDslScopeProvider extends AbstractMyDslScopeProvider {

	override getScope(EObject context, EReference reference) {
		if (context instanceof Proc) {
			if (reference.featureID == MyDslPackage.PROC__ITEM1) {
				var contName = (context as Proc).con1.name
				val rootElement = EcoreUtil2.getContainerOfType(context, Model);
				val candidates = getItems(rootElement, contName)
				return Scopes.scopeFor(candidates)
			} else if (reference.featureID == MyDslPackage.PROC__ITEM2) {
				var contName = (context as Proc).con2.name
				val rootElement = EcoreUtil2.getContainerOfType(context, Model);
				val candidates = getItems(rootElement, contName)
				return Scopes.scopeFor(candidates)
			}			
		}
		return super.getScope(context, reference);
	}

	def List<EObject> getItems(Model model, String contName) {
		var retList = newArrayList
		for (cont : model.conts) {
			if (cont.name.equals(contName)) {
				for (item : cont.items) {
					retList.add(item)
				}
			}
		}
		retList
	}

}
Re: Cross reference to duplicate items in the model [message #1803839 is a reply to message #1803838] Tue, 12 March 2019 09:36 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13068
Registered: July 2009
Senior Member
can you simply do a (context as Proc).con2.items and (context as Proc).con1.items to collect the items

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Cross reference to duplicate items in the model [message #1803950 is a reply to message #1803839] Thu, 14 March 2019 06:36 Go to previous message
Virag Purnam is currently offline Virag PurnamFriend
Messages: 107
Registered: June 2014
Senior Member
Thanks Mr. Christian Dietrich.
I will do the changes.
Previous Topic:XImportSection and global scoping
Next Topic:Custom TemplateNewFileWizard
Goto Forum:
  


Current Time: Thu Aug 22 07:56:16 GMT 2019

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

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

Back to the top