1

I want to create a template that's indented 4 spaces, like below:

def myMethod() '''
        for (int i =0; i!= size; ++i) {
            doSomething();
        }
    '''

But Xtend removes the 4 spaces before the for() and the closing '}'. How can I add indentation that's not removed?

Adam Horvath
  • 1,249
  • 1
  • 10
  • 25

1 Answers1

2

I have had similar issues, Xtend's template system can be finicky, but there are workarounds. When using something like the method you showed I usually find I call it from another part of the template and you can create the indentation in the calling method. For instance:

def callingMethod() {'''
    for (1 to 10) {
        «myMethod()»  «««   This puts indents before everything within the method
    }
'''}
def myMethod() {'''
    for (int i =0; i!= size; ++i) {
        doSomething();
    }
'''}

Another option is to explicitly add whitespace within the template such as:

def myMethod() {'''
    «"    "»for (int i =0; i!= size; ++i) {
    «"    "»    doSomething();
    «"    "»}
'''}

Or another way I found looking just now

def myMethod() {'''
«""»
    for (int i =0; i!= size; ++i) {
        doSomething();
    }
«""»
'''}

Personally I think it is much cleaner the first way where possible. There are probably other ways to achieve this as well, these are just a few things I have found in my own work.

Zannith
  • 429
  • 4
  • 21
  • Your first solution is what the Xtend user manual says, which is not working for me. I have no idea why. So I went for your second option, thank you! – Adam Horvath Jul 10 '18 at 16:42
  • Glad I could be of help, also note in the third method from the tests I ran it doesn't put a new line where I have the empty quotes, it just treats it as nothing but creates the indentation – Zannith Jul 11 '18 at 14:13