Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [solved] Indentation of elements in EList using formatting2 API
[solved] Indentation of elements in EList using formatting2 API [message #1827721] Thu, 21 May 2020 17:31 Go to next message
Aaron Miller is currently offline Aaron MillerFriend
Messages: 5
Registered: May 2020
Junior Member
Hello,

I am having trouble figuring out how to indent elements in an EList using the formatting2 API. Everything I've tried so far leads to this strange "indentInc=1;indentDec=1" result or an error along the lines of "That is not allowed on an AST element.

Could anyone please help me understand what is happening when I get "indentInc=1;indentDec=1"? And of course any guidance on the proper way to go about this will be greatly appreciated.

Cheers and thank you!

grammar org.xtext.example.mydsl.MyDsl hidden(WS)

import "URL_REMOVED" as ecore
generate myDsl "URL_REMOVED"

Model:
    greeting=Greeting
;

Greeting:
    'Hello' ','? name=SIMPLE_WORD '!' NL+
    lines+=Line*
;

Line: {Line}
    SIMPLE_WORD+
    NL
;

terminal SIMPLE_WORD: 
    ('0'..'9' | 'a'..'z' | 'A'..'Z') 
    ('0'..'9' | 'a'..'z' | 'A'..'Z' | '-' | '_')*
;
terminal NL: ('\r'? '\n');
terminal WS: (' ' | '\t');


class MyDslFormatter extends AbstractFormatter2 {

    @Inject extension MyDslGrammarAccess

    def dispatch void format(Model model, extension IFormattableDocument document) {
        model.greeting.format()

        println(document)
    }

    def dispatch void format(Greeting greeting, extension IFormattableDocument document) {
        for (line : greeting.lines) {
            line.format()
            line.prepend[indent]
        }
    }

    def dispatch void format(Line line, extension IFormattableDocument document) {
        // TODO...
    }
}


@RunWith(XtextRunner)
@InjectWith(MyDslInjectorProvider)
class MyDslFormattingTest {

    @Inject extension FormatterTestHelper

    @Test
    def void indentLines() {
        assertFormatted[
            toBeFormatted = '''
                Hello, World!
                The quick brown fox
                Jumps over the lazy dog
            '''
            expectation = '''
                Hello, World!
                    The quick brown fox
                    Jumps over the lazy dog
            '''
        ]
    }
}


Quote:

----------- RootDocument with ITextReplacers (syntax: <offset|text>) -----------
Hello, World!
<14|>The quick brown fox
<34|>Jumps over the lazy dog
--------------------------------------------------------------------------------
14 0 "": HiddenRegionReplacer: indentInc=1;indentDec=1
34 0 "": HiddenRegionReplacer: indentInc=1;indentDec=1

[Updated on: Fri, 22 May 2020 20:20]

Report message to a moderator

Re: Indentation of elements in EList using formatting2 API [message #1827739 is a reply to message #1827721] Fri, 22 May 2020 08:50 Go to previous messageGo to next message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 157
Registered: February 2016
Senior Member
Hello Aaron,

the following configuration works for me:

def dispatch void format(Greeting greeting, extension IFormattableDocument document)  {
	val begin = greeting.regionFor.ruleCallTo(NLRule)
	val end = greeting.lines.last.regionFor.ruleCallTo(NLRule)
		
	interior(begin, end)[indent]
	for (line : greeting.lines) {
		line.format
		line.prepend[indent]
	}
}


Hope that helps,
Tamás
Re: Indentation of elements in EList using formatting2 API [message #1827767 is a reply to message #1827739] Fri, 22 May 2020 20:19 Go to previous messageGo to next message
Aaron Miller is currently offline Aaron MillerFriend
Messages: 5
Registered: May 2020
Junior Member
Many thanks Tamás, this is exactly what I was looking for!

I had tried something similar, but couldn't quite get the begin/end regions correct.

Cheers and thank you again!
Re: Indentation of elements in EList using formatting2 API [message #1828076 is a reply to message #1827767] Sun, 31 May 2020 22:48 Go to previous messageGo to next message
Aaron Miller is currently offline Aaron MillerFriend
Messages: 5
Registered: May 2020
Junior Member
Sorry to piggyback, but could you please give me your thoughts on this as well?

Step:
	keyword=ANY_KEYWORD
	description=Text EOL+
	content=DocString?
;

terminal ANY_KEYWORD: '*';

DocString:
	value=DOC_STRING
	EOL+
;

terminal DOC_STRING: ('"""' -> '"""' | "'''" -> "'''");

Text returns ecore::EString:
 	TextLiteral+
;


@Test
def void indentStepContent() {
    // fail
    assertFormatted[
        toBeFormatted = '''
            Scenario: Hello, World!
            * The quick brown fox
            * Jumps over the lazy dog
            """
            At -9.8m/s^2
            """
        '''
        expectation = '''
            Scenario: Hello, World!
                * The quick brown fox
                * Jumps over the lazy dog
                """
                At -9.8m/s^2
                """
        '''
    ]
}


I can get the first `"""` to indent correctly, but the rest is contained within the value. I was thinking of maybe calculating the indentation of the starting keyword, then manually append it after every line break in the string. Would this be the correct way to go about it, or am I over complicating things?

Cheers, and thank you for your time!
~Aaron
Re: Indentation of elements in EList using formatting2 API [message #1828087 is a reply to message #1828076] Mon, 01 June 2020 07:45 Go to previous messageGo to next message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 157
Registered: February 2016
Senior Member
Hello Aaron,

You can take a look at the Xtend RichStringFormatter to get some inspiration.

Hope that helps,
Tamás
Re: Indentation of elements in EList using formatting2 API [message #1828101 is a reply to message #1828087] Mon, 01 June 2020 17:53 Go to previous message
Aaron Miller is currently offline Aaron MillerFriend
Messages: 5
Registered: May 2020
Junior Member
Ah, perfect! This saves me a lot of work.

Much appreciated!
Previous Topic:How to infer an annotation with a key-value pair
Next Topic:star files and Decision can match input such as
Goto Forum:
  


Current Time: Thu Apr 18 23:41:19 GMT 2024

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

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

Back to the top