【问题标题】:How to tell if the active document is a text document?如何判断活动文档是否为文本文档?
【发布时间】:2017-10-10 16:07:34
【问题描述】:

我正在开发一个 Visual Studio 扩展,其中仅当活动文档是文本文档时,其中一个已实现的命令才可用(例如 Visual Studio 的“切换书签”)。问题是我不知道如何判断是这种情况。

现在我有一个半有效的解决方案。在包的Initialize 方法中,我订阅了DTE 的WindowActivated 事件,然后每当激活一个窗口时,我检查窗口DocumentData 属性是否属于TextDocument 类型:

protected override void Initialize()
{
    base.Initialize();

    var dte = Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
    dte.Events.WindowEvents.WindowActivated += WindowEventsOnWindowActivated;

    //More initialization here...
}

//This is checked from command's BeforeQueryStatus
public bool ActiveDocumentIsText { get; private set; } = false;

private void WindowEventsOnWindowActivated(Window gotFocus, Window lostFocus)
{
    if (gotFocus.Kind != "Document")                
        return; //It's not a document (e.g. it's a tool window)

    TextDocument textDoc = gotFocus.DocumentData as TextDocument;
    ActiveDocumentIsText = textDoc != null;
}

这种方法的问题在于 1) Window.DocumentData is documented as ".NET Framework internal use only" 和 2) 当同时具有代码视图和设计视图的文档(例如 .visxmanifest 文件)在设计模式下打开时,这会产生误报.

我也尝试过使用IVsTextManager.GetActiveView,但这会返回打开的 last 活动文本视图 - 因此,如果我打开一个 .txt 文件,然后打开一个 .png 文件,它会返回数据.txt 文件,即使它不再是活动文档。

那么,我如何检查活动文档是文本文档,还是可以有设计器的文档的代码视图……如果可能,不使用“未记录的”类/成员?

更新:我找到了一个稍微好一点的解决方案。在窗口激活处理程序内:

ActiveDocumentIsText = gotFocus.Document.Object("TextDocument") != null;

至少this one is properly documented,但我还是有设计师误报的问题。

【问题讨论】:

    标签: visual-studio visual-studio-extensions vsix


    【解决方案1】:

    我终于明白了。这有点棘手,但它有效并且100%“合法”。这是食谱:

    1- 使包类实现IVsRunningDocTableEvents。让所有方法都只是return VSConstants.S_OK;

    2- 将以下字段和以下辅助方法添加到包类中:

    private IVsRunningDocumentTable runningDocumentTable;
    
    private bool DocIsOpenInLogicalView(string path, Guid logicalView, out IVsWindowFrame windowFrame)
    {
        return VsShellUtilities.IsDocumentOpen(
            this,
            path,
            VSConstants.LOGVIEWID_TextView,
            out var dummyHierarchy2, out var dummyItemId2,
            out windowFrame);
    }
    

    3- 在包类的Initialize方法中添加以下内容:

    runningDocumentTable = GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;
    runningDocumentTable.AdviseRunningDocTableEvents(this, out var dummyCookie);
    

    4- 别眨眼,魔法来了!如下实现IVsRunningDocTableEvents.OnBeforeDocumentWindowShow方法:

    public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
    {
        runningDocumentTable.GetDocumentInfo(docCookie,
            out var dummyFlags, out var dummyReadLocks, out var dummyEditLocks,
            out string path, 
            out var dummyHierarchy, out var dummyItemId, out var dummyData);
    
        IVsWindowFrame windowFrameForTextView;
        var docIsOpenInTextView =
            DocIsOpenInLogicalView(path, VSConstants.LOGVIEWID_Code, out windowFrameForTextView) ||
            DocIsOpenInLogicalView(path, VSConstants.LOGVIEWID_TextView, out windowFrameForTextView);
    
        //Is the document open in the code/text view,
        //AND the window for that view is the one that has been just activated?
    
        ActiveDocumentIsText = docIsOpenInTextView && pFrame == logicalViewWindowFrame;
    
        return VSConstants.S_OK;
    } 
    

    【讨论】:

    • 看起来 DocIsOpenInLogicalView 不使用logicalView 参数,因此看起来只需使用LOGVIEWID_TextView 调用一次VsShellUtilities.IsDocumentOpen。但是这个答案比我找到的任何其他答案都更好,因为它可以提供有关如何使用这些指南的线索!
    猜你喜欢
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 2011-04-09
    • 2012-09-01
    • 2012-08-18
    • 1970-01-01
    相关资源
    最近更新 更多