SharpDevelop Community

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

AvalonEdit: TextPosition style information

Last post 12-28-2010 6:19 PM by Daniela da Cruz. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 12-04-2009 11:32 AM

    • s0upyd
    • Not Ranked
    • Joined on 12-04-2009
    • Posts 7

    AvalonEdit: TextPosition style information


    This may be the wrong place to post and ask this (sorry), but is there a way to determine the class of a TextPosition?

    By this, I mean a way of determine if line 7, column 22 is quoted or a comment?

    The reason I ask is I'm trying to do a RegEx that finds some given tokens, but I want to ultimately exclude those that fall within quotes or comments.

    I appreciate any help anyone can give.



  • 12-04-2009 1:49 PM In reply to

    Re: AvalonEdit: TextPosition style information

    There are two possible ways to get information from the highlighting engine:

    The highlighting engine only stores the "span stack" at the start of each line. You can use the DocumentHighlighter.GetSpanStack method to retrieve it.

    DocumentHighlighter documentHighlighter = textEditor.TextArea.GetService(typeof(IHighlighter)) as DocumentHighlighter;
     isInComment = documentHighlighter.GetSpanStack(1).Any(s => s.SpanColor != null && s.SpanColor.Name == "Comment");

    This will return true if the end of line 1 (= start of line 2) is inside a multiline comment.

    For more detailed results inside lines, you'll have to run the highlighter.

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

    Of course, identifying spans/sections by color only works reliably if those colors are named. Currently only CSharp-Mode.xshd uses named colors for "Comment", "String" and "Char".

  • 12-04-2009 5:09 PM In reply to

    • s0upyd
    • Not Ranked
    • Joined on 12-04-2009
    • Posts 7

    Re: AvalonEdit: TextPosition style information

     Hi Daniel,

    Thanks for your detailed response. This looks a promsing beginning that I can play with.

    I am using AvalonEdit for a SQL utility I'm working on, so have written my own XML definition for the highlighting and have named the classes, so hopefully your suggestions should ring true for me.

    An additional question: I have my own HighlightingColorizer hooked into the TextArea.TextView.LineTransformers, but it seems to run *before* the engine highlighting. Therefore, I style something that needs to be done on the fly, but then if the engine matches something in there (like a keyword), my styling is stamped upon.

    I'm assuming I'm missing something in my colorizer, such as "this text has been dealt with, skip"?

    Cheers again,


  • 12-05-2009 5:12 PM In reply to

    Re: AvalonEdit: TextPosition style information

    Just make sure you add your own transformer behind the one added by the highlighting engine - transformers always overwrite the styling, so your custom line transformer must be called after the builtin highlighting engine.

  • 12-06-2009 5:57 PM In reply to

    • s0upyd
    • Not Ranked
    • Joined on 12-04-2009
    • Posts 7

    Re: AvalonEdit: TextPosition style information

     Thanks for the info. What you say makes complete sense. However, a silly question coming up; how do I add my transformer after the built-in one?

    I am doing


    I thought this would add it at the end of the queue, so I also tried ...Insert(0, _myColorizingTransformer), but this resulted in the same; the built-in engine stamped on my coloring.

    Sorry for the question, but a shove in the right direction would be appreciated.


  • 12-06-2009 6:05 PM In reply to

    Re: AvalonEdit: TextPosition style information

    When you set TextEditor.SyntaxHighlighting, the editor inserts its transformer at the beginning:

                    colorizer = new HighlightingColorizer(textView, newValue.MainRuleSet);
                    textView.LineTransformers.Insert(0, colorizer);

    So I don't know how it could overwrite the changes in your line transformer.

    If I take AvalonEdit.Sample (from SharpDevelop/samples), add the "ColorizeAvalonEdit" transformer (you'll have to add the ColorizeAvalonEdit.cs to the project and add "textEditor.TextArea.TextView.LineTransformers.Add(new ColorizeAvalonEdit());" to the end of the Window1 constructor), and then change it to highlight a word that conflicts with a keyword, the new transformer takes precedence over the built-in highlighting.

  • 12-28-2010 6:19 PM In reply to

    Re: AvalonEdit: TextPosition style information


    I woul like to select a set of lines (possibly disjoint) and change their background color. Is that possible?

    In fact, I tried to create an additional syntax highlithing definition but didn't worked.

    <Color name="Slicing" background="Red"/>

    <Span color="Slicing" begin="/\[" end="\]/"></Span>

    The background didn't worked out. Only the foreground.

    Can you give me an idea why?

    The perfect solution for me it would be to be able to select a Line and change its background color, without the need of create a new syntax rule.





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