我可以在VSIX中使用Visual Studio Workspace检测保存(未更改)的文档吗?

时间:2017-01-12 18:18:21

标签: c# visual-studio roslyn vsix

在我的扩展程序中,我使用DTE进行文档保存事件:

this._events2 = (Events2)this._dte.Events;

//setup document events
this._documentEvents = this._events2.DocumentEvents;
this._documentEvents.DocumentSaved += _documentEvents_DocumentSaved;

我正在将我的扩展程序迁移到VS 2017,并希望开始使用Roslyn而不是DTE。我想出了如何获取Visual Studio工作区并点击workspace changed event。现在我可以访问所有这些文档事件

    /// <summary>
    /// A document was added to the current solution.
    /// </summary>
    DocumentAdded = 9,

    /// <summary>
    /// A document was removed from the current solution.
    /// </summary>
    DocumentRemoved = 10,

    /// <summary>
    /// A document in the current solution was reloaded.
    /// </summary>
    DocumentReloaded = 11,

    /// <summary>
    /// A document in the current solution was changed.
    /// </summary>
    DocumentChanged = 12,

但是没有DocumentSaved。每次敲击时DocumentChange都会触发,DocumentReloaded似乎永远不会触发。是否可以仅检测使用roslyn工作区事件保存的文档?

2 个答案:

答案 0 :(得分:1)

要检测文档保存事件(OnBeforeSave()或OnAfterSave()),您可以实现IVsRunningDocTableEvents3接口。您可以通过将此接口实现到帮助程序类并公开公共事件event OnBeforeSaveHandler BeforeSave和公共委托delegate void OnBeforeSaveHandler(object sender, Document document)来实现。

要抓住此事件:runningDocTableEvents.BeforeSave += OnBeforeSave然后您可以使用OnBeforeSave方法编写代码。

IVsRunningDocTableEvents3界面的实现如下所示:

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
}

当触发VS的任何类型的保存命令(CTRL + S,Save All,Compile,Build等)时,我已使用此实现格式化一些文档。

如果您想从某个命令(如编译)获取保存事件,则必须在OnBeforeSave()方法中添加一个检查,添加更多代码

首先,您必须强烈引用 CommandEvents var vommandEvents = dte.Events.CommandEvents并向 CommandEvents commandEvents.BeforeExecute += CommandEventsBeforeExecute;添加新方法。

这是有效的,因为 CommandsEvents 将始终在 BeforeSave 之前调用。这就是Visual Studio中的工作方式,每个操作都代表一个需要一些步骤和事件的命令(例如,编译命令包含在其工作流程中,任何事情之前,保存文档事件)。

public override void OnBeforeSave(object sender, Document aDocument)
{
  if (false == myCompileCommandFlag)
    return;

  // write your code here 

}

public void CommandEventsBeforeExecute(string aGuid, int aId, object aCustomIn, object aCustomOut, ref bool aCancelDefault)
{
  string commandName = GetCommandName(aGuid, aId);
  if (0 != string.Compare("Build.Compile", commandName))
  {
    return;
  }
  myCompileCommandFlag= true;
}

public string GetCommandName(string aGuid, int aId)
{
  if (null == aGuid)
    return string.Empty;

  if (null == mCommand)
    return string.Empty;

  Command cmd = mCommand.Item(aGuid, aId);
  if (null == cmd)
    return string.Empty;

  return cmd.Name;
}

答案 1 :(得分:0)

你有DTE2界面,你有Events.DocumentEvents.DocumentSaved 我没有尝试过,但看起来很有希望。