0

I am creating a .vsix (plugin) project for accessing VS Editor data or text. I am able to see the editor's text in debug console but now I want all the text shown on saving the document. How can I handle on saved event?

I tried following code but didn't work.

public void FormEvents_Save(object sender, SaveEventArgs<ITextView> e)
{
   MessageBox.Show("Saved!!");
}

How can I handle on saved event?

Tanmay Bairagi
  • 564
  • 5
  • 25
  • 1
    Does https://stackoverflow.com/questions/9844900/visual-studio-sdk-handle-file-save-event help? – Athanasios Kataras Dec 11 '19 at 06:54
  • 1
    Does this answer your question? [Visual Studio SDK - Handle File Save Event](https://stackoverflow.com/questions/9844900/visual-studio-sdk-handle-file-save-event) – jazb Dec 11 '19 at 06:57
  • @AthanasiosKataras No, It didn't help. I am very beginner so it might be the reason. Thanks by the way. Looking forward for a better explanation. – Tanmay Bairagi Dec 11 '19 at 09:48

1 Answers1

1

To detect document save event( OnBeforeSave() or OnAfterSave() ) you can implement IVsRunningDocTableEvents3 interface. You can do that by implementing this interface into a helper class and exposing a public event event OnBeforeSaveHandler BeforeSave and a public delegate delegate void OnBeforeSaveHandler(object sender, Document document).

To catch this event just : runningDocTableEvents.BeforeSave += OnBeforeSave and then you can write your code in the OnBeforeSave method.

I have used this implementation to format the code style of the documents when any kind of save command (CTRL + S, Save All, Compile, Build and so on) from VS was triggered.

My implementation of the IVsRunningDocTableEvents3 interface look like this:

public class RunningDocTableEvents : IVsRunningDocTableEvents3
{
  #region Members

  private RunningDocumentTable mRunningDocumentTable;
  private DTE mDte;

  public delegate void OnBeforeSaveHandler(object sender, Document document);
  public event OnBeforeSaveHandler BeforeSave;

  #endregion

  #region Constructor

  public RunningDocTableEvents(Package aPackage)
  {
    mDte = (DTE)Package.GetGlobalService(typeof(DTE));
    mRunningDocumentTable = new RunningDocumentTable(aPackage);
    mRunningDocumentTable.Advise(this);
  }

  #endregion

  #region IVsRunningDocTableEvents3 implementation

  public int OnAfterAttributeChange(uint docCookie, uint grfAttribs)
  {
    return VSConstants.S_OK;
  }

  public int OnAfterAttributeChangeEx(uint docCookie, uint grfAttribs, IVsHierarchy pHierOld, uint itemidOld, string pszMkDocumentOld, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew)
  {
    return VSConstants.S_OK;
  }

  public int OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame)
  {
    return VSConstants.S_OK;
  }

  public int OnAfterFirstDocumentLock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
  {
    return VSConstants.S_OK;
  }

  public int OnAfterSave(uint docCookie)
  {
    return VSConstants.S_OK;
  }

  public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
  {
    return VSConstants.S_OK;
  }

  public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
  {
    return VSConstants.S_OK;
  }

  public int OnBeforeSave(uint docCookie)
  {
    if (null == BeforeSave)
      return VSConstants.S_OK;

    var document = FindDocumentByCookie(docCookie);
    if (null == document)
      return VSConstants.S_OK;

    BeforeSave(this, FindDocumentByCookie(docCookie));
    return VSConstants.S_OK;
  }

  #endregion

  #region Private Methods

  private Document FindDocumentByCookie(uint docCookie)
  {
    var documentInfo = mRunningDocumentTable.GetDocumentInfo(docCookie);
    return mDte.Documents.Cast<Document>().FirstOrDefault(doc => doc.FullName == documentInfo.Moniker);
  }

  #endregion
}
Ionut Enache
  • 461
  • 8
  • 22