Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Auto convert keywords to uppercase while typing
Auto convert keywords to uppercase while typing [message #1027805] Wed, 27 March 2013 12:36 Go to next message
Cash Ko is currently offline Cash KoFriend
Messages: 21
Registered: May 2012
Junior Member
Hello,

is it possible to get all keywords automatically converted to uppercase while typing it in MyDSL Eclipse Editor?

f.e. after i typed the keyword 'entity' and then whitespace after it, it should be automatically converted into 'ENTITY'.

Thank you!
Re: Auto convert keywords to uppercase while typing [message #1027810 is a reply to message #1027805] Wed, 27 March 2013 12:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6483
Registered: July 2009
Senior Member
Hi,

you may have a look at
org.eclipse.xtext.ui.editor.autoedit.DefaultAutoEditStrategyProvider
Re: Auto convert keywords to uppercase while typing [message #1027868 is a reply to message #1027810] Wed, 27 March 2013 14:26 Go to previous messageGo to next message
Cash Ko is currently offline Cash KoFriend
Messages: 21
Registered: May 2012
Junior Member
Hi Christian,

thank you for quick response.

Next 2 questions:

1. Have i to implement my own "public class KeywordEditStrategy extends AbstractEditStrategy implements ISourceViewerAware { ..." using "protected void internalCustomizeDocumentCommand(IDocument document, DocumentCommand command)" to make all keywords (command.text) uppercase?
2. How can i get all keywords form my grammar ( perhaps i need something like "GrammarUtil.getAllKeywords(???)" )
Re: Auto convert keywords to uppercase while typing [message #1027875 is a reply to message #1027868] Wed, 27 March 2013 14:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6483
Registered: July 2009
Senior Member
Hi,

(1)why not subclassing DefaultAutoEditStrategyProvider.
(2) let IGrammarAccess be injected, ask it for grammar und use grammarutil
Re: Auto convert keywords to uppercase while typing [message #1027939 is a reply to message #1027875] Wed, 27 March 2013 16:17 Go to previous messageGo to next message
Cash Ko is currently offline Cash KoFriend
Messages: 21
Registered: May 2012
Junior Member
Hi Christian,

have an another idea. Can we somehow use the eclipse syntax coloring at this place? I mean it is already the place where all keywords will be correctly recognised, so we don't need to implement it again.
Re: Auto convert keywords to uppercase while typing [message #1027956 is a reply to message #1027939] Wed, 27 March 2013 16:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6483
Registered: July 2009
Senior Member
If you have font that is uppercase. Adjusting the
defaulthighlightingconfiguration (or something like that) should do
it.

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Auto convert keywords to uppercase while typing [message #1027984 is a reply to message #1027956] Wed, 27 March 2013 17:23 Go to previous messageGo to next message
Cash Ko is currently offline Cash KoFriend
Messages: 21
Registered: May 2012
Junior Member
Thank you for all the hints.

And the last question is, how can i get a token-Object (ILexerTokenRegion?) according to a specified <offset> in a IDocument-Object? Is this possible?

Re: Auto convert keywords to uppercase while typing [message #1028454 is a reply to message #1027984] Thu, 28 March 2013 09:19 Go to previous messageGo to next message
Cash Ko is currently offline Cash KoFriend
Messages: 21
Registered: May 2012
Junior Member
Hi,

sorry, is it not possible or is too stupid to ask about this?
Re: Auto convert keywords to uppercase while typing [message #1028477 is a reply to message #1028454] Thu, 28 March 2013 09:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6483
Registered: July 2009
Senior Member
I don't know the answer

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Auto convert keywords to uppercase while typing [message #1028725 is a reply to message #1028477] Thu, 28 March 2013 16:55 Go to previous message
Cash Ko is currently offline Cash KoFriend
Messages: 21
Registered: May 2012
Junior Member
Hi,

i founded 2 solutions, which is working fine to me.

NOTE: This is a basic version and can only recognise keywords separated by whitespaces.

Solution 1 ( quickier one ):

public class MyDslEditStrategyProvider extends DefaultAutoEditStrategyProvider {

	@Inject
	Provider<IGrammarAccess> iGrammar;

	private Set<String> kwds;
	
	@Override
	protected void configure(IEditStrategyAcceptor acceptor) {

		kwds = GrammarUtil.getAllKeywords(iGrammar.get().getGrammar());

		IAutoEditStrategy strategy = new IAutoEditStrategy() {

			@Override
			public void customizeDocumentCommand(IDocument document, DocumentCommand command) {

				if ( command.text.length() == 0 || command.text.charAt(0) > ' ') return;

				IRegion reg = ((XtextDocument) document).getLastDamage();

				try {
					String token = document.get(reg.getOffset(), reg.getLength());
					String uToken = token.toUpperCase();
					if ( uToken.equals(token) || !kwds.contains(uToken) ) return; 
					document.replace(reg.getOffset(), reg.getLength(), uToken);

				} catch (Exception e) {}
			}
		};

		acceptor.accept(strategy, IDocument.DEFAULT_CONTENT_TYPE);

		super.configure(acceptor);

	}

}




Solution 2 ( more stable ):

public class MyDslEditStrategyProvider extends DefaultAutoEditStrategyProvider {

	@Inject
	Provider<ITokenScanner> iScanner;

	private ITokenScanner scanner;
	private TextAttribute kwdatt;
	
	@Override
	protected void configure(IEditStrategyAcceptor acceptor) {

		scanner = iScanner.get();
		kwdatt = ((TokenScanner)scanner).getTextAttributeProvider().getAttribute("keyword");

		IAutoEditStrategy strategy = new IAutoEditStrategy() {

			@Override
			public void customizeDocumentCommand(IDocument document, DocumentCommand command) {

				if ( command.text.length() == 0 || command.text.charAt(0) > ' ') return;

				IRegion reg = ((XtextDocument) document).getLastDamage();

				scanner.setRange(document, reg.getOffset(), reg.getLength());
				IToken tok = scanner.nextToken();
				if (!tok.getData().equals(kwdatt)) return;

				try {
					String token = document.get(reg.getOffset(), reg.getLength());
					String uToken = token.toUpperCase();
					if ( uToken.equals(token) ) return; 
					document.replace(reg.getOffset(), reg.getLength(), uToken);

				} catch (Exception e) {}


			}
		};

		acceptor.accept(strategy, IDocument.DEFAULT_CONTENT_TYPE);

		super.configure(acceptor);

	}

}



Many thanks to Christian for support!

[Updated on: Thu, 28 March 2013 18:54]

Report message to a moderator

Previous Topic:Creating XExpressions Manually
Next Topic:Static Initializers
Goto Forum:
  


Current Time: Sat Nov 22 00:22:29 GMT 2014

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

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