Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Indent block of statements(IFormatter2)
Indent block of statements [message #1721178] Mon, 25 January 2016 13:50 Go to next message
Anakreontas Mentis is currently offline Anakreontas MentisFriend
Messages: 85
Registered: October 2014
Member
Formatting for nested statement blocks should be as follows:
{
    line1
    line2
}

I used this code adapted from example.domainmodel.formatting2.DomainmodelFormatter
       //Block of statements
	def dispatch void format(StatementGroup entity, extension IFormattableDocument doc) {
		val open = entity.regionFor.ruleCallTo(LBRACERule)  //{
		val close = entity.regionFor.ruleCallTo(RBRACERule) //}
		if (open != null && close != null) {
			interior(open, close)[indent]
			entity.directives.forEach[format]
		}
	}

        //Contains a single line statement
	def dispatch void format(Statement s, extension IFormattableDocument doc) {
		s.prepend[noSpace; noAutowrap].append[setNewLines(0, 1, 1)]
	}


Formatting rules affect only the lines that are not indented at all (have no space). Lines that have fewer or more spaces then the indentation level are not affected. For example:

{
    line1 //Correctly indented. Not affected
line2  //Will be correctly indented
  line3 //Not affected but it should
        line4 //Not affected but it should
}


How can I apply the indentation rules on all lines?
Also, how can I use spaces instead of tabs for indentation?

Regards
Re: Indent block of statements [message #1721267 is a reply to message #1721178] Tue, 26 January 2016 08:51 Go to previous messageGo to next message
Moritz Eysholdt is currently offline Moritz EysholdtFriend
Messages: 161
Registered: July 2009
Location: Kiel, Germany
Senior Member
Hi Anakreontas,

your
//Contains a single line statement
	def dispatch void format(Statement s, extension IFormattableDocument doc) {
		s.prepend[noSpace; noAutowrap].append[setNewLines(0, 1, 1)]
	}


looks suspicious. It's formatting the whitespace before a statement as well as after a statement. But you may only format each whitespace once. I strongly assume that the whitespace after the first statement is in fact the whitespace before the second whitespace. You should be getting "conflicting formatting" exceptions in these cases.

Also, calling "noAutowrap" may not be necessary because by default there is no autowrap. The method does only exist to selectively turn off autowrap for a few whitespaces after you turned it on explicitly for many whitespaces.

Quote:
Also, how can I use spaces instead of tabs for indentation?


You can set the characters used for indentation by setting a values for key org.eclipse.xtext.formatting2.FormatterPreferenceKeys.indentation in org.eclipse.xtext.formatting2.FormatterRequest.getPreferences().

regards,
Moritz
Re: Indent block of statements [message #1721286 is a reply to message #1721267] Tue, 26 January 2016 10:47 Go to previous messageGo to next message
Anakreontas Mentis is currently offline Anakreontas MentisFriend
Messages: 85
Registered: October 2014
Member
Hello Moritz. Thank you for providing feedback.

I removed noAutowrap. Replacing tabs with spaces works fine. Thanks.

The formatter does not raise exceptions and it has no effect on the text If I remove noSpace.

I changed the implementation for format(Statement) to:
s.prepend[noSpace].append[noSpace; setNewLines(0, 1, 1)]


My understanding is that the formatting rule above is applied on the region corresponding to the Statement (in the examples that would be 'line1'). The rule is not concerned with indentation spaces proceeding the statement region. Therefore the final result would be:
[indentation space because of format(StatementGroup)][remove any spaces][statement region][remove trailing spaces][at most one new line]

Probably I don't understand correctly the meaning of the formatting rules because I don't see that output when an incorrect number of spaces precede the statement.

Regards
Re: Indent block of statements [message #1721301 is a reply to message #1721286] Tue, 26 January 2016 12:37 Go to previous messageGo to next message
Moritz Eysholdt is currently offline Moritz EysholdtFriend
Messages: 161
Registered: July 2009
Location: Kiel, Germany
Senior Member
hi Anakreontas,

there may be several misunderstandings here Smile

about indentation: Indentation is only applied when the formatter decided to insert one or more newLines. Basically, whenever the formatter does a newLine it also checks what the current indentation level for that line is an applies the indentation. That's important to unterstand because "noSpace" does not do a newLine (as the name indicates). Also setNewLines(0, 1, 1) does not force the formatter to insert a newLine either, because the first parameter (0) tells the formatter that for the current HiddenRegion at least zero newLines are required.

Quote:
My understanding is that the formatting rule above is applied on the region corresponding to the Statement (in the examples that would be 'line1'). The rule is not concerned with indentation spaces proceeding the statement region. Therefore the final result would be:
[indentation space because of format(StatementGroup)][remove any spaces][statement region][remove trailing spaces][at most one new line]


not quite. When you put multiple formatting instructions between two SemanticRegions (non-whitespace-text) they're being merged, not concatenated. See
org.eclipse.xtext.formatting2.internal.HiddenRegionFormatting.mergeValuesFrom(IHiddenRegionFormatting)
for how the merging works.

Also, the characters that are within the same IHiddenRegion are by default whitespace, tabs, *and* newLines.

Consequently, in your example { statement1 statement2 } there is only *one* IHiddenRegion between statement1 and statement2. Thus doing append() on statement1 affects the *same* hiddenRegion as doing prepend() on statement2.

To be on the safe side, you may try

s.append[setNewLines(1, 1, 1)]
Re: Indent block of statements [message #1721325 is a reply to message #1721301] Tue, 26 January 2016 15:31 Go to previous messageGo to next message
Anakreontas Mentis is currently offline Anakreontas MentisFriend
Messages: 85
Registered: October 2014
Member
If I have a SemanticRegion s (Statement from my example) and two formatting instructions, say b and e (s.prepend[b].append[e]) which of the following is true
1. Formatter applies b on the hidden region preceding s and e on the hidden region following s.
2. applies both b and e on the hidden region preceding s. Append and prepend only affect the order in which b and e apply
3. applies both b and e on the hidden region following s. Etc...

Regards
Re: Indent block of statements [message #1721326 is a reply to message #1721325] Tue, 26 January 2016 15:36 Go to previous messageGo to next message
Moritz Eysholdt is currently offline Moritz EysholdtFriend
Messages: 161
Registered: July 2009
Location: Kiel, Germany
Senior Member
(1) is true. And now think of what's happening if you have *two* SemanticRegions s1 and s2 and apply formatting for both of them: s1.e collides with s2.b
Re: Indent block of statements [message #1721448 is a reply to message #1721326] Wed, 27 January 2016 14:40 Go to previous messageGo to next message
Anakreontas Mentis is currently offline Anakreontas MentisFriend
Messages: 85
Registered: October 2014
Member
Yes, I see. I changed the format for Statement to s.prepend[noSpace; setNewLines(1, 1, 1)] but it does not what I expected.
   Input                    Expected      Output
1 \n___\n\n_\n               ____\n       \n___\n\n____\n
2 st1                        st1          st1
3 \n____                    \n____        \n____\n____
4 st2                        st2           st2


I assumed that the hidden regions for st1 and st2 are lines (1) and (3) respectively. If this is the case, why are not the excessive new lines and spaces striped from hidden region (1)? In case this is relevant, my grammar does not extend another grammar (e.g. Terminals) and it does not specify hidden terminals.

Which class implements the interpreter of the layout mini-language?

Regards
Re: Indent block of statements [message #1809868 is a reply to message #1721448] Thu, 25 July 2019 05:47 Go to previous message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 54
Registered: February 2016
Member
Hi Anakreontas,

as you mentioned above, you successfully configured the Formatter to use spaces instead of tabs for indentation.

Could you please share the code snippet how to do that?

Thanks in advance,
Tamás
Previous Topic:Auto-Completion cross-reference from a non-xtext model
Next Topic:Multiple QualifiedProviders within a DSL language
Goto Forum:
  


Current Time: Fri Sep 20 05:11:23 GMT 2019

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

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

Back to the top