How to check if a word at a position is in a Span[i] of the xshd-ruleset?
Than开发者_如何学编程ks!
There are two possible ways to get information from the highlighting engine:
The highlighting engine only stores the "span stack" at the start of each line. You can use the DocumentHighlighter.GetSpanStack
method to retrieve it.
var documentHighlighter = textEditor.TextArea.GetService(typeof(IHighlighter)) as DocumentHighlighter;
bool isInComment = documentHighlighter.GetSpanStack(1)
.Any(s => s.SpanColor != null && s.SpanColor.Name == "Comment");
This will return true if the end of line 1 (= start of line 2) is inside a multiline comment.
For more detailed results inside lines, you'll have to run the highlighter.
int off = document.GetOffset(7, 22);
HighlightedLine result = documentHighlighter.HighlightLine(7);
bool isInComment = result.Sections.Any(s => s.Offset <= off
&& s.Offset+s.Length >= off
&& s.Color.Name == "Comment");
Of course, identifying spans/sections by color only works reliably if those colors are named. Not all built-in highlightings have been updated to use named colors, so please check the .xshd files first.
精彩评论