SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

AvalonEdit check if an offset is inside a comment or string?

Last post 02-18-2011 1:31 PM by Cookies.net. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 02-16-2011 4:34 PM

    AvalonEdit check if an offset is inside a comment or string?

    I'm trying to check if an offset is inside an offset and found some code for it on these forums, however it always returned false for it being highlighted so i decided to check what was wrong. The following code shows a messagebox with "0" printed in it

    TextEditor te = ((TextEditors.SelectedItem as TabItem).Content as TextEditor);
    HighlightedLine hline = new HighlightedLine(te.Document, te.Document.GetLineByOffset(te.SelectionStart));
    MessageBox.Show(hline.Sections.Count.ToString());

    even though im checking from a selection which is inside a multiline comment.

    Here's what im doing inside my highlighting class

    private bool IsInComment(int offset, DocumentLine line)
    {
    HighlightedLine hline = new HighlightedLine(CurrentContext.Document, line);
    return (hline.Sections.Any(s => s.Offset <= offset && s.Offset + s.Length >= offset && s.Color.Name == "Comment"));
    }

    and it always return false, not supported anymore?

  • 02-16-2011 5:12 PM In reply to

    Re: AvalonEdit check if an offset is inside a comment or string?

    From the old post, here's the code again:

    int off = document.GetOffset(722);
    HighlightedLine result = documentHighlighter.HighlightLine(document.GetLineByNumber(7));
    bool isInComment = result.Sections.Any(s => s.Offset <= off && s.Offset+s.Length >= off && s.Color.Name == "Comment");

    Note that it is not creating a new (empty) HighlightedLine, but asks the highlighting engine to create one. You can get a reference to the existing highlighting engine like this:

    IHighlighter documentHighlighter = textEditor.TextArea.GetService(typeof(IHighlighter)) as IHighlighter;

  • 02-16-2011 5:39 PM In reply to

    Re: AvalonEdit check if an offset is inside a comment or string?

    Ohh, thats what i did wrong, but i just tried it and the example is not 100% correct, it should be

    int off = document.GetOffset(7, 22);

    IHighlighter documentHighlighter = textEditor.TextArea.GetService(typeof(IHighlighter)) as IHighlighter;
    HighlightedLine result = documentHighlighter.HighlightLine(document.GetLineByNumber(7).LineNumber);
    bool IsInComment = result.Sections.Any(s => s.Offset <= off && s.Offset + s.Length >= off && s.Color.Name == "Comment");

    And thanks for your answer once again

     

    edit: but it doesnt seem like i pass the IHighlighter on to my highlighter class, it only returns false there

  • 02-18-2011 1:31 PM In reply to

    Re: AvalonEdit check if an offset is inside a comment or string?

    Found the way to get it from the TextView so now i can access it from my DocumentColorizingTransformer

    public bool IsInStringComment(int offset)
    {
    IHighlighter documentHighlighter = CurrentContext.TextView.Services.GetService(typeof(IHighlighter)) as IHighlighter;
    HighlightedLine result = documentHighlighter.HighlightLine(CurrentContext.Document.GetLineByOffset(offset).LineNumber);
    return result.Sections.Any(s => s.Offset <= offset && s.Offset + s.Length >= offset && (s.Color.Name == "Comment" || s.Color.Name == "String" || s.Color.Name == "Char"));
    }

     

Page 1 of 1 (4 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.