jsmouret added a comment.

A slightly improved version of @jasjuang code.
Restores current selection, and really saves if modified.

  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++")
      {
        TextSelection selection = doc.Selection as TextSelection;
        int begin = selection.TopPoint.AbsoluteCharOffset;
        int end = selection.BottomPoint.AbsoluteCharOffset;
  
        selection.SelectAll();
        DTE.ExecuteCommand("Tools.ClangFormat");
  
        selection.MoveToAbsoluteOffset(begin, false);
        selection.MoveToAbsoluteOffset(end, true);
  
        if (!doc.Saved)
        {
          doc.Save();
        }
  
      }
    }
  
    private EnvDTE80.DTE2 DTE;
    private EnvDTE.Events events;
    private EnvDTE.DocumentEvents documentEvents;
  }


https://reviews.llvm.org/D12407



_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to