문제

I'm currently working with T4 templates and I have noticed that sometimes the code is not indented properly, how can I avoid that?

For instance I have this code in the template

}
    <# } #>
    this.cmbDecisionList.Dat = dataSource;
    this.btnDec.Enabled = dataSource.Count > 0;
}

and in the generated class it's like

}
                 this.cmbDecisionList.Dat = dataSource;
      this.btnDec.Enabled = dataSource.Count > 0;
}
도움이 되었습니까?

해결책

Allow me to illustrate your problem by replacing spaces with dots.

}
....<# } #>
....this.cmbDecisionList.Dat = dataSource;
    this.btnDec.Enabled = dataSource.Count > 0;
}

and in the generated class it's like

}
........this.cmbDecisionList.Dat = dataSource;
    this.btnDec.Enabled = dataSource.Count > 0;
}

Now, let us remove the preceding dots.

}
<# } #>
....this.cmbDecisionList.Dat = dataSource;
    this.btnDec.Enabled = dataSource.Count > 0;
}

and in the generated class it's like

}
....this.cmbDecisionList.Dat = dataSource;
    this.btnDec.Enabled = dataSource.Count > 0;
}

다른 팁

I think it's good that you strive for readable generated code. We will sit and try to debug the generated code occassionally so it's good if it's easy on the eyes (ofc we never edit the generated code).

I have adopted a pattern where I might sacrifice some readability of the template to gain generated code readability.

Generated code
<#
    T4 statements
#>
Generated code

IE #> always appear after a newline and a newline is added immedietly after.

Your code then would be changed into:

}
<# 
    } 
#>
    this.cmbDecisionList.Dat = dataSource;
    this.btnDec.Enabled = dataSource.Count > 0;
}

That way the generated code tends to be formatted as intended.

It's probably not the only way to retain the formatting as intended but it's the one I use.

Hope this helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top