Creating your own Text Editor with AvalonEdit
- Posted: Mar 06, 2013 at 6:00 AM
- 10,338 Views
In my day to day secret identity (aka. my job) I've been recently using AvalonEdit to help me present text files to my users, mark them up and also allow them to interact with that data and thought that since I hadn't shared AvalonEdit with you before...
So there I was, having a bunch of plain text files, that I needed to quick display to my users, no access at the time to a commercial third party library suite and needing to, at runtime, find, search, select and highlight text bits. Living in Visual Studio as I do, I wanted something like it. Plus it would be awesome if it were open source and used in a real world project...
I've been personally following the SharpDevelop project for years as well as its spinoff projects AvalonDock and AvalonEdit and having been waiting for the right project in which I could use them and it looks like I found it. And while I can't share the details of that project with you, I can at least share one of its foundational pieces.
AvalonEdit is the name of the new WPF-based text editor in SharpDevelop 4.x "Mirador"
Source code: You can download AvalonEdit as part of SharpDevelop 4.x from our git repository or from the build server. These builds target .NET 4.0, but the AvalonEdit source code can can still be compiled for .NET 3.5 SP1 (change the target framework and remove the DOTNET4 conditional compilation symbol). You will need to run 'debugbuild.bat' for compiling SharpDevelop so that GlobalAssemblyInfo.cs gets generated.
The source code download of AvalonEdit contains sources for a help file (created with Sandcastle, if you don't like reading the XML sources). The compiled help file is available as download on the CodeProject article.
Blogs Posts / Articles on AvalonEdit
From the Team
- Using AvalonEdit - CodeProject article
- 'lost chapters' from above article: removed because the article got too long; not completely finished
- Forum post on retrieving syntax highlighting information
- Forum post on converting syntax-highlighted text to HTML
- Forum post on converting from the old .xshd format
- Forum post on tooltips
- WPF Text Rendering Performance
- Forum post on custom highlighting (ANSI escape sequences)
To get started, first I suggest the referenced CodeProject article;
ICSharpCode.AvalonEditis the WPF-based text editor that I've written for SharpDevelop 4.0. It is meant as a replacement for ICSharpCode.TextEditor, but should be:
- Easy to use
- Better at handling large files
Extensible means that I wanted SharpDevelop add-ins to be able to add features to the text editor. For example, an add-in should be able to allow inserting images into comments – this way, you could put stuff like class diagrams right into the source code!
With, Easy to use, I'm referring to the programming API. It should just work™. For example, this means if you change the document text, the editor should automatically redraw without having to call
Invalidate(). And, if you do something wrong, you should get a meaningful exception, not corrupted state and crash later at an unrelated location.
Better at handling large files means that the editor should be able to handle large files (e.g., the mscorlib XML documentation file, 7 MB, 74100 LOC), even when features like folding (code collapsing) are enabled.
Once you read through this article, the next step is to get the AvalonEdit sample from Nuget. This will let you play with it quickly and easily, by changing one line of code, your StartUp URI.
Once you install this sample (I suggest into a temp, Delete Me, Hello AvalonEdit World, kind of project), just tweak your app's StartUp URI (as noted there in the Nuget package description);
Then F5 and you've got yourself an IDE like text editor!
One which you can extend as need be, with your own formats, colorizers, etc. Heck, out of the box you get a number of formats already....
What's nice is that it's all wrapped up into one reference/assembly, which makes deployment easier.
Now due note that this is a LGPL 2.1 licensed project, so make sure that doesn't impact you.
One final touch is that when you install the sample, you also get some starting points for extending it for your own needs too;
If your looking around for a WPF Text Editor component, one that's used in tools like an IDE, one that you can extend and morph, one that's open source, then run, don't walk over to AvalonEdit and check it out!