获得编辑window..visual工作室扩展选定的文本 [英] get the selected text of the editor window..visual studio extension

查看:98
本文介绍了获得编辑window..visual工作室扩展选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是做了Visual Studio和我需要的就是编辑器窗口中进行进一步的处理所选文本的特定事物的扩展。有人知道什么接口或服务有吗?
以前我需要找到开放式解决方案的路径和我索要实现IVsSolution服务,所以对于这个其他问题,我的事情,必须有一些服务,它为我提供此信息。

hi i'm making a extension for visual studio and the specific thing that i need is get the selected text of the editor windows for further processing. Someone know what interface or service has this? Previously i need to locate the path of the open solution and for that i ask for a service that implements IVsSolution, so for this other problem I thing that there must be some service that provides me this information.

推荐答案

要澄清刚刚拿到viewhost,在堆垛机的回答,这里是你如何能得到当前编辑器查看完整的代码,并从那里ITextSelection,从在Visual Studio 2010中VSPackage的其他地方。特别是,我用这从一个菜单命令回调当前的选择。

To clarify "just get the viewhost" in Stacker's answer, here is the full code for how you can get the current editor view, and from there the ITextSelection, from anywhere else in a Visual Studio 2010 VSPackage. In particular, I used this to get the current selection from a menu command callback.

IWpfTextViewHost GetCurrentViewHost()
{
    // code to get access to the editor's currently selected text cribbed from
    // http://msdn.microsoft.com/en-us/library/dd884850.aspx
    IVsTextManager txtMgr = (IVsTextManager)GetService(typeof(SVsTextManager));
    IVsTextView vTextView = null;
    int mustHaveFocus = 1;
    txtMgr.GetActiveView(mustHaveFocus, null, out vTextView);
    IVsUserData userData = vTextView as IVsUserData;
    if (userData == null)
    {
        return null;
    }
    else
    {
        IWpfTextViewHost viewHost;
        object holder;
        Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
        userData.GetData(ref guidViewHost, out holder);
        viewHost = (IWpfTextViewHost)holder;
        return viewHost;
    }
}


/// Given an IWpfTextViewHost representing the currently selected editor pane,
/// return the ITextDocument for that view. That's useful for learning things 
/// like the filename of the document, its creation date, and so on.
ITextDocument GetTextDocumentForView( IWpfTextViewHost viewHost )
{
    ITextDocument document;
    viewHost.TextView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document);
    return document;
}

/// Get the current editor selection
ITextSelection GetSelection( IWpfTextViewHost viewHost )
{
    return viewHost.TextView.Selection;
}

下面是MSDN的文档进行的 IWpfTextViewHost ,的 ITextDocument 和的 ITextSelection

Here's MSDN's docs for IWpfTextViewHost, ITextDocument, and ITextSelection.

这篇关于获得编辑window..visual工作室扩展选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