Click here to Skip to main content
15,897,291 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 393.1K   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="Mike Krüger" email="mike@icsharpcode.net"/>
//     <version>$Revision: 2314 $</version>
// </file>

using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Actions;
using ICSharpCode.TextEditor.Document;
using NUnit.Framework;
using System;

namespace ICSharpCode.TextEditor.Tests
{
	[TestFixture]
	public class BlockCommentTests
	{
		IDocument document = null;
		string commentStart = "<!--";
		string commentEnd = "-->";
		
		[SetUp]
		public void Init()
		{
			document = new DocumentFactory().CreateDocument();
			document.HighlightingStrategy = HighlightingManager.Manager.FindHighlighter("XML");
		}
		
		[Test]
		public void NoTextSelected()
		{
			document.TextContent = String.Empty;
			int selectionStartOffset = 0;
			int selectionEndOffset = 0;
			
			BlockCommentRegion commentRegion = ToggleBlockComment.FindSelectedCommentRegion(document, commentStart, commentEnd, selectionStartOffset, selectionEndOffset);
			Assert.IsNull(commentRegion, "Should not be a comment region for an empty document");
		}
		
		[Test]
		public void EntireCommentSelected()
		{
			document.TextContent = "<!---->";
			int selectionStartOffset = 0;
			int selectionEndOffset = 7;
			BlockCommentRegion expectedCommentRegion = new BlockCommentRegion(commentStart, commentEnd, 0, 4);
			
			BlockCommentRegion commentRegion = ToggleBlockComment.FindSelectedCommentRegion(document, commentStart, commentEnd, selectionStartOffset, selectionEndOffset);
			Assert.AreEqual(expectedCommentRegion, commentRegion);
		}
		
		[Test]
		public void EntireCommentAndExtraTextSelected()
		{
			document.TextContent = "a<!-- -->";
			int selectionStartOffset = 0;
			int selectionEndOffset = 9;
			BlockCommentRegion expectedCommentRegion = new BlockCommentRegion(commentStart, commentEnd, 1, 6);
			
			BlockCommentRegion commentRegion = ToggleBlockComment.FindSelectedCommentRegion(document, commentStart, commentEnd, selectionStartOffset, selectionEndOffset);
			Assert.AreEqual(expectedCommentRegion, commentRegion);
		}
		
		[Test]
		public void OnlyCommentStartSelected()
		{
			document.TextContent = "<!-- -->";
			int selectionStartOffset = 0;
			int selectionEndOffset = 4;
			BlockCommentRegion expectedCommentRegion = new BlockCommentRegion(commentStart, commentEnd, 0, 5);
			
			BlockCommentRegion commentRegion = ToggleBlockComment.FindSelectedCommentRegion(document, commentStart, commentEnd, selectionStartOffset, selectionEndOffset);
			Assert.AreEqual(expectedCommentRegion, commentRegion);
		}
		
		[Test]
		public void OnlyCommentEndSelected()
		{
			document.TextContent = "<!-- -->";
			int selectionStartOffset = 5;
			int selectionEndOffset = 8;
			BlockCommentRegion expectedCommentRegion = new BlockCommentRegion(commentStart, commentEnd, 0, 5);
			
			BlockCommentRegion commentRegion = ToggleBlockComment.FindSelectedCommentRegion(document, commentStart, commentEnd, selectionStartOffset, selectionEndOffset);
			Assert.AreEqual(expectedCommentRegion, commentRegion);
		}
		
		[Test]
		public void LastCharacterOfCommentEndSelected()
		{
			document.TextContent = "<!-- -->";
			int selectionStartOffset = 7;
			int selectionEndOffset = 8;
			BlockCommentRegion expectedCommentRegion = new BlockCommentRegion(commentStart, commentEnd, 0, 5);
			
			BlockCommentRegion commentRegion = ToggleBlockComment.FindSelectedCommentRegion(document, commentStart, commentEnd, selectionStartOffset, selectionEndOffset);
			Assert.AreEqual(expectedCommentRegion, commentRegion);
		}		
		
		[Test]
		public void CaretInsideCommentButNoSelectedText()
		{
			document.TextContent = "<!---->";
			int selectionStartOffset = 4;
			int selectionEndOffset = 4;
			BlockCommentRegion expectedCommentRegion = new BlockCommentRegion(commentStart, commentEnd, 0, 4);
			
			BlockCommentRegion commentRegion = ToggleBlockComment.FindSelectedCommentRegion(document, commentStart, commentEnd, selectionStartOffset, selectionEndOffset);
			Assert.AreEqual(expectedCommentRegion, commentRegion);
		}
		
		[Test]
		public void FirstCharacterOfCommentStartSelected()
		{
			document.TextContent = "<!-- -->";
			int selectionStartOffset = 0;
			int selectionEndOffset = 1;
			BlockCommentRegion expectedCommentRegion = new BlockCommentRegion(commentStart, commentEnd, 0, 5);
			
			BlockCommentRegion commentRegion = ToggleBlockComment.FindSelectedCommentRegion(document, commentStart, commentEnd, selectionStartOffset, selectionEndOffset);
			Assert.AreEqual(expectedCommentRegion, commentRegion);
		}	
		
		[Test]
		public void CursorJustOutsideCommentStart()
		{
			document.TextContent = "<!-- -->";
			int selectionStartOffset = 0;
			int selectionEndOffset = 0;
			
			BlockCommentRegion commentRegion = ToggleBlockComment.FindSelectedCommentRegion(document, commentStart, commentEnd, selectionStartOffset, selectionEndOffset);
			Assert.IsNull(commentRegion);
		}
		
		[Test]
		public void CursorJustOutsideCommentEnd()
		{
			document.TextContent = "<!-- -->";
			int selectionStartOffset = 8;
			int selectionEndOffset = 8;
			
			BlockCommentRegion commentRegion = ToggleBlockComment.FindSelectedCommentRegion(document, commentStart, commentEnd, selectionStartOffset, selectionEndOffset);
			Assert.IsNull(commentRegion);
		}
		
		[Test]
		public void TwoExistingBlockComments()
		{
			document.TextContent = "<a>\r\n" +
									"<!--<b></b>-->\r\n" +
									"\t<c></c>\r\n" +
									"<!--<d></d>-->\r\n" +
									"</a>";
			
			string selectedText = "<c></c>";
			int selectionStartOffset = document.TextContent.IndexOf(selectedText);
			int selectionEndOffset = selectionStartOffset + selectedText.Length;
			
			BlockCommentRegion commentRegion = ToggleBlockComment.FindSelectedCommentRegion(document, commentStart, commentEnd, selectionStartOffset, selectionEndOffset);
			Assert.IsNull(commentRegion);
		}
	}
}

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