如何在 IClassifier.GetClassificationSpans 上获取行号? [英] How to get line number on IClassifier.GetClassificationSpans?

查看:23
本文介绍了如何在 IClassifier.GetClassificationSpans 上获取行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 开发 Visual Studio 扩展,我想根据对已提供的文件的分析在文件上添加分类.我得到了每个缺陷的一组位置(文件、行、列)的分析结果.

I am developing a Visual Studio extension in C# and I want to add classifications on a file based on an analysis of this file that is already provided. I have got analysis results with a set of locations (file, line, column) for each defect.

基于 MS 文档,我已经看到我们应该实现 IClassifier.GetClassificationSpans 方法.我可以看到我们得到了一组 SnapshotSpan.看起来这些跨度大部分是来自打开文件的完整行:当前显示的可见行和当前正在编辑的行.

Based on MS doc, I have seen that we should implement the IClassifier.GetClassificationSpans method. I can see that we are given a set of SnapshotSpan. It looks like these spans are mostly complete lines from the open file: the visible lines currently shown and the line currently being edited.

但是,正如前面所说,我已经有了它们位置的一组缺陷.我想获得当前的跨度行号,以便我可以检查我是否在该行上注册了缺陷.我用调试器浏览了整个 SnapshotSpan 结构,但找不到任何行号.

However, as said previouly, I already have a set of defects with their location. I would like to get the current span line number so I can check I have got a defect registered on that line. I have browsed the whole SnapshotSpan structure with the debugger and I couldn't find anything looking the line number.

如何获取当前的 SnapshotSpan 行号?

How to get current SnapshotSpan line number?

当我已经给出了带有位置的结果并且我想根据这些位置在编辑器中放置字形/分类/工具提示/大纲区域/等时,框架的逻辑是什么?

What is the logic of the framework when I already have results given with their location and I want to place glyphs/classifications/tooltips/outlining regions/etc in the editor based on these locations?

推荐答案

我终于发现可以通过以下方式获取行号:

I finally found out that I can get the line number the following way:

var lineNumber= span.Snapshot.GetLineNumberFromPosition(span.Start.Position) + 1;

+1 因为内部值从 0 开始,而编辑器中的可见行从 1 开始(或者因为我的文件分析给了我从 1 开始的行).

+1 because internal values start at 0 while visible lines in the editor start at 1 (or because my file analysis gives me lines starting at 1).

这篇关于如何在 IClassifier.GetClassificationSpans 上获取行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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