Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Using the XText Formatter
Using the XText Formatter [message #1047601] Tue, 23 April 2013 08:37 Go to next message
Mårten Carlzon is currently offline Mårten Carlzon
Messages: 60
Registered: March 2013
Location: Sweden
Member

Hello folks

I seem to be unable to use the XText formatter and was wondering if I could get some help here. (Sitting on XText 2.4)

I am working with a diagram in GMF and Eugeina and since I want it to look nice in the text when I add things in the diagram I tried to use the code here to get a simple start. But I seem to get quite a lot of errors so I was wondering if I am missing something big.

The errors I get are as follows:
---------------------------------------------------------------------------------
First of I cant create a MyDslGrammarAccess inside the function but have to declare it outside, otherwise it cant find the class.

But if I declare a MyDslGrammarAccess f and later set f = getGrammarAccess() I get
Type mismatch: cannot convert from IGrammarAccess to IiDslGrammarAccess

Unable to find pair.getFirst() pair.getSecond(), guessing this is just outdated and moved to getKey() and getValue().

f.getTestIndentationAccess is undefined for the type MyDslFormatter
f.getLineAccess is undefined for the type MyDslFormatter
f.getParamAccess is undefined for the type MyDslFormatter

And I guess the cfg is supposed to be just c for the last three rows Smile
---------------------------------------------------------------------------------

Any help to let me modify so the text doesn't all end up on one row is much appreciated Smile

Thanks for all help

Writ you later / Mårten
Re: Using the XText Formatter [message #1047602 is a reply to message #1047601] Tue, 23 April 2013 08:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6213
Registered: July 2009
Senior Member
Hi,

you need to downcast getGrammarAccess to MyDslGrammarAccess
Re: Using the XText Formatter [message #1047607 is a reply to message #1047601] Tue, 23 April 2013 08:43 Go to previous messageGo to next message
Claudio Heeg is currently offline Claudio Heeg
Messages: 75
Registered: April 2013
Member
Hello,

may I suggest looking into the Domainmodel-Example for Xtext, which has a nice formatter added to it?
Other than that, you'll have to cast things like this:
MyDSLGrammarAccess f = (MyDSLGrammarAccess) getGrammarAccess()


An example of how to work with that access would be
c.setIndentationIncrement().after(f.getEntityAccess().getLeftCurlyBracketKeyword_3());


Hope that helps! (The example will probably help more.)
This is all in Java, by the way, as I can't really get in touch with Xtend.

[Updated on: Tue, 23 April 2013 08:51]

Report message to a moderator

Re: Using the XText Formatter [message #1047611 is a reply to message #1047607] Tue, 23 April 2013 08:50 Go to previous messageGo to next message
Mårten Carlzon is currently offline Mårten Carlzon
Messages: 60
Registered: March 2013
Location: Sweden
Member

Hi you two.

Will look at the example after this but I seem to have a strange error.

For some reason I can't downcast, I get the error:
"The method or field IiDslGrammarAccess is undefined for the type IiDslFormatter"

This might be related to why I have to declare IiDslGrammarAccess outside of the configureFormatting function but as soon as I try to use it inside the IiDslFormatter.xtend's configureFormatting it cant find much at all even thought its imported. getEntityAccess for instance isn't declared anywhere in the project.

But will look at the domainmodel-example and hopefully it will help.
Re: Using the XText Formatter [message #1047617 is a reply to message #1047611] Tue, 23 April 2013 08:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6213
Registered: July 2009
Senior Member
Hi,

how does your formatter class look like?
Re: Using the XText Formatter [message #1047621 is a reply to message #1047617] Tue, 23 April 2013 08:57 Go to previous messageGo to next message
Mårten Carlzon is currently offline Mårten Carlzon
Messages: 60
Registered: March 2013
Location: Sweden
Member

At the moment it looks like this:

/*
 * generated by Xtext
 */
package idainfront.xtex.iipax.idd.formatting

import org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter
import org.eclipse.xtext.formatting.impl.FormattingConfig

 import com.google.inject.Inject;
 import idainfront.xtex.iipax.idd.services.IiDslGrammarAccess;

/**
 * This class contains custom formatting description.
 * 
 * see : http://www.eclipse.org/Xtext/documentation/latest/xtext.html#formatting
 * on how and when to use it 
 * 
 * Also see {@link org.eclipse.xtext.xtext.XtextFormattingTokenSerializer} as an example
 */
class IiDslFormatter extends AbstractDeclarativeFormatter {

	@Inject extension IiDslGrammarAccess
	
	IiDslGrammarAccess f;
	
	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
//		c.setLinewrap(0, 1, 2).before(SL_COMMENTRule)
//		c.setLinewrap(0, 1, 2).before(ML_COMMENTRule)
//		c.setLinewrap(0, 1, 1).after(ML_COMMENTRule)

		f = ([u]IiDslGrammarAccess[/u])getGrammarAccess();
	    c.setAutoLinewrap(120);
	    
	    // find common keywords an specify formatting for them
	    for (Pair<Keyword, Keyword> pair : [u]f.findKeywordPairs("(", ")")[/u]) {
	      c.setNoSpace().after(pair.[u]getFirst[/u]());
	      c.setNoSpace().before(pair.[u]getSecond[/u]());
	    }
	    for (Keyword comma : f.findKeywords(",")) {
	      c.setNoSpace().before(comma);
	    }
	
	    // formatting for grammar rule Line
	    c.setLinewrap(2).after(f.[u]getLineAccess[/u]().getSemicolonKeyword_1());
	    c.setNoSpace().before(f.[u]getLineAccess[/u]().getSemicolonKeyword_1());
	    
	    // formatting for grammar rule TestIndentation
	    c.setIndentationIncrement().after(
	        f.[u]getTestIndentationAccess[/u]().getLeftCurlyBracketKeyword_1());
	    c.setIndentationDecrement().before(
	        f.[u]getTestIndentationAccess[/u]().getRightCurlyBracketKeyword_3());
	    c.setLinewrap().after(
	        f.[u]getTestIndentationAccess[/u]().getLeftCurlyBracketKeyword_1());
	    c.setLinewrap().after(
	        f.[u]getTestIndentationAccess[/u]().getRightCurlyBracketKeyword_3());
	    
	    // formatting for grammar rule Param
	    c.setNoLinewrap().around(f.[u]getParamAccess[/u]().getColonKeyword_1());
	    c.setNoSpace().around(f.[u]getParamAccess[/u]().getColonKeyword_1());
	    
	    // formatting for Comments 
	    [u]cfg[/u].setLinewrap(0, 1, 2).before(g.getSL_COMMENTRule());
	    [u]cfg[/u].setLinewrap(0, 1, 2).before(g.getML_COMMENTRule());
	    [u]cfg[/u].setLinewrap(0, 1, 1).after(g.getML_COMMENTRule());

	}
}


Underlined the places where I have errors Smile
[Edit: Or so I thought, seems underline doesn't work in code Smile]

[Updated on: Tue, 23 April 2013 08:58]

Report message to a moderator

Re: Using the XText Formatter [message #1047662 is a reply to message #1047621] Tue, 23 April 2013 09:50 Go to previous messageGo to next message
Mårten Carlzon is currently offline Mårten Carlzon
Messages: 60
Registered: March 2013
Location: Sweden
Member

*Sigh*
Ok, I found a big difference between my files and the one in the domainmodel example, they have a java file and I have an xtend file.

Now it actually makes more sense why I can't downcast and such things.

edited away


I made my file also a java file but a question popped up, what is the big difference between having it as an xtend file or a java file? What perks do you get for having a xtend file instead of a java file?


[Updated on: Tue, 23 April 2013 09:57]

Report message to a moderator

Re: Using the XText Formatter [message #1047673 is a reply to message #1047662] Tue, 23 April 2013 10:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6213
Registered: July 2009
Senior Member
Hi,

in Xtend you cast via

getGrammarAccess() as MyDslGrammarAccess
Re: Using the XText Formatter [message #1047680 is a reply to message #1047673] Tue, 23 April 2013 10:10 Go to previous messageGo to next message
Mårten Carlzon is currently offline Mårten Carlzon
Messages: 60
Registered: March 2013
Location: Sweden
Member

Hello,

Ahh ok, well, since I can use Java I think I will stick with it. More used to that syntax but thanks for showing me how its done in xtend.
Re: Using the XText Formatter [message #1047683 is a reply to message #1047680] Tue, 23 April 2013 10:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6213
Registered: July 2009
Senior Member
the xtend/java is toggled in the workflow
Re: Using the XText Formatter [message #1048198 is a reply to message #1047683] Wed, 24 April 2013 02:49 Go to previous messageGo to next message
Mårten Carlzon is currently offline Mårten Carlzon
Messages: 60
Registered: March 2013
Location: Sweden
Member

Thanks, now just to ponder on if I should switch my work to Java or keep it in xtend Smile
Re: Using the XText Formatter [message #1051844 is a reply to message #1048198] Mon, 29 April 2013 05:25 Go to previous message
Mårten Carlzon is currently offline Mårten Carlzon
Messages: 60
Registered: March 2013
Location: Sweden
Member

Question about the xtend/java toggle.
I guess it is the: "var generateXtendStub = false" variable, but it doesn't seem to affect all files.

I switched this to false but it only affected my formatter and my scope provider. Not my validator or generator which is the two I would like to change to java since I can make nicer code in java then what I currently have Smile.

First I thought it had to do with the auto-inject (since when I removed it my formatter class became java Smile) but even removing it from all function calls except "fragment = compare.CompareFragment auto-inject {}" (since it wont compile otherwise) didn't seem to help so asking you guys, where do I set so my validator and generator becomes java files?

Thanks for any help

Write you later / Mårten
Previous Topic:message console and terminate button
Next Topic:cross reference / multiple files / JvmModelInferer
Goto Forum:
  


Current Time: Fri Aug 22 23:52:54 EDT 2014

Powered by FUDForum. Page generated in 0.02328 seconds