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

问题描述 投票:3回答:2

在我的扩展中,我使用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工作区事件保存的文档?

c# visual-studio roslyn vsix
2个回答
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等)时,我已经使用此实现来格式化一些文档。

如果你想从像Compile这样的特定命令中获取save事件,你必须在OnBeforeSave()方法中添加一个检查,添加一些代码

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

这将起作用,因为将始终在BeforeSave之前调用CommandsEvents。这就是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;
}

0
投票

你有DTE2接口,你有Events.DocumentEvents.DocumentSaved我没有尝试,但它看起来很有前景。

© www.soinside.com 2019 - 2024. All rights reserved.