简体   繁体   English

使用t4模板进行了错误的缩进

[英]Wrong indentation with t4 templates

I'm currently working with T4 templates and I have noticed that sometimes the code is not indented properly, how can I avoid that? 我目前正在使用T4模板,我注意到有时代码没有正确缩进,我怎么能避免这种情况?

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. IE#>总是出现在换行符之后,并且在之后立即添加换行符。

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM