简体   繁体   English

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

[英]Can I detect document saved (not changed) with Visual Studio Workspace in a VSIX?

In my extension I use to tap into the document saved event using DTE: 在我的扩展中,我使用DTE进入文档保存事件:

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

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

I am migrating my extension to VS 2017 and want to start using the Roslyn stuff instead of DTE. 我正在将我的扩展迁移到VS 2017,并希望开始使用Roslyn而不是DTE。 I figured out how to get the Visual Studio Workspace and tap into the workspace changed event . 我想出了如何获取Visual Studio工作区并点击工作区更改事件 Now I have access to all these document events 现在我可以访问所有这些文档事件

    /// <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,

But there is no DocumentSaved. 但是没有DocumentSaved。 DocumentChange fires every time a keystroke is made and DocumentReloaded never seems to fire at all. 每次敲击时DocumentChange都会触发,DocumentReloaded似乎永远不会触发。 Is it possible to detect just a document saved using the roslyn workspace events? 是否可以仅检测使用roslyn工作区事件保存的文档?

To detect document save event( OnBeforeSave() or OnAfterSave() ) you can implement IVsRunningDocTableEvents3 interface. 要检测文档保存事件(OnBeforeSave()或OnAfterSave()),您可以实现IVsRunningDocTableEvents3接口。 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) . 您可以通过将此接口实现到帮助程序类并公开公共事件event OnBeforeSaveHandler BeforeSave和公共委托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. 要捕获此事件: runningDocTableEvents.BeforeSave += OnBeforeSave然后您可以在OnBeforeSave方法中编写代码。

My implementation of the IVsRunningDocTableEvents3 interface look like this: 我对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
}

I have used this implementation to format some documents when any kind of save command (CTRL + S, Save All, Compile, Build etc) from VS was triggered. 当触发VS的任何类型的保存命令(CTRL + S,Save All,Compile,Build等)时,我已经使用此实现来格式化一些文档。

If you want to get the save event from a certain command like Compile you must add a check in your OnBeforeSave() method add some more code 如果你想从像Compile这样的特定命令中获取save事件,你必须在OnBeforeSave()方法中添加一个检查,添加一些代码

First you must to keep a strong reference to CommandEvents var vommandEvents = dte.Events.CommandEvents and add a new method to the CommandEvents commandEvents.BeforeExecute += CommandEventsBeforeExecute; 首先,你必须保持一个强有力的参考CommandEvents var vommandEvents = dte.Events.CommandEvents并添加新的方法到CommandEvents commandEvents.BeforeExecute += CommandEventsBeforeExecute; .

This will work because the CommandsEvents will always be called before the BeforeSave . 这将起作用,因为将始终在BeforeSave之前调用CommandsEvents This is how things works in Visual Studio, every action represents a command which requires some steps and events (Eg. Compile command contains in its workflow, before of anything, the save document event). 这就是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;
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM