Click here to Skip to main content
15,893,486 members
Articles / Desktop Programming / Windows Forms

Using ICSharpCode.TextEditor

Rate me:
Please Sign up or sign in to vote.
4.82/5 (60 votes)
12 Nov 2008MIT8 min read 392.7K   23.4K   194  
Use TextEditorControl to put a syntax-highlighting editor in your application.
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
//     <version>$Revision: 1471 $</version>
// </file>

using System;
using System.Collections.Generic;

namespace ICSharpCode.TextEditor.Document
{
	/// <summary>
	/// A stack of Span instances. Works like Stack&lt;Span&gt;, but can be cloned quickly
	/// because it is implemented as linked list.
	/// </summary>
	public sealed class SpanStack : ICloneable, IEnumerable<Span>
	{
		internal sealed class StackNode
		{
			public readonly StackNode Previous;
			public readonly Span Data;
			
			public StackNode(StackNode previous, Span data)
			{
				this.Previous = previous;
				this.Data = data;
			}
		}
		
		StackNode top = null;
		
		public Span Pop()
		{
			Span s = top.Data;
			top = top.Previous;
			return s;
		}
		
		public Span Peek()
		{
			return top.Data;
		}
		
		public void Push(Span s)
		{
			top = new StackNode(top, s);
		}
		
		public bool IsEmpty {
			get {
				return top == null;
			}
		}
		
		public SpanStack Clone()
		{
			SpanStack n = new SpanStack();
			n.top = this.top;
			return n;
		}
		object ICloneable.Clone()
		{
			return this.Clone();
		}
		
		public Enumerator GetEnumerator()
		{
			return new Enumerator(new StackNode(top, null));
		}
		IEnumerator<Span> IEnumerable<Span>.GetEnumerator()
		{
			return this.GetEnumerator();
		}
		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return this.GetEnumerator();
		}
		
		public struct Enumerator : IEnumerator<Span>
		{
			StackNode c;
			
			internal Enumerator(StackNode node)
			{
				c = node;
			}
			
			public Span Current {
				get {
					return c.Data;
				}
			}
			
			object System.Collections.IEnumerator.Current {
				get {
					return c.Data;
				}
			}
			
			public void Dispose()
			{
				c = null;
			}
			
			public bool MoveNext()
			{
				c = c.Previous;
				return c != null;
			}
			
			public void Reset()
			{
				throw new NotSupportedException();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer None
Canada Canada
Since I started programming when I was 11, I wrote the SNES emulator "SNEqr", the FastNav mapping component, the Enhanced C# programming language (in progress), the parser generator LLLPG, and LES, a syntax to help you start building programming languages, DSLs or build systems.

My overall focus is on the Language of your choice (Loyc) initiative, which is about investigating ways to improve interoperability between programming languages and putting more power in the hands of developers. I'm also seeking employment.

Comments and Discussions