Skip to main content


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: 14661
Registered: July 2009
Senior Member
Hi,

you may have a look at
org.eclipse.xtext.ui.editor.autoedit.DefaultAutoEditStrategyProvider


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
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: 14661
Registered: July 2009
Senior Member
Hi,

(1)why not subclassing DefaultAutoEditStrategyProvider.
(2) let IGrammarAccess be injected, ask it for grammar und use grammarutil


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
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: 14661
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.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: 14661
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.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: 131
Registered: June 2015
Senior 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: 33
Registered: June 2015
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: 131
Registered: June 2015
Senior 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: 33
Registered: June 2015
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: 131
Registered: June 2015
Senior 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: 131
Registered: June 2015
Senior 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: 14661
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)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
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: 131
Registered: June 2015
Senior 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: 14661
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
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: 131
Registered: June 2015
Senior 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: 14661
Registered: July 2009
Senior Member
which one?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
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 messageGo to next message
ayman salah is currently offline ayman salahFriend
Messages: 131
Registered: June 2015
Senior 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
Re: Auto convert keywords to uppercase while typing [message #1722814 is a reply to message #1027805] Tue, 09 February 2016 11:20 Go to previous messageGo to next message
Anakreontas Mentis is currently offline Anakreontas MentisFriend
Messages: 85
Registered: October 2014
Member
I need to install an autoedit provider to trigger formatting on enter key (new line). In the UI module I have:
	public Class<? extends DefaultAutoEditStrategyProvider> bindDefaultAutoEditStrategyProvider() 
	{
	    System.out.println("Returning autoedit strategy provider");
	    return MyDSLAutoEditStrategyProvider.class;
	}

and MyDSLAutoEditStrategyProvider currently just prints a message:

public class MyDSLAutoEditStrategyProvider extends DefaultAutoEditStrategyProvider {
       public MyDSLAutoEditStrategyProvider () {
		System.out.println("Created");
	}

	@Override
	protected void configure(IEditStrategyAcceptor acceptor) {
		System.out.println("configure called");
		super.configure(acceptor);
	}
}


I see the message "Returning autoedit strategy provider" but not the message from the constructor of MyDSL... or the configure method.
I wonder if there are API changes in XText 2.9.1 that prevent the creation and installation of the custom AutoEditStrategy class.

Regards
Re: Auto convert keywords to uppercase while typing [message #1722817 is a reply to message #1722814] Tue, 09 February 2016 11:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
hi are you sure you use

public Class<? extends AbstractEditStrategyProvider> bindAbstractEditStrategyProvider() {
return YourAutoEditStrategyProvider.class;
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Auto convert keywords to uppercase while typing [message #1722823 is a reply to message #1722817] Tue, 09 February 2016 12:21 Go to previous messageGo to next message
Anakreontas Mentis is currently offline Anakreontas MentisFriend
Messages: 85
Registered: October 2014
Member
In the UI module I also had this binding:
public Class<? extends AbstractEditStrategyProvider> bindAbstractEditStrategyProvider()

which overshadowed the declaration
public Class<? extends DefaultAutoEditStrategyProvider> bindDefaultAutoEditStrategyProvider() 


Not an obvious error since the two methods bind different classes of strategy providers which turn out to be the same because of the class hierarchy
Re: Auto convert keywords to uppercase while typing [message #1722825 is a reply to message #1722823] Tue, 09 February 2016 12:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
so does it work with my signature or does it not?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Auto convert keywords to uppercase while typing [message #1722827 is a reply to message #1722825] Tue, 09 February 2016 12:53 Go to previous message
Anakreontas Mentis is currently offline Anakreontas MentisFriend
Messages: 85
Registered: October 2014
Member
It works with either signatures as long as there is only one of them.

Thank you
Previous Topic:Get text ID for a reference during linking?
Next Topic:Lexer problem
Goto Forum:
  


Current Time: Fri Mar 29 05:43:57 GMT 2024

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

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

Back to the top