Formatting guides? [message #755554] |
Tue, 08 November 2011 19:28 |
David Struck Messages: 29 Registered: July 2010 |
Junior Member |
|
|
Are there any guides that provide detailed explanations of how to work with the formatting functionality? I know I can create my own formatter by extending AbstractDeclarativeFormatter and I've taken a look at this guide here -- http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.xtext.doc%2Fhelp%2Fformatting.html.
But I'm still struggling a lot to get my formatter to work for me. As an example, here's a file I am trying to format:
action Assisted_ingress_exists(float ev1, ev2, float st) [duration]
{
constant float s1 := 3;
[all] contains {
s1: Ingress_by_two(ev2,st);
s2: Untether_for_ingress(ev1,st); //change s2 to s1
};
start(s2) == start(s2) + s1;
end(s1) == end(s2);
}
action Ingress_by_two(float ev, float st)
{
duration := 2;
}
action Untether_for_ingress(float ev1, float st)
{
duration := 2.0;
}
A simple thing, for starters, would be to put newlines before and after both left and right curly braces. I found some code online that looks great:
Iterable<Keyword> keywords = GrammarUtil.containedKeywords(f.getGrammar());
Stack<Keyword> openBraceStack = new Stack<Keyword>();
for(Keyword currentKeyword : keywords) {
if("{".equals(currentKeyword.getValue())) {
openBraceStack.add(currentKeyword);
c.setLinewrap().after(currentKeyword);
}
else if("}".equals(currentKeyword.getValue())) {
c.setLinewrap().after(currentKeyword);
c.setLinewrap().before(currentKeyword);
if(!openBraceStack.isEmpty()) {
c.setIndentation(openBraceStack.pop(), currentKeyword);
}
}
}
Unfortunately, it doesn't work for me. No linewraps or indentation is done when I try to format my file. Does anyone have any idea why that might be?
Thanks,
David
|
|
|
Re: Formatting guides? [message #755576 is a reply to message #755554] |
Tue, 08 November 2011 20:31 |
Sebastian Zarnekow Messages: 3118 Registered: July 2009 |
Senior Member |
|
|
Hi David,
how does your grammar look like? How does your complete formatter look
like? Did you verify that the for-loop is actually executed?
Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
On 08.11.11 20:28, David Struck wrote:
> Are there any guides that provide detailed explanations of how to work
> with the formatting functionality? I know I can create my own formatter
> by extending AbstractDeclarativeFormatter and I've taken a look at this
> guide here --
> http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.xtext.doc%2Fhelp%2Fformatting.html.
>
>
> But I'm still struggling a lot to get my formatter to work for me. As an
> example, here's a file I am trying to format:
>
>
> action Assisted_ingress_exists(float ev1, ev2, float st) [duration]
> {
>
> constant float s1 := 3;
>
> [all] contains {
> s1: Ingress_by_two(ev2,st);
> s2: Untether_for_ingress(ev1,st); //change s2 to s1 };
>
> start(s2) == start(s2) + s1; end(s1) == end(s2);
> }
> action Ingress_by_two(float ev, float st)
> {
> duration := 2;
> }
>
> action Untether_for_ingress(float ev1, float st)
> {
> duration := 2.0;
> }
>
>
> A simple thing, for starters, would be to put newlines before and after
> both left and right curly braces. I found some code online that looks
> great:
>
>
> Iterable<Keyword> keywords = GrammarUtil.containedKeywords(f.getGrammar());
> Stack<Keyword> openBraceStack = new Stack<Keyword>();
> for(Keyword currentKeyword : keywords) {
> if("{".equals(currentKeyword.getValue())) {
> openBraceStack.add(currentKeyword);
> c.setLinewrap().after(currentKeyword);
> }
> else if("}".equals(currentKeyword.getValue())) {
> c.setLinewrap().after(currentKeyword);
> c.setLinewrap().before(currentKeyword);
> if(!openBraceStack.isEmpty()) {
> c.setIndentation(openBraceStack.pop(), currentKeyword);
> }
> }
> }
>
>
> Unfortunately, it doesn't work for me. No linewraps or indentation is
> done when I try to format my file. Does anyone have any idea why that
> might be?
>
> Thanks,
>
> David
|
|
|
Re: Formatting guides? [message #755599 is a reply to message #755576] |
Tue, 08 November 2011 22:01 |
David Struck Messages: 29 Registered: July 2010 |
Junior Member |
|
|
Sebastian, the grammar is quite long (1300+ lines) so I didn't think it made sense to post it. The formatter implementation is pretty simple. Here it is:
public class TESTFormatter extends AbstractDeclarativeFormatter {
@Override
protected void configureFormatting(FormattingConfig c) {
TESTGrammarAccess f = (TESTGrammarAccess) getGrammarAccess();
// 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(f.getSL_COMMENTRule());
c.setLinewrap(0, 1, 2).before(f.getML_COMMENTRule());
c.setLinewrap(0, 1, 1).after(f.getML_COMMENTRule());
Iterable<Keyword> keywords = GrammarUtil.containedKeywords(f.getGrammar());
Stack<Keyword> openBraceStack = new Stack<Keyword>();
for(Keyword currentKeyword : keywords) {
if("{".equals(currentKeyword.getValue())) {
openBraceStack.add(currentKeyword);
c.setLinewrap().after(currentKeyword);
}
else if("}".equals(currentKeyword.getValue())) {
c.setLinewrap().after(currentKeyword);
c.setLinewrap().before(currentKeyword);
if(!openBraceStack.isEmpty()) {
c.setIndentation(openBraceStack.pop(), currentKeyword);
}
}
}
}
If I throw a print statement in the for loop, I can see that the left curly brace is being found. But, for whatever reason, the calls to setLinewrap, etc. don't work. Hopefully that's enough to go on.
Thanks,
David
|
|
|
|
Powered by
FUDForum. Page generated in 0.03042 seconds