Indent block of statements [message #1721178] |
Mon, 25 January 2016 08:50  |
Eclipse User |
|
|
|
Formatting for nested statement blocks should be as follows:
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 #1721301 is a reply to message #1721286] |
Tue, 26 January 2016 07:37   |
Eclipse User |
|
|
|
hi Anakreontas,
there may be several misunderstandings here
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 #1809868 is a reply to message #1721448] |
Thu, 25 July 2019 01:47  |
Eclipse User |
|
|
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.33847 seconds