Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AvalonEdit: Determine if you are in a Comment

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)

like image 547
J Fabian Meier Avatar asked Jan 18 '26 06:01

J Fabian Meier


1 Answers

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");
}
like image 149
Daniel Avatar answered Jan 20 '26 20:01

Daniel