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
}