Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » insert automatic space before a token
insert automatic space before a token [message #637993] Tue, 09 November 2010 15:04 Go to next message
Maria is currently offline MariaFriend
Messages: 49
Registered: October 2010
Member
I would like to insert a space automattically in my editor file before each token "co"
So I have seen the section 5.9 of the documentation and I have followed it by writing this little code under the package: org.xtext.example.NameOf MyProject

"public class ModeleFormatter extends AbstractDeclarativeFormatter {

@Override
protected void configureFormatting(FormattingConfig c) {
// It's usually a good idea to activate the following three statements.
// They will add and preserve newlines around comments
ModeleGrammarAccess f = (ModeleGrammarAccess) getGrammarAccess();
for (Keyword co : f.findKeywords("CO")) {
c.setSpace(" ").before(CO);
}

}
}

I have in my model a sentence like this: Number of character in frezCOghjj

After running there is no automatic space inserted before "CO".
What is missing please?
Re: insert automatic space before a token [message #638008 is a reply to message #637993] Tue, 09 November 2010 15:57 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

my bet is that 'CO' is not a keyword in your grammar and even if it is, it is unlikely that 'frezCOghjj' is tokenised as something like ID 'frez' followed by keyword 'CO' followed by ID 'ghjj' but rather as ID 'frezCOghjj' (you would have to redefine ID so that it cannot contain 'CO' (but I don't think one would really want that). So the formatter configuration will not hit.

A simple (but unsafe way - copy/paste, deleting characters etc.) possibility would be an autoedit strategy that makes sure that after entering 'CO', a space is inserted if it does not exist yet. (There may be a code snippet for that in one of the examples shipped with Xtext.)

You may have to dig deeper into the formatter code in order to insert spaces somewhere within tokens.

Alex
Re: insert automatic space before a token [message #638232 is a reply to message #638008] Wed, 10 November 2010 15:18 Go to previous messageGo to next message
Maria is currently offline MariaFriend
Messages: 49
Registered: October 2010
Member
Yes Co is not a keyword.
My aim is to insert automatically a space when I write "CO" in my editor.

What is the mean to do it?

I have tried the example of the documentation in the section 5.9 but it doesn't function for me.

I think that this section is not very well explained.
Re: insert automatic space before a token [message #638408 is a reply to message #638232] Thu, 11 November 2010 08:30 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

auto formatting is not what you want in your use case. You want auto edit. Look at the ui project of the domain model example shipped with Xtext (Helios). There is an autoedit package containing an implementation of an auto edit strategy. It is not activated in the example as the binding in the UIModule is missing.

Implement a similar strategy for your CO and bind the implementation to IAutoStrategy. Then it will automatically be injected in the Xtext SourceViewerConfiguration.

Alex
Re: insert automatic space before a token [message #638443 is a reply to message #638408] Thu, 11 November 2010 10:25 Go to previous messageGo to next message
Maria is currently offline MariaFriend
Messages: 49
Registered: October 2010
Member
What is the name of this package?
Re: insert automatic space before a token [message #638444 is a reply to message #638443] Thu, 11 November 2010 10:26 Go to previous messageGo to next message
Maria is currently offline MariaFriend
Messages: 49
Registered: October 2010
Member
what is the name of this package?Can I use it to insert a return to line \n' automatically?
Re: insert automatic space before a token [message #638467 is a reply to message #638444] Thu, 11 November 2010 12:15 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

have you opened the domain model example shipped with Xtext in your workspace???

New->Examples->Xtext Examples->Xtext Domain-Model Example

The project org.eclipse.xtext.example.domainmodel.ui contains a folder src. That folder contains a package org.eclipse.xtext.example.ui.autoedit which contains the file FantasticAutoEditStrategy.java.
This file contains an example implementatioj of an auto edit strategy. Adapt that implementation to insert whatever you want wherever you want and bind that class in the UIModule of your project.

Alex
Re: insert automatic space before a token [message #638562 is a reply to message #638467] Thu, 11 November 2010 16:06 Go to previous messageGo to next message
Maria is currently offline MariaFriend
Messages: 49
Registered: October 2010
Member
Thank you I have found this class FantasticAutoEditStrategy.java
I have changed the method getText() of the class FantasticAutoEditStrategy like this:
private String getText() {
return
"Hello";
Then I have run my project org.eclipse.xtext.example.domainmodel

In my editor I have written:
datatype Xtext
entity Df{
name : Xtext*
}

But I didn't see anything of auto edition Here.
So I have changed
if ((command.text.equals("t") && document.get(command.offset-4, 4).equals("Xtex"))) by
if (! ((command.text.equals("t") && document.get(command.offset-4, 4).equals("Xtex"))))

And nothing has changed Sad

Re: insert automatic space before a token [message #638757 is a reply to message #638408] Fri, 12 November 2010 13:41 Go to previous messageGo to next message
Maria is currently offline MariaFriend
Messages: 49
Registered: October 2010
Member
what does this class automatically add into the editor?!!!!
Re: insert automatic space before a token [message #638788 is a reply to message #638757] Fri, 12 November 2010 15:25 Go to previous message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

if you read my posts carefully, you will note that I said that the auto edit strategy was not activated in the example. I also wrote that you have to bind your implementation in the UIModule.

Alex
Previous Topic:debug message eSetBasedAllContainersStateProvider
Next Topic:Code Template does not respect formatting / indentation?
Goto Forum:
  


Current Time: Thu Apr 25 08:49:24 GMT 2024

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

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

Back to the top