locked
T4: Indent output file RRS feed

  • Question

  • Hi, i'm trying indent output file executing the code above :

    public void indentText()
    {
      var dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
      dte.ExecuteCommand("File.OpenFile", "TextTemplate3.cs");
      dte.ActiveDocument.DTE.ExecuteCommand("Edit.FormatDocument");
      dte.ActiveDocument.Close(vsSaveChanges.vsSaveChangesYes);
    }
    This code works perfectly in the existant .cs files in project, but don't works in output file of my text templates...

    Example "TextTemplate3.tt":

    <#@ template debug="true" language="C#" hostspecific="true" #>
    <#@ output extension=".cs" #>
    <#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
    <#@ import namespace="EnvDTE" #>
    <#@ import namespace="EnvDTE80" #>
    <#@ assembly name="EnvDTE" #>
    <#@ assembly name="EnvDTE80" #>
    namespace WriterTest
    {
    public class Teste{
    	public object model{
    get;
    set;
    }
    }
    }
    <#
      indentText();
    #>
    <#+
    public void indentText()
    {
      var dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
      dte.ExecuteCommand("File.OpenFile", "TextTemplate3.cs");
      dte.ActiveDocument.DTE.ExecuteCommand("Edit.FormatDocument");
      dte.ActiveDocument.Close(vsSaveChanges.vsSaveChangesYes);
    }
    #>

    Output file "TextTemplate3.cs":

    namespace WriterTest
    {
    public class Teste{
    	public object model{
    get;
    set;
    }
    }
    }
    

    I would like the file output was like this:

    namespace WriterTest
    {
      public class Teste
      {
        public object model
        {
          get;
          set;
        }
      }
    }
    
    I already did some tricks to execute the "indentText()" method like re-writting the "indentText()" method in another TT file and run after output file be generated.

    I would like to know if that is possible. I'm thinking to write a preprocessedtextemplate to solve this issue but I haven't warranties that works:/

    Thanks.

    Best Regards,

    Ricardo Vilas Boas

     

     

    Thursday, February 24, 2011 4:15 PM

Answers

  • Hi Ricardo,

    I haven't actually tried this, but that GetActiveObject call may (or may not) give you the DTE interface from the instance of DevEnv.exe you think it will. If more than one instance of VS is running, you'll likely just get the last one placed in the Running Object Table. Generally speaking, I haven't seen anyone attempt to automate the IDE from a template like this.

    Sincerely,


    Ed Dore
    Wednesday, June 15, 2011 6:39 PM

All replies

  • Hello Ricardo,

     

    Thank you for your question.

     

    I am currently looking into this issue and will give you an update as soon as possible.

     

    Thank you for your understanding and support.

     

     

    Best Regards,

    Ziwei Chen

     

     

     


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 28, 2011 9:43 AM
  • Hi Ricardo,

    I haven't actually tried this, but that GetActiveObject call may (or may not) give you the DTE interface from the instance of DevEnv.exe you think it will. If more than one instance of VS is running, you'll likely just get the last one placed in the Running Object Table. Generally speaking, I haven't seen anyone attempt to automate the IDE from a template like this.

    Sincerely,


    Ed Dore
    Wednesday, June 15, 2011 6:39 PM