Visual studio 2012 在自定义编辑器中处理标准命令
我已经创建了一个VisualStudio扩展,通过实现Visual studio 2012 在自定义编辑器中处理标准命令,visual-studio-2012,editor,vsix,Visual Studio 2012,Editor,Vsix,我已经创建了一个VisualStudio扩展,通过实现IClassiferProvider来突出显示提供程序语法。我想添加其他功能,例如支持标准的Edit.CommentSelection和Edit.FormatDocument命令,但我不知道如何做到这一点。我能找到的所有文档都是关于添加新命令的,但是我想要处理的命令已经存在 如何处理这些命令?我将您称为特例的特定注释选择和取消注释选择命令视为特例,因为我正在处理一个专门用于支持这两个操作的命令。该服务正在开发中,准备就绪后将通过NuGet发布
IClassiferProvider
来突出显示提供程序语法。我想添加其他功能,例如支持标准的Edit.CommentSelection
和Edit.FormatDocument
命令,但我不知道如何做到这一点。我能找到的所有文档都是关于添加新命令的,但是我想要处理的命令已经存在
如何处理这些命令?我将您称为特例的特定注释选择和取消注释选择命令视为特例,因为我正在处理一个专门用于支持这两个操作的命令。该服务正在开发中,准备就绪后将通过NuGet发布。我将首先介绍这项服务,然后介绍一些关于实现对特定命令的支持的一般信息,包括格式化文档命令 我想在本周发布这个库及其依赖项,但是程序集是一个组件的限制要求进行更多的测试,而不是在最初发布之前对库进行测试。幸运的是,这个特定程序集中只有两个接口,即名称空间 使用评论服务 资料来源: 此服务允许扩展开发人员轻松支持VisualStudio中新语言的注释和取消注释命令 提供标准的评论者 提供评论功能的最简单方法是使用
icomcenter
接口的标准Commenter
实现。以下步骤显示如何创建Commenter
的实例,并通过导出icominterprovider
的实例将其提供给Commenter服务
IComInterProvider
派生的新类。对于使用ContentTypeAttribute
的一个或多个特定内容类型,使用MEFExportAttribute
导出此类。示例中的注释器支持C++风格的行注释和块注释,用于SimpleC
内容类型
[Export(typeof(ICommenterProvider))]
[ContentType("SimpleC")]
public sealed class SimpleCCommenterProvider : ICommenterProvider
{
public ICommenter GetCommenter(ITextView textView)
{
// TODO: provide a commenter
throw new NotImplementedException();
}
}
private static readonly LineCommentFormat LineCommentFormat =
new LineCommentFormat("//");
private static readonly BlockCommentFormat BlockCommentFormat =
new BlockCommentFormat("/*", "*/");
Commenter
的实例,实现GetCommenter(ITextView)
方法。导入ITextendoHistoryRegistry
服务是为了Commenter
正确支持撤销和重做命令。以下代码是支持简单语言的注释和取消注释命令所需的icominterprovider
的完整实现
[Export(typeof(ICommenterProvider))]
[ContentType("SimpleC")]
public sealed class SimpleCCommenterProvider : ICommenterProvider
{
private static readonly LineCommentFormat LineCommentFormat =
new LineCommentFormat("//");
private static readonly BlockCommentFormat BlockCommentFormat =
new BlockCommentFormat("/*", "*/");
[Import]
private ITextUndoHistoryRegistry TextUndoHistoryRegistry
{
get;
set;
}
public ICommenter GetCommenter(ITextView textView)
{
Func<Commenter> factory =
() => new Commenter(textView, TextUndoHistoryRegistry, LineCommentFormat, BlockCommentFormat);
return textView.Properties.GetOrCreateSingletonProperty<Commenter>(factory);
}
}
[导出(类型化(IComInterProvider))]
[内容类型(“SimpleC”)]
公共密封类SimpleCommenterProvider:ICommenterProvider
{
专用静态只读LineCommentFormat LineCommentFormat=
新的LineCommentFormat(“/”);
私有静态只读BlockCommentFormat BlockCommentFormat=
新的BlockCommentFormat(“/*”,“*/”);
[进口]
私有ITextUndoHistoryRegistry文本UndoHistoryRegistry
{
得到;
设置
}
公共IComCenter GetCommenter(ITextView文本视图)
{
Func工厂=
()=>新的注释器(textView、TextUndoHistoryRegistry、LineCommentFormat、BlockCommentFormat);
返回textView.Properties.GetOrCreateSingletonProperty(工厂);
}
}
QueryStatus
方法应检查由命令目标处理的特定命令,并返回适当的状态标志。应实现执行命令的Exec
方法ITextView
的实例获取IVsTextView
的方法来获取IVsTextView
IOleCommandTarget
虽然这个答案并不能完全回答我的问题,但足以让我走上正轨。谢谢。@AntoineAubry现在是:)顺便说一句,祝贺你的回答和GitHub项目。VisualStudio的可扩展性非常困难,因为文档太少。我自己永远也不会明白这件事。