Using the XText Formatter [message #1047601] |
Tue, 23 April 2013 12:37 |
|
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
---------------------------------------------------------------------------------
Any help to let me modify so the text doesn't all end up on one row is much appreciated
Thanks for all help
Writ you later / Mårten
|
|
|
|
Re: Using the XText Formatter [message #1047607 is a reply to message #1047601] |
Tue, 23 April 2013 12:43 |
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 12:51] Report message to a moderator
|
|
|
Re: Using the XText Formatter [message #1047611 is a reply to message #1047607] |
Tue, 23 April 2013 12:50 |
|
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 #1047621 is a reply to message #1047617] |
Tue, 23 April 2013 12:57 |
|
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
[Edit: Or so I thought, seems underline doesn't work in code ]
[Updated on: Tue, 23 April 2013 12:58] Report message to a moderator
|
|
|
Re: Using the XText Formatter [message #1047662 is a reply to message #1047621] |
Tue, 23 April 2013 13:50 |
|
*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 awaySo two questions:
- How do I change to a java file instead of an xtend file?
- I tried to just switch but then I get a new xtend file when I ran as MWE2 workflow.
- Whats the benefit of keeping it as an xtend file?
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 13:57] Report message to a moderator
|
|
|
|
|
|
|
Re: Using the XText Formatter [message #1051844 is a reply to message #1048198] |
Mon, 29 April 2013 09:25 |
|
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 .
First I thought it had to do with the auto-inject (since when I removed it my formatter class became java ) 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
|
|
|
Powered by
FUDForum. Page generated in 0.03040 seconds