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 :-)

Print | posted on Tuesday, August 19, 2008 4:55 PM

Feedback


Gravatar

# re: Smarter Syntax Highlighting 8/19/2008 5:55 PM Ben Hall

Out of interest, what control is TextEditorControl ?

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


Gravatar

# re: Smarter Syntax Highlighting 8/19/2008 6:00 PM Ayende Rahien

Stupid of me, sorry.
That is ICSharpCode.TextEditor and it has native support for boo syntax highlighting


Gravatar

# re: Smarter Syntax Highlighting 8/19/2008 6:05 PM Ben Hall

Thanks,

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


Gravatar

# re: Smarter Syntax Highlighting 8/19/2008 6:13 PM Ayende Rahien

It actually implements a tokenizer, does this properly, etc


Gravatar

# re: Smarter Syntax Highlighting 8/19/2008 7:04 PM 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...


Gravatar

# re: Smarter Syntax Highlighting 8/19/2008 8:15 PM Igal Tabachnik

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


Gravatar

# re: Smarter Syntax Highlighting 8/19/2008 9:01 PM Rob

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


Gravatar

# re: Smarter Syntax Highlighting 8/19/2008 9:23 PM Ayende Rahien

Wait for it...


Gravatar

# re: Smarter Syntax Highlighting 8/20/2008 3:45 AM 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.


Gravatar

# re: Smarter Syntax Highlighting 8/20/2008 6:22 AM 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.