Introduction

ICSharpCode.AvalonEdit is 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:

Extensible

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.

Using the Code

The main class of the editor is ICSharpCode.AvalonEdit.TextEditor. You can use it just similar to a normal WPF TextBox:

ICSharpCode.AvalonEdit: TextEditor — the main control that brings it all together

Here is the visual tree of the TextEditor control:

It's important to understand that AvalonEdit is a composite control with three layers: TextEditor (main control), TextArea (editing), TextView (rendering). While the main control provides some convenience methods for common tasks, for most advanced features, you have to work directly with the inner controls. You can access them using textEditor.TextArea or textEditor.TextArea.TextView.

Document (The Text Model)

The main class of the model is ICSharpCode.AvalonEdit.Document.TextDocument. Basically, the document is a StringBuilder with events. However, the Document namespace also contains several features that are useful to applications working with the text editor.

In the text editor, all three controls (TextEditor, TextArea, TextView) have a Document property pointing to the TextDocument instance. You can change the Document property to bind the editor to another document. It is possible to bind two editor instances to the same document; you can use this feature to create a split view.

Offsets usually represent the position between two characters. The first offset at the start of the document is 0; the offset after the first char in the document is 1. The last valid offset is document.TextLength, representing the end of the document. This is exactly the same as the 'index' parameter used by methods in the .NET String or StringBuilder classes.

Offsets are easy to use, but sometimes you need Line / Column pairs instead. AvalonEdit defines a struct called TextLocation for those.

The document provides the methods GetLocation and GetOffset to convert between offsets and TextLocations. Those are convenience methods built on top of the DocumentLine class.

The TextDocument.Lines collection contains a DocumentLine instance for every line in the document. This collection is read-only to user code, and is automatically updated to reflect the current document content.

Rendering

In the whole 'Document' section, there was no mention of extensibility. The text rendering infrastructure now has to compensate for that by being completely extensible.

The ICSharpCode.AvalonEdit.Rendering.TextView class is the heart of AvalonEdit. It takes care of getting the document onto the screen.

To do this in an extensible way, the TextView uses its own kind of model: the VisualLine. Visual lines are created only for the visible part of the document.

The rendering process looks like this:

The last step in the pipeline is the conversion to one or more System.Windows.Media.TextFormatting.TextLine instances. WPF then takes care of the actual text rendering.

The "element generators", "line transformers", and "background renderers" are the extension points; it is possible to add custom implementations of them to the TextView to implement additional features in the editor.

Editing

The TextArea class handles user input and executes the appropriate actions. Both the caret and the selection are controlled by the TextArea.

You can customize the text area by modifying the TextArea.DefaultInputHandler by adding new or replacing existing WPF input bindings in it. You can also set TextArea.ActiveInputHandler to something different than the default, to switch the text area into another mode. You could use this to implement an "incremental search" feature, or even a VI emulator.

The text area has the LeftMargins property – use it to add controls to the left of the text view that look like they're inside the scroll viewer, but don't actually scroll. The AbstractMargin base class contains some useful code to detect when the margin is attached/detached from a text view; or when the active document changes. However, you're not forced to use it; any UIElement can be used as the margin.

Folding

Folding (code collapsing) is implemented as an extension to the editor. It could have been implemented in a separate assembly without having to modify the AvalonEdit code. A VisualLineElementGenerator takes care of the collapsed sections in the text document, and a custom margin draws the plus and minus buttons.

You could use the relevant classes separately; but, to make it a bit easier to use, the static FoldingManager.Install method will create and register the necessary parts automatically.

All that's left for you is to regularly call FoldingManager.UpdateFoldings with the list of foldings you want to provide. You could calculate that list yourself, or you could use a built-in folding strategy to do it for you.

If you want the folding markers to update when the text is changed, you have to repeat the foldingStrategy.UpdateFoldings call regularly.

Currently, only the XmlFoldingStrategy is built into AvalonEdit. The sample application to this article also contains the BraceFoldingStrategy that folds using { and }. However, it is a very simple implementation and does not handle { and } inside strings or comments correctly.

Syntax Highlighting

The highlighting engine in AvalonEdit is implemented in the class DocumentHighlighter. Highlighting is the process of taking a DocumentLine and constructing a HighlightedLine instance for it by assigning colors to different sections of the line.

The HighlightingColorizer class is the only link between highlighting and rendering. It uses a DocumentHighlighter to implement a line transformer that applies the highlighting to the visual lines in the rendering process.

