[PATCH] D12407: [clang-format-vs] Add an option to reformat source code when file is saved to disk

Jason Juang via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 27 08:57:29 PDT 2016


jasjuang added a comment.

Just in case someone is interested. I figured out a hack with VCMD that actually does the job.

  using EnvDTE;
  using EnvDTE80;
  
  public class E : VisualCommanderExt.IExtension
  {
    public void SetSite(EnvDTE80.DTE2 DTE_, Microsoft.VisualStudio.Shell.Package package)
    {
      DTE = DTE_;
      events = DTE.Events;
      documentEvents = events.DocumentEvents;
      documentEvents.DocumentSaved += OnDocumentSaved;  
    }
  
    public void Close()
    {
      documentEvents.DocumentSaved -= OnDocumentSaved;
    }
  
    private void OnDocumentSaved(EnvDTE.Document doc)
    {
      if(doc.Language == "C/C++")
      {       
         DTE.ExecuteCommand("Edit.SelectAll");
         DTE.ExecuteCommand("Tools.ClangFormat");
         DTE.ExecuteCommand("View.NavigateBackward");
      }
    }
  
    private EnvDTE80.DTE2 DTE;
    private EnvDTE.Events events;
    private EnvDTE.DocumentEvents documentEvents;
  }

This will invoke clang format whenever a c++ related file is saved


https://reviews.llvm.org/D12407





More information about the cfe-commits mailing list