Unable to indent code with TextGen

I was not able to indent code from TextGen, this
append {/** test 1} \n ; 
with indent { 
  append {Line1} \n ; 
  append {Line2} \n ; 
} 
append \n ;
prettyPrint();
result in
/** test 1
Line1
Line2
prettyPrint();
Then I looked at ForStatement_TextGen, here foreach is used so I tried
string[] lines1 = {"Line 1", "Line 2"}; 
append {/** test 2} \n ; 
with indent { 
  foreach line in lines1 { 
    append ${line} \n ; 
  } 
} 
append \n ;
prettyPrint();
with result
/** test 2
Line 1
Line 2
prettyPrint();
Using $list{} has the same result.

I successfully indented code where my node is not a root node with (node type is a concept similar to a /** */ comment from baseLanguage)
with indent { 
  foreach line in node.lines { 
    append ${line} \n ; 
  } 
}
prettyPrint();
But I don't know why it's actually working or better why the other cases are not working.
Thx
4 comments
Due to a java compilation error I came to my *_TextGen.java classes that I did not know, and the mentioned behavior is due to a missing this.indexBuffer() into the non-functioning code, that is translated into:
    this.increaseDepth();
    for (String line : lines) {
      this.append(line);
      this.appendNewLine();
    }
    this.decreaseDepth();lti
prettyPrint();
whereas the working example is translated into
    this.increaseDepth();
    for (SNode line : ListSequence.fromList(SLinkOperations.getTargets(node, "lines", true))) {
      this.indentBuffer(); // <-- this is missing into the above code.
      appendNode(line);
      this.appendNewLine();
    }
    this.decreaseDepth();
prettyPrint();
It's strange because the source TextGen code appears, to me, the same... it's a with index + foreach construct although the type of the cycle variable is different.
0
There's a non-obvious difference in the Inspector for the appended text, e.g. ${line} - set the "with indent" flag to true.
Alternatively, you can manually insert the current indentation by the "indent buffer;" command before calling "append".
0
Hi, inspecting how with indent and append are implemented gives me a better understanding (reduce_withIndentOperation).
However for a beginner like me a better name for with indent could be with increasing depth seen that "with indent" does not indent all the statements it contains (that should be indented one by one through the inspector), it just sorrounds the statements with increase/decrease depth.
Nevertheless MPS is a great tool and thx to point me to the inspector.
0
Indeed, the "indentation" must be inserted into the output text buffer explicitly by "appends". As you say, the "with indent" block only increases the current size of the indentation, but the individual "appends" may or may not wish to be prepended with the indentation buffer of the current size.

For example, the TextGen for ReturnStatement:

text gen component for concept ReturnStatement { 
  (context, buffer, node)->void { 
    append \n {return} ; 
    if (node.expression.isNotNull) { 
      append { } ${node.expression} ; 
    } 
    append {;} ; 
  } 
}
prettyPrint();

only the first append - "{return}" - needs to be indented, not the other ones, since these should follow right after the "return".
0

Please sign in to leave a comment.