Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » specify STRING in my DSL, so I can use templates inside the string(use plain eclipse templates inside my DSL comment string)
specify STRING in my DSL, so I can use templates inside the string [message #1705437] Sat, 15 August 2015 11:47 Go to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 246
Registered: June 2013
Senior Member
in my DSL, I have defined some comment as STRING:


AComment: {AComment}
'comment=' commentText=STRING?
;

For convenience, I want to provide some templates in the DSL preference page which holds text blocks to be used inside the comment string

Problem:
I have not found a way to specify the context of the template so it will show up if I press CTRL-space inside the string.

Question:
Is there a trick to do the job with some clever grammar or so?

Why:
in xtend, when I call CTRL+space inside the ''' ''' string, it will insert the «»
Re: specify STRING in my DSL, so I can use templates inside the string [message #1729300 is a reply to message #1705437] Tue, 12 April 2016 17:32 Go to previous messageGo to next message
Miguel Jimenez is currently offline Miguel JimenezFriend
Messages: 38
Registered: July 2015
Member
Hello,

Did you find how to implement this functionality?
Re: specify STRING in my DSL, so I can use templates inside the string [message #1729338 is a reply to message #1729300] Wed, 13 April 2016 08:28 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 246
Registered: June 2013
Senior Member
unfortunately, noone gave me a hint
Re: specify STRING in my DSL, so I can use templates inside the string [message #1729412 is a reply to message #1729338] Wed, 13 April 2016 23:51 Go to previous messageGo to next message
Miguel Jimenez is currently offline Miguel JimenezFriend
Messages: 38
Registered: July 2015
Member
Well, it seems templates is not the way to go; you can see here the Xtend default templates.
Re: specify STRING in my DSL, so I can use templates inside the string [message #1729420 is a reply to message #1729412] Thu, 14 April 2016 05:09 Go to previous message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13186
Registered: July 2009
Senior Member
Hi,

xtend actually parses Strings. ntl you can xtend the behaviour how contexts are created. here is a wild guess (not really tested)

public class MyDslTemplateProposalProvider extends DefaultTemplateProposalProvider {

	private ContextTypeIdHelper helper;

	@Inject
	public MyDslTemplateProposalProvider(TemplateStore templateStore, ContextTypeRegistry registry,
			ContextTypeIdHelper helper) {
		super(templateStore, registry, helper);
		this.helper = helper;
	}
	
	@Override
	protected void createTemplates(TemplateContext templateContext, ContentAssistContext context,
			ITemplateAcceptor acceptor) {
		System.out.println("context: " + context);
		super.createTemplates(templateContext, context, acceptor);
	}
	
	@Override
	protected TemplateContext[] createTemplateContexts(ContentAssistContext context) {
		return super.createTemplateContexts(context);
	}
	
	@Override
	protected IFollowElementAcceptor createFollowElementAcceptor(Collection<TemplateContextType> result) {
		return new FollowElementAcceptor(result) {
			
			@Override
			public Boolean caseTerminalRule(TerminalRule object) {
				addContextType(helper.getId(object));
				return super.caseTerminalRule(object);
			}
			
		};
	}

}



public class MyDslXtextTemplateContextTypeRegistry extends XtextTemplateContextTypeRegistry {

	
	@Inject
	public MyDslXtextTemplateContextTypeRegistry(IGrammarAccess grammarAccess, Provider<XtextTemplateContextType> ctxTypeProvider, ContextTypeIdHelper helper) {
		super(grammarAccess, ctxTypeProvider, helper);
	}
	
	@Override
	protected void registerContextTypes(IGrammarAccess grammarAccess,
			Provider<XtextTemplateContextType> ctxTypeProvider) {
		List<TerminalRule> terminalRules = GrammarUtil.allTerminalRules(grammarAccess.getGrammar());
		for (TerminalRule terminalRule : terminalRules) {
			String value = getHelper().getId(terminalRule);
			if (value!=null) {
				XtextTemplateContextType type = ctxTypeProvider.get();
				type.setName("Terminal '"+terminalRule.getName()+"'");
				type.setId(value);
				addContextType(type);
			}
		}
		super.registerContextTypes(grammarAccess, ctxTypeProvider);
	}

}


public class MyDslContextTypeIdHelper extends ContextTypeIdHelper{
	
	@Inject
	private MyDslGrammarAccess ga;
	
	@Override
	public String getId(AbstractRule rule) {
		if (rule instanceof TerminalRule) {
			String id = ga.getGrammar().getName()+"."+rule.getName();
			return id;
		}
		return super.getId(rule);
	}

}


@FinalFieldsConstructor
class MyDslUiModule extends AbstractMyDslUiModule {
	
	override bindITemplateProposalProvider() {
		MyDslTemplateProposalProvider
	}
	
	def Class<? extends ContextTypeIdHelper> bindContextTypeIdHelper() {
		return MyDslContextTypeIdHelper
	}
	
	override bindContextTypeRegistry() {
		MyDslXtextTemplateContextTypeRegistry
	}
	
}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Xtext Intellij integration
Next Topic:Default Scope Shadowing
Goto Forum:
  


Current Time: Wed Nov 13 08:08:06 GMT 2019

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

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

Back to the top