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: 7559
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: 7559
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: 7559
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: 7559
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 messageGo to next 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

Re: Auto convert keywords to uppercase while typing [message #1699420 is a reply to message #1028725] Wed, 24 June 2015 08:50 Go to previous messageGo to next message
ayman salah is currently offline ayman salahFriend
Messages: 51
Registered: June 2015
Member
I tried making exactly the same as you did, but nothing happens. How can I trigger the auto edit to occur? It doesn't happen automatically. Am I missing something? What folder/package should it be placed in?
Re: Auto convert keywords to uppercase while typing [message #1699429 is a reply to message #1699420] Wed, 24 June 2015 09:46 Go to previous messageGo to next message
Mohsin waqas is currently offline Mohsin waqasFriend
Messages: 24
Registered: June 2015
Junior Member
@ayman did you bind it in UIModule?
Re: Auto convert keywords to uppercase while typing [message #1699433 is a reply to message #1699429] Wed, 24 June 2015 10:13 Go to previous messageGo to next message
ayman salah is currently offline ayman salahFriend
Messages: 51
Registered: June 2015
Member
No. How can i?
Re: Auto convert keywords to uppercase while typing [message #1699437 is a reply to message #1699433] Wed, 24 June 2015 10:37 Go to previous messageGo to next message
Mohsin waqas is currently offline Mohsin waqasFriend
Messages: 24
Registered: June 2015
Junior Member
        public Class<? extends DefaultAutoEditStrategyProvider>
	bindDefaultAutoEditStrategyProvider() {
	return MyDslEditStrategyProvider.class;
	}
       

add this in MyDslUIModule.java
Re: Auto convert keywords to uppercase while typing [message #1699451 is a reply to message #1699437] Wed, 24 June 2015 12:18 Go to previous messageGo to next message
ayman salah is currently offline ayman salahFriend
Messages: 51
Registered: June 2015
Member
I have done this now. And still it is not working. What am I doing wrong?
Should the DefaultAutoEditStrategyProvider be in some specific package?
And what is the expected behavior? Will writing a keyword then a space after trigger it and boom it's capitalized? or wat?
Re: Auto convert keywords to uppercase while typing [message #1699455 is a reply to message #1699437] Wed, 24 June 2015 12:36 Go to previous messageGo to next message
ayman salah is currently offline ayman salahFriend
Messages: 51
Registered: June 2015
Member
I have done it. I had some logical error. Now the thing is can I trigger it on format? on Ctrl+Shift+F?
Re: Auto convert keywords to uppercase while typing [message #1699459 is a reply to message #1699455] Wed, 24 June 2015 12:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 7559
Registered: July 2009
Senior Member
@Override
public Class<? extends AbstractEditStrategyProvider> bindAbstractEditStrategyProvider() {
return MyDslEditStrategyProvider.class;
}


works fine for me (upper case is done as you type)
Re: Auto convert keywords to uppercase while typing [message #1699461 is a reply to message #1699459] Wed, 24 June 2015 12:50 Go to previous messageGo to next message
ayman salah is currently offline ayman salahFriend
Messages: 51
Registered: June 2015
Member
@Christian
Does that make it on "Format" Ctrl+Shift+F?
Because that was what I wanted. I didn't really wanted it as I type but it is a plus.

[Updated on: Wed, 24 June 2015 12:51]

Report message to a moderator

Re: Auto convert keywords to uppercase while typing [message #1699462 is a reply to message #1699461] Wed, 24 June 2015 12:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 7559
Registered: July 2009
Senior Member
No

in the greeting example i type

crtl+space
then select Hello,
then type space

or i Type letter by Letter Hello and then space
Re: Auto convert keywords to uppercase while typing [message #1699463 is a reply to message #1699462] Wed, 24 June 2015 12:54 Go to previous messageGo to next message
ayman salah is currently offline ayman salahFriend
Messages: 51
Registered: June 2015
Member
Great! Thanks.

Um, Would you help me with my question on stackoverflow? It's major for me.
Re: Auto convert keywords to uppercase while typing [message #1699467 is a reply to message #1699463] Wed, 24 June 2015 13:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 7559
Registered: July 2009
Senior Member
which one?
Re: Auto convert keywords to uppercase while typing [message #1699470 is a reply to message #1699467] Wed, 24 June 2015 13:19 Go to previous message
ayman salah is currently offline ayman salahFriend
Messages: 51
Registered: June 2015
Member
It is related to comments. I know this has been addressed in formatting2. So would you help me with either ones?

http://stackoverflow.com/questions/31022234/xtext-comments-formatting
https://www.eclipse.org/forums/index.php/m/1699469/#msg_1699469
Previous Topic:Includes, global constants in DSL files
Next Topic:Hexcode terminal vs Named Colors terminal. Terminal structuring problem.
Goto Forum:
  


Current Time: Thu Sep 03 04:52:19 GMT 2015

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

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