Except for this single call, syntax highlighting is independent from the rendering namespace. To help with other potential uses of the highlighting engine, the HighlightedLine class has the method ToHtml to produce syntax highlighted HTML source code.

The rules for highlighting are defined using an "extensible syntax highlighting definition" (.xshd) file. Here is a complete highlighting definition for a sub-set of C#:

The highlighting engine works with "spans" and "rules" that each have a color assigned to them. In the XSHD format, colors can be both referenced (color="Comment") or directly specified (fontWeight="bold" foreground="Blue").

Spans consist of two Regular Expressions (begin+end); while rules are simply a single regex with a color. The <Keywords> element is just a nice syntax to define a highlighting rule that matches a set of words; internally, a single regex will be used for the whole keyword list.

The highlighting engine works by first analyzing the spans: whenever a begin regex matches some text, that span is pushed onto a stack. Whenever the end regex of the current span matches some text, the span is popped from the stack.

Each span has a nested rule set associated with it, which is empty by default. This is why keywords won't be highlighted inside comments: the span's empty ruleset is active there, so the keyword rule is not applied.

This feature is also used in the string span: the nested span will match when a backslash is encountered, and the character following the backslash will be consumed by the end regex of the nested span (. matches any character). This ensures that \" does not denote the end of the string span; but \\" still does.

What's great about the highlighting engine is that it highlights only on-demand, works incrementally, and yet usually requires only a few KB of memory even for large code files.

On-demand means that when a document is opened, only the lines initially visible will be highlighted. When the user scrolls down, highlighting will continue from the point where it stopped the last time. If the user scrolls quickly, so that the first visible line is far below the last highlighted line, then the highlighting engine still has to process all the lines in between – there might be comment starts in them. However, it will only scan that region for changes in the span stack; highlighting rules will not be tested.

The stack of active spans is stored at the beginning of every line. If the user scrolls back up, the lines getting into view can be highlighted immediately because the necessary context (the span stack) is still available.

Incrementally means that even if the document is changed, the stored span stacks will be reused as far as possible. If the user types /*, that would theoretically cause the whole remainder of the file to become highlighted in the comment color. However, because the engine works on-demand, it will only update the span stacks within the currently visible region and keep a notice 'the highlighting state is not consistent between line X and line X+1', where X is the last line in the visible region. Now, if the user would scroll down, the highlighting state would be updated and the 'not consistent' notice would be moved down. But usually, the user will continue typing and type */ only a few lines later. Now, the highlighting state in the visible region will revert to the normal 'only the main ruleset is on the stack of active spans'. When the user now scrolls down below the line with the 'not consistent' marker, the engine will notice that the old stack and the new stack are identical, and will remove the 'not consistent' marker. This allows reusing the stored span stacks cached from before the user typed /*.

While the stack of active spans might change frequently inside the lines, it rarely changes from the beginning of one line to the beginning of the next line. With most languages, such changes happen only at the start and end of multiline comments. The highlighting engine exploits this property by storing the list of span stacks in a special data structure (ICSharpCode.AvalonEdit.Utils.CompressingTreeList). The memory usage of the highlighting engine is linear to the number of span stack changes; not to the total number of lines. This allows the highlighting engine to store the span stacks for big code files using only a tiny amount of memory, especially in languages like C# where sequences of // or /// are more popular than /* */ comments.

Code Completion

AvalonEdit comes with a code completion drop down window. You only have to handle the text entering events to determine when you want to show the window; all the UI is already done for you.

This code will open the code completion window whenever '.' is pressed. By default, the CompletionWindow only handles key presses like Tab and Enter to insert the currently selected item. To also make it complete when keys like '.' or ';' are pressed, we attach another handler to the TextEntering event and tell the completion window to insert the selected item.

The CompletionWindow will actually never have focus - instead, it hijacks the WPF keyboard input events on the text area and passes them through its ListBox. This allows selecting entries in the completion list using the keyboard and normal typing in the editor at the same time.

For the sake of completeness, here is the implementation of the MyCompletionData class used in the code above:

Both the content and the description shown may be any content acceptable in WPF, including custom UIElements. You may also implement custom logic in the Complete method if you want to do more than simply insert text. The insertionRequestEventArgs can help decide which kind of insertion the user wants - depending on how the insertion was triggered, it is an instance of TextCompositionEventArgs, KeyEventArgs, or MouseEventArgs.

History

August 13, 2008: Work on AvalonEdit started

November 7, 2008: First version of AvalonEdit added to the SharpDevelop 4.0 trunk

June 14, 2009: The SharpDevelop team switches to SharpDevelop 4 as their IDE for working on SharpDevelop; AvalonEdit starts to get used for real work

Comments and Discussions

Hi .But i'he faced with some problems.I couldnt solve them by the expressions wich your put in the sait codeproject.com.I want to change the syntaxt highlighting of the passage.for example i'd like the colors change into red in php of the string.
In the .xshd file :
&lt;Color name="String" foreground="Blue" /&gt;
changed to :
&lt;Color name="String" foreground="Red" /&gt;
But dont changed string color to red.
please help me.
thank you.

If you use InsightWindow, just set its Content property to whatever WPF content you want to display.

If you want to allow the user to choose between overloads by pressing up/down, use the OverloadInsightWindow. There you have to set the Provider property to your own implementation of IOverloadProvider.

First of all, good job! I'm really impressed by the quality of the code and architecture
My question is: Since it's licenced under de GNU LPGL, does that mean it is possible for me to link the library to my software (which is not free) in order to use the TextEditor?

I'm not sure I clearly understands that part of the license:

"Program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License."

Since it's licenced under de GNU LPGL, does that mean it is possible for me to link the library to my software (which is not free) in order to use the TextEditor?

Yes.

The LGPL must be read with the classic C linking model in mind.
In my understanding, you must publish the source code (using the LGPL license) of anything that ends up in the same binary as our LGPL source code.
Translated to .NET assemblies, that means you have to publish the source code for modifications to AvalonEdit, but you can use AvalonEdit (both with and without modifications) in closed-source applications.

However, statically linking AvalonEdit with other closed-source assemblies (e.g. using ILMerge) is not allowed. Basically the idea seems to be that users of your application should be able to use their own modified versions of AvalonEdit with your application.

AvalonEdit doesn't have this build in, but you can take a look at the SharpDevelop source code to see how it's implemented on top of AvalonEdit.

Basically you'll have to add a custom margin to textEditor.TextArea.LeftMargins and paint the icons yourself. Take a look at SharpDevelop\src\AddIns\DisplayBindings\AvalonEdit.AddIn\Src\IconBarMargin.cs.

First I want to say I'm really impressed by the application, the sample just run, I pasted 300kb of text and it pasted it instantaneously! Outstanding!

Now, from just a quick browse at the sample and API I wonder... can AvalonEdit handle rich text? i.e. text with multiple font / font size?

A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.

But AvalonEdit is meant as a code editor, not as rich text editor. Its "document model" is very simple: the document is a string without any formatting information. Anything else is "syntax highlighting". Normally syntax highlighting is calculated from the text (+highlighting definition), but you could also write a custom kind of syntax highlighter that uses your own data structure to store the document's formatting.

If you want to try such a thing, I suggest you take a look at the TextSegmentCollection class as a basis for that data structure. The TextSegmentCollection automatically updates the offsets of the segments stored in it if text is inserted or removed in the document. It also supports efficient querying of the segments in an interval (the line you're applying the highlighting to).

A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.

Microsoft created the ElementHost container in order to use WPF controls inside Windows Forms applications, but unfortunately the DLL file for this control doesn't have a valid control that can be placed in the toolbox, basically making it impossible to place inside the ElementHost. Any ideas?

Yes, I've seen this link. The problem is I can't get AvalonEdit inside of the ElementHost because I can't add it as a control to the toolbox. Visual Studio doesn't recognize any user control inside of the AvalonEdit dll. Any ideas?

However, my rendering code assumes that VisualLineElements are rendered left-to-right, so if an element gets split (part of it is selected/highlighted) the results are incorrect or (as I've just discovered) AvalonEdit might even crash.

I'll try to fix the crashes but I won't implement RTL support. Patches are welcome, though.

Actually there were less problems than expected, only the BackgroundGeometryBuilder had those assumptions coded in.

I've changed BackgroundGeometryBuilder to use WPF TextLine.GetTextBounds in revision 5421. This fixes the crash and makes the selection look correct when selecting bidirectional text.

However, top-level RTL text (not embedded in LTR paragraphs) is not supported yet. It should be pretty easy to fix though, you just need to look for the places where FlowDirection.LeftToRight is hardcoded.