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:
Nice :-)
Comments
Out of interest, what control is TextEditorControl ?
Does editorControl.SetHighlighting("Boo"); know what you be blue etc?
Stupid of me, sorry.
That is ICSharpCode.TextEditor and it has native support for boo syntax highlighting
Thanks,
Would be interesting to know how ICSharpCode's implementation differs from your previous post....
It actually implements a tokenizer, does this properly, etc
I used the ICSharpCode.TextEditor in SqlBuddy (many moons ago), and it was awesome IMHO!
Now I need a good JavaScript version for Squilbo...
Also, this (awesome) component is LGPL licensed, so its assembly can be freely used in your apps.
Cool. What I would really like to see, though, is custom intellisense over the dsl.
Wait for it...
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.
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!
Comment preview