如何获得标准分类类型的IClassifier
How to get IClassifier for the standard classification type?
我有一个MS Visual Studio编辑器的扩展,它为C++添加了一些语法突出显示。
我想确保提供的 SnapshotSpan 具有标准的分类类型("注释")。有几种方法可以做到这一点:
1.我可以手动解析C++代码以查找注释范围。这是我想要使用的最后一个选项:)
2. 我可以使用黑客:
this.colorer = buffer.Properties.PropertyList // <-- buffer is a ITextBuffer
.Select(p => p.Value as IClassifier) // Get all classifiers someone put into the properies of the current text buffer
.Where(i => i != null)
.Where(i => i.GetType().FullName == "Microsoft.VisualC.CppColorer") // <-- Hack
.FirstOrDefault();
现在我可以通过以下方式使用此着色器(它是C++分类器的内部Microsoft实现):
this.colorer.GetClassificationSpans(span)
.Where(s => s.ClassificationType.Classification == FormatNames.Comment ||
s.ClassificationType.Classification == FormatNames.XmlDocComment)
哒哒!我有关于文本缓冲区中注释的信息。如您所知,这是一个黑客,我想避免这种:)
3.我可以尝试(以某种方式)获取标准分类类型的分类器(例如,用于"评论")。
所以我的问题是:这有可能通过分类类型名称获得 IClassifier 吗?
您可以导入IClassifierAggregatorService
:
[Import]
internal IClassifierAggregatorService classifierAggregatorService = null;
然后遍历ClassificationSpan
以检查每个分类范围是否属于 "comment"
类型:
IClassifier classifier = classifierAggregatorService.GetClassifier(textBuffer);
IList<ClassificationSpan> classificationSpanList = _classifier.GetClassificationSpans(span);
foreach (ClassificationSpan classificationSpan in classificationSpanList)
{
if (classificationSpan.ClassificationType.IsOfType("comment"))
{
// ...
}
}
作为获得IClassifierAggregatorService
的替代方法,您可以从IBufferTagAggregatorFactoryService
获得ITagAggregator<IClassificationTag>
。如果要根据以前的分类添加分类,则特别有用(请参阅此答案)。
看起来没有官方的方法可以做到这一点。所以我自己实现了代码注释的分类器。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- 如何获得标准分类类型的IClassifier