문제

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