Ayende @ Rahien

Unnatural acts on source code

Smarter Syntax Highlighting

My previous approach isn't really suited to anything but the most trivial of tasks. What we can do, however, is to utilize existing components that already built this functionality. Let us say that we want to embed a Boo's code editor in our application. Here is what we need to do:

public class TextEditorForm : Form
{
    public TextEditorForm()
    {
        var editorControl = new TextEditorControl
        {
            Dock = DockStyle.Fill
        };
        Controls.Add(editorControl);
        editorControl.Document.FormattingStrategy = new BooFormattingStrategy();
        editorControl.SetHighlighting("Boo");
    }
}
public class BooFormattingStrategy : DefaultFormattingStrategy
{
    public override void IndentLines(TextArea textArea, int begin, int end)
    {
    }

    protected override int SmartIndentLine(TextArea area, int line)
    {
        IDocument document = area.Document;
        LineSegment lineSegment = document.GetLineSegment(line - 1);
        if (document.GetText(lineSegment).EndsWith(":"))
        {
            LineSegment segment = document.GetLineSegment(line);
            string str = base.GetIndentation(area, line - 1) + Tab.GetIndentationString(document);
            document.Replace(segment.Offset, segment.Length, str + document.GetText(segment));
            return str.Length;
        }
        return base.SmartIndentLine(area, line);
    }
}

And that gives us this:

image

Nice :-)

Comments

Ben Hall
08/19/2008 02:55 PM by
Ben Hall

Out of interest, what control is TextEditorControl ?

Does editorControl.SetHighlighting("Boo"); know what you be blue etc?

Ayende Rahien
08/19/2008 03:00 PM by
Ayende Rahien

Stupid of me, sorry.

That is ICSharpCode.TextEditor and it has native support for boo syntax highlighting

Ben Hall
08/19/2008 03:05 PM by
Ben Hall

Thanks,

Would be interesting to know how ICSharpCode's implementation differs from your previous post....

Ayende Rahien
08/19/2008 03:13 PM by
Ayende Rahien

It actually implements a tokenizer, does this properly, etc

Tobin Harris
08/19/2008 04:04 PM by
Tobin Harris

I used the ICSharpCode.TextEditor in SqlBuddy (many moons ago), and it was awesome IMHO!

Now I need a good JavaScript version for Squilbo...

Igal Tabachnik
08/19/2008 05:15 PM by
Igal Tabachnik

Also, this (awesome) component is LGPL licensed, so its assembly can be freely used in your apps.

Rob
08/19/2008 06:01 PM by
Rob

Cool. What I would really like to see, though, is custom intellisense over the dsl.

Kyle
08/20/2008 12:45 AM by
Kyle

I had to write something similar once, for the string-processing language that I believe I mentioned before. It was more to notify users of exactly where their bug was, but I tokenized and compiled code, and did generally the same thing as the above. Pretty neat stuff.

Paul Kohler
08/20/2008 03:22 AM by
Paul Kohler

I have used ICSharpCode.TextEditor in several in house apps/tools and also in "Mini SQL Query" and I also think it rocks! I would love to see an implementation of "intellisense" too!

Comments have been closed on this topic.