T4 indentation in generated C# code
Asked Answered
B

3

9

When using T4 to generate C# code, I can't get correct identation with TABS scattered around:

public partial class Foo : Base
{
        public int C { get; set; }
        [MappedProperty("A.B[{C}].Foo")]
    public int Foo { get; set; }
}

I'm using a seemingly correctly indented .TT code similar to the following:

public partial class <#= ViewModelName #>
{
    <#  foreach(var property in ViewModelProperties) { #> 
        <# if(property.Mapping != null) { #>
        [MappedProperty("<#= property.Mapping #>")]
        <# } #>
        public <#= property.TypeDeclaration #> <#= property.MemberName #> { get; set; }
    <# } #>
}

This code snippet reflects what I've already tried to do: make control statements and blocks to a single line as much as possible.

Berthaberthe answered 3/11, 2015 at 14:5 Comment(1)
try move public ... on same line as if end: <# } #>public ...Balzer
Z
11

I like to do it this way and never had any problems.

public partial class <#= ViewModelName #>
{
<#
    foreach(var property in ViewModelProperties) { 
        if(property.Mapping != null) { 
#>
    [MappedProperty("<#= property.Mapping #>")]
<#
        }
#>
    public <#= property.TypeDeclaration #> <#= property.MemberName #> { get; set; }
<#
    }
#>
}
Zelikow answered 3/11, 2015 at 14:16 Comment(1)
yeap, clean way to go, I do the same and avoid most of the issuesAnticatalyst
D
6

Use PushIndent(), PopIndent() and ClearIndent() like so:

public partial class <#= ViewModelName #>
{
<# PushIndent("   "); #>
<#  foreach(var property in ViewModelProperties) { #> 
<# if(property.Mapping != null) { #>
[MappedProperty("<#= property.Mapping #>")]
<# } #>
public <#= property.TypeDeclaration #> <#= property.MemberName #> { get; set; }
<# } #>
<# PopIndent(); #>
}

or alternatively...

public partial class <#= ViewModelName #>
{
<# 
   PushIndent("   "); 
   foreach(var property in ViewModelProperties) {
      if(property.Mapping != null) { 
         WriteLine("[MappedProperty({0})]", property.Mapping);
      }
      WriteLine("public {0} {1} {{ get; set; }}", property.TypeDeclaration, property.MemberName);
   }
   PopIndent(); 
#>
}
Dillingham answered 3/11, 2015 at 14:17 Comment(1)
I think its not how most people expect to define their templates, not very readable, but still, nice approach.Anticatalyst
A
1

you need to be a bit less messy with your whitespaces and '<#' and the result will be more expectable:

  1. you dont need to use '<#' in half of the places, foreach and if may stay within one. That way you'll reduce the potential whitespaces outside tags and make it more readable.

  2. your public line starts with double tab, so it generates a property with indented with double tab instead of a single one. Remove one.

  3. all the whitespaces outside '<#' tags will be printed, remove them, leave only what's necessary outside the tags (before and after them). Otherwise they will accumulate and break your formatting.

Anticatalyst answered 3/11, 2015 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.