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)

有帮助吗?

解决方案

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");
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top