惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google Online Security Blog
Google Online Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
Project Zero
Project Zero
E
Exploit-DB.com RSS Feed
S
Secure Thoughts
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
NISL@THU
NISL@THU
WordPress大学
WordPress大学
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
小众软件
小众软件
P
Privacy & Cybersecurity Law Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hacker News: Front Page
F
Full Disclosure
Latest news
Latest news
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
G
GRAHAM CLULEY
Forbes - Security
Forbes - Security
V
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
P
Privacy International News Feed
C
Check Point Blog
N
News and Events Feed by Topic

博客园 - winkingzhang

Build 2016概览 WinRT开发系列之编程语言:功能和效率 WinRT开发系列之基础概念:WinRT不是…… VS2010 Extension实践(3)——实现自定义配置 [VS2010 Extension]PowerExtension.GoToDefinition VS2010 Extension实践 如何通过反射调用带有ref或者out的参数的方法[迁移] Win7硬盘安装和移动硬盘访问出错的修复办法[迁移] zt. Windows Mobile开发文章收藏 [WPF]在Style中设置ToolTip的问题分析 [WPF]RadioButton在Group的Header区部分不响应鼠标选择的bug分析 WPF模式思考 (zt) How to Get IIS Web Sites Information Programmatically Visual Studio 调试器 Application Request Routing and the IIS 7.0 Web Management Service - winkingzhang Reference Resources for MOSS and WSS - winkingzhang FileSystemWatcher事件多次触发的解决方法 - winkingzhang ASP.NET Application Life Cycle - winkingzhang CAS and Native Code - winkingzhang
VS2010 Extension实践(2)
winkingzhang · 2010-02-04 · via 博客园 - winkingzhang

在上一篇(VS2010 Extension实践)里,主要展示了如何使用MEF扩展VS2010,来扩展编辑控制和展现自己的UI;在实现QuickToolbar的时候,发现MEF仅仅提供了很基本的编辑控制,如果需要高级的操作,比如注释选择的代码,就捉襟见肘,很是麻烦。

本篇我将展示如何深入挖掘VS2010 Extension,使它成为锋利的军刀,而不是绣花枕头。鉴于此,这里就从上面提到了的Feature——注释和取消注释选择的代码来剖析,希望可以为大家拓宽思路,更好的利用VS2010。

首先回顾一下上篇中的实现,当时是基于TextViewLine做注释代码的,这里有两个潜在问题:其一,TextViewLine,顾名思义,是“可视区域”的行,所以如果选择超出可视区域,超出的部分就没有注释掉;其二,当选择的结束位置在行的结尾时,无法实现IDE注释代码后保持Caret在选择结尾而不跳到下一行的行为,当尝试自己重新选择并移动Caret就会收到ITextSpanshot无效的异常。

上面提到了VS2010 Extension对编辑器的编辑行为的控制能力仅仅提供了通用的,比如Cut/Copy/Paste等等,而其他的诸如注释/取消注释代码,添加、删除、导航到Bookmark等程序员常用功能没有暴露出来,具体可以参考IEditorOperations Interface(http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.text.operations.ieditoroperations_methods%28VS.100%29.aspx),这里的所有Member表达了其所支持的编辑操作。总之,这条路只有这么几个目的地。

那么,还有其他方法吗?貌似走到了死胡同了,但是当我们使用IDE时候,却是可以很容易的通过Edit菜单找到所有的功能的,问题是,它们要怎样才能为我所用呢?

我首先想到的是在VSSDK中找找,结果一个名字看起来很顺眼的接口撞到眼里,它就是IVsUIShell Interface(http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.interop.ivsuishell%28VS.100%29.aspx),MSDN上市这么说的:

This interface provides access to basic windowing functionality, including access to and creation of tool windows and document windows. provided by the environment.

也就是说这是一个由IDE提供的全局的Service,可以创建、访问工具窗口和编辑窗口。浏览一下这个所有Member,发现了一个叫IVsUIShell.PostExecCommand(...)(http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.interop.ivsuishell.postexeccommand%28VS.100%29.aspx)的方法,MSDN描述说通过它可以异步执行Command,那么,只要找到注释代码的Command,在通过这个接口就可以实现VS IDE一样的注释代码的Feature了。酷毙了,就是它,当怎么得到它呢?现在请留心MSDN上的解释,就是上面我使用红色粗体表示出来的部分——这个由IDE提供的全局的Service,那么可以通过Package.GetGlobalService(...)(http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.package.getglobalservice%28VS.100%29.aspx)来获取:

IVsUIShell shell = Package.GetGlobalService(typeof(IVsUIShell)) as IVsUIShell;

接下来是找到自己需要Command,然后PostExecCommand就搞定了;而VS提供的Command有两部分组成:Guid和CommandID,这个大部分都在VSConstants Class(http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vsconstants%28VS.100%29.aspx)里面,以注释代码为例,其Guid是:VsConstants.VSStd2k,而CommandID是VSConstants.VSStd2kCmdID.COMMENTBLOCK。下面是我包装的注释和取消注释的代码片段:

代码

1 public static void ProcessComments(bool comment)
2 {
3 IVsUIShell shell = Package.GetGlobalService(typeof(IVsUIShell)) as IVsUIShell;
4 if (shell != null)
5 {
6 Guid std2k = VSConstants.VSStd2K;
7 uint cmdId = comment ?
(
uint)VSConstants.VSStd2KCmdID.COMMENT_BLOCK :
(
uint)VSConstants.VSStd2KCmdID.UNCOMMENT_BLOCK;
8 object arg = null;
9 shell.PostExecCommand(ref std2k, cmdId, 0, ref arg);
10 }
11 }

至此,我们通过VSSDK提供的能力,顺利的挖掘出VS2010 Extension的部分宝藏,你是不是也有点心动,要自己去挖掘一点呢?

PS:

本文所讲解内容是根据我的练习之作而进行的,如果需要源代码,请自行到GCDN论坛下载,代码和VSIX安装程序均匀更新

[VS2010扩展]浮动工具栏(http://gcdn.grapecity.com/showtopic-345.html