How to get line number on IClassifier.GetClassificationSpans?

大兔子大兔子 提交于 2019-12-11 04:49:47

问题


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.

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.

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.

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?


回答1:


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

var lineNumber= span.Snapshot.GetLineNumberFromPosition(span.Start.Position) + 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).



来源:https://stackoverflow.com/questions/56651201/how-to-get-line-number-on-iclassifier-getclassificationspans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!