Question

For AvalonEdit I have defined "Comments" in the xshd-File. Now, in my program, I would like to determine whether a given offset lies inside or outside of a comment.

I did find some code on the net, namely:
http://community.sharpdevelop.net/forums/t/12793.aspx

However, I do not know how to receive the necessary objects (like CurrentContext) from my AvalonEdit-Object.

I'm hoping someone has created such a function before. Can you please post some code or point me in the right direction? (documentation, etc)

Was it helpful?

Solution

I'm not sure what the "current context" is in that example, but it's only used to access the service container with the IHighlighter. You can get that directly from the TextEditor:

bool IsInComment(int line, int column)
{
    IHighlighter highlighter = textEditor.TextArea.GetService(typeof(IHighlighter)) as IHighlighter;
    if (highlighter == null)
        return false;
    int off = textEditor.Document.GetOffset(line, column);
    HighlightedLine result = highlighter.HighlightLine(document.GetLineByNumber(line));
    return result.Sections.Any(s => s.Offset <= off && s.Offset+s.Length >= off && s.Color.Name == "Comment");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top