Browse Source
Implemented GetWordBeforeCaret(). git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@3901 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
20 changed files with 458 additions and 63 deletions
@ -0,0 +1,91 @@
@@ -0,0 +1,91 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <author name="Daniel Grunwald"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using ICSharpCode.AvalonEdit.Gui; |
||||
using System; |
||||
using ICSharpCode.AvalonEdit.Document; |
||||
using ICSharpCode.AvalonEdit.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace ICSharpCode.AvalonEdit.Tests.Utils |
||||
{ |
||||
[TestFixture] |
||||
public class CaretNavigationTests |
||||
{ |
||||
int GetNextCaretStop(string text, int offset, CaretPositioningMode mode) |
||||
{ |
||||
return TextUtilities.GetNextCaretPosition(new StringTextSource(text), offset, false, mode); |
||||
} |
||||
|
||||
int GetPrevCaretStop(string text, int offset, CaretPositioningMode mode) |
||||
{ |
||||
return TextUtilities.GetNextCaretPosition(new StringTextSource(text), offset, true, mode); |
||||
} |
||||
|
||||
[Test] |
||||
public void CaretStopInEmptyString() |
||||
{ |
||||
Assert.AreEqual(0, GetNextCaretStop("", -1, CaretPositioningMode.Normal)); |
||||
Assert.AreEqual(-1, GetNextCaretStop("", 0, CaretPositioningMode.Normal)); |
||||
Assert.AreEqual(-1, GetPrevCaretStop("", 0, CaretPositioningMode.Normal)); |
||||
Assert.AreEqual(0, GetPrevCaretStop("", 1, CaretPositioningMode.Normal)); |
||||
|
||||
Assert.AreEqual(-1, GetNextCaretStop("", -1, CaretPositioningMode.WordStart)); |
||||
Assert.AreEqual(-1, GetNextCaretStop("", -1, CaretPositioningMode.WordBorder)); |
||||
Assert.AreEqual(-1, GetPrevCaretStop("", 1, CaretPositioningMode.WordStart)); |
||||
Assert.AreEqual(-1, GetPrevCaretStop("", 1, CaretPositioningMode.WordBorder)); |
||||
} |
||||
|
||||
[Test] |
||||
public void StartOfDocumentWithWordStart() |
||||
{ |
||||
Assert.AreEqual(0, GetNextCaretStop("word", -1, CaretPositioningMode.Normal)); |
||||
Assert.AreEqual(0, GetNextCaretStop("word", -1, CaretPositioningMode.WordStart)); |
||||
Assert.AreEqual(0, GetNextCaretStop("word", -1, CaretPositioningMode.WordBorder)); |
||||
|
||||
Assert.AreEqual(0, GetPrevCaretStop("word", 1, CaretPositioningMode.Normal)); |
||||
Assert.AreEqual(0, GetPrevCaretStop("word", 1, CaretPositioningMode.WordStart)); |
||||
Assert.AreEqual(0, GetPrevCaretStop("word", 1, CaretPositioningMode.WordBorder)); |
||||
} |
||||
|
||||
[Test] |
||||
public void StartOfDocumentNoWordStart() |
||||
{ |
||||
Assert.AreEqual(0, GetNextCaretStop(" word", -1, CaretPositioningMode.Normal)); |
||||
Assert.AreEqual(1, GetNextCaretStop(" word", -1, CaretPositioningMode.WordStart)); |
||||
Assert.AreEqual(1, GetNextCaretStop(" word", -1, CaretPositioningMode.WordBorder)); |
||||
|
||||
Assert.AreEqual(0, GetPrevCaretStop(" word", 1, CaretPositioningMode.Normal)); |
||||
Assert.AreEqual(-1, GetPrevCaretStop(" word", 1, CaretPositioningMode.WordStart)); |
||||
Assert.AreEqual(-1, GetPrevCaretStop(" word", 1, CaretPositioningMode.WordBorder)); |
||||
} |
||||
|
||||
[Test] |
||||
public void EndOfDocumentWordBorder() |
||||
{ |
||||
Assert.AreEqual(4, GetNextCaretStop("word", 3, CaretPositioningMode.Normal)); |
||||
Assert.AreEqual(-1, GetNextCaretStop("word", 3, CaretPositioningMode.WordStart)); |
||||
Assert.AreEqual(4, GetNextCaretStop("word", 3, CaretPositioningMode.WordBorder)); |
||||
|
||||
Assert.AreEqual(4, GetPrevCaretStop("word", 5, CaretPositioningMode.Normal)); |
||||
Assert.AreEqual(0, GetPrevCaretStop("word", 5, CaretPositioningMode.WordStart)); |
||||
Assert.AreEqual(4, GetPrevCaretStop("word", 5, CaretPositioningMode.WordBorder)); |
||||
} |
||||
|
||||
[Test] |
||||
public void EndOfDocumentNoWordBorder() |
||||
{ |
||||
Assert.AreEqual(4, GetNextCaretStop("txt ", 3, CaretPositioningMode.Normal)); |
||||
Assert.AreEqual(-1, GetNextCaretStop("txt ", 3, CaretPositioningMode.WordStart)); |
||||
Assert.AreEqual(-1, GetNextCaretStop("txt ", 3, CaretPositioningMode.WordBorder)); |
||||
|
||||
Assert.AreEqual(4, GetPrevCaretStop("txt ", 5, CaretPositioningMode.Normal)); |
||||
Assert.AreEqual(0, GetPrevCaretStop("txt ", 5, CaretPositioningMode.WordStart)); |
||||
Assert.AreEqual(3, GetPrevCaretStop("txt ", 5, CaretPositioningMode.WordBorder)); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,86 @@
@@ -0,0 +1,86 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <author name="Daniel Grunwald"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using ICSharpCode.AvalonEdit.Gui; |
||||
using System; |
||||
using ICSharpCode.AvalonEdit.Utils; |
||||
using ICSharpCode.SharpDevelop.Dom.Refactoring; |
||||
|
||||
namespace ICSharpCode.SharpDevelop |
||||
{ |
||||
/// <summary>
|
||||
/// Extension methods for ITextEditor and IDocument.
|
||||
/// </summary>
|
||||
public static class DocumentUtilitites |
||||
{ |
||||
/// <summary>
|
||||
/// Gets the word in front of the caret.
|
||||
/// </summary>
|
||||
public static string GetWordBeforeCaret(this ITextEditor editor) |
||||
{ |
||||
if (editor == null) |
||||
throw new ArgumentNullException("editor"); |
||||
int endOffset = editor.Caret.Offset; |
||||
int startOffset = FindPrevWordStart(editor.Document, endOffset); |
||||
if (startOffset < 0) |
||||
return string.Empty; |
||||
else |
||||
return editor.Document.GetText(startOffset, endOffset - startOffset); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Finds the first word start in the document before offset.
|
||||
/// </summary>
|
||||
/// <returns>The offset of the word start, or -1 if there is no word start before the specified offset.</returns>
|
||||
public static int FindPrevWordStart(IDocument document, int offset) |
||||
{ |
||||
return TextUtilities.GetNextCaretPosition(GetTextSource(document), offset, true, CaretPositioningMode.WordStart); |
||||
} |
||||
|
||||
#region ITextSource implementation
|
||||
public static ICSharpCode.AvalonEdit.Document.ITextSource GetTextSource(IDocument document) |
||||
{ |
||||
if (document == null) |
||||
throw new ArgumentNullException("document"); |
||||
return new DocumentTextSource(document); |
||||
} |
||||
|
||||
sealed class DocumentTextSource : ICSharpCode.AvalonEdit.Document.ITextSource |
||||
{ |
||||
readonly IDocument document; |
||||
|
||||
public DocumentTextSource(IDocument document) |
||||
{ |
||||
this.document = document; |
||||
} |
||||
|
||||
public event EventHandler TextChanged { |
||||
add { document.TextChanged += value; } |
||||
remove { document.TextChanged -= value; } |
||||
} |
||||
|
||||
public string Text { |
||||
get { return document.Text; } |
||||
} |
||||
|
||||
public int TextLength { |
||||
get { return document.TextLength; } |
||||
} |
||||
|
||||
public char GetCharAt(int offset) |
||||
{ |
||||
return document.GetCharAt(offset); |
||||
} |
||||
|
||||
public string GetText(int offset, int length) |
||||
{ |
||||
return document.GetText(offset, length); |
||||
} |
||||
} |
||||
#endregion
|
||||
} |
||||
} |
Loading…
Reference in new issue