Skip to content

Commit

Permalink
Removed BufferUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpar committed Dec 29, 2009
1 parent bada8a0 commit 1e7f805
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 191 deletions.
2 changes: 1 addition & 1 deletion Todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ X - Move more operations to the interfaces in normal mode
X - Join needs to move into ICommonOperations
X - Move Shift into ICommonOperations
X - Implement IEditorOperationsTest
- Remove BufferUtil
X - Remove BufferUtil

Version 0.7
===
Expand Down
20 changes: 0 additions & 20 deletions VimCore/BufferUtil.fs

This file was deleted.

9 changes: 0 additions & 9 deletions VimCore/BufferUtil.fsi

This file was deleted.

24 changes: 20 additions & 4 deletions VimCore/Modes_Normal_DefaultOperations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,28 @@ type internal DefaultOperations
if moveCursor then
ViewUtil.MoveCaretToPoint _textView span.End |> ignore

/// Insert a line above the current cursor position
member x.InsertLineAbove ()=
member x.InsertLineBelow () =
let point = ViewUtil.GetCaretPoint _textView
let line = BufferUtil.AddLineAbove (point.GetContainingLine())
let line = point.GetContainingLine()
let buffer = line.Snapshot.TextBuffer
let tss = buffer.Replace(new Span(line.End.Position,0), System.Environment.NewLine)
let newLine = tss.GetLineFromLineNumber(line.LineNumber+1)

// Move the caret to the same indent position as the previous line
let indent = TssUtil.FindIndentPosition(line)
let point = new VirtualSnapshotPoint(newLine, indent)
ViewUtil.MoveCaretToVirtualPoint _textView point |> ignore
newLine

member x.InsertLineAbove () =
let point = ViewUtil.GetCaretPoint _textView
let line = point.GetContainingLine()
let buffer = line.Snapshot.TextBuffer
let tss = buffer.Replace(new Span(line.Start.Position,0), System.Environment.NewLine)
let line = tss.GetLineFromLineNumber(line.LineNumber)
ViewUtil.MoveCaretToPoint _textView line.Start |> ignore

line

/// Implement the r command in normal mode.
member x.ReplaceChar (ki:KeyInput) count =
let point = ViewUtil.GetCaretPoint _textView
Expand Down
8 changes: 7 additions & 1 deletion VimCore/Modes_Normal_IOperations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

namespace Vim.Modes.Normal
open Vim
open Microsoft.VisualStudio.Text
open Microsoft.VisualStudio.Text.Editor
open Microsoft.VisualStudio.Text.Operations

/// Normal mode operations
type IOperations =
abstract InsertLineAbove : unit -> unit
abstract ReplaceChar : KeyInput -> count:int -> bool
abstract YankLines : count:int -> Register -> unit
abstract DeleteCharacterAtCursor : count:int -> Register -> unit
abstract DeleteCharacterBeforeCursor : count:int -> Register -> unit
abstract PasteAfterCursor : text:string -> count:int -> opKind:OperationKind -> moveCursorToEnd: bool -> unit
abstract PasteBeforeCursor : text:string -> count:int -> moveCursorToEnd:bool -> unit

/// Adds an empty line to the buffer below the cursor and returns the resulting ITextSnapshotLine
abstract InsertLineBelow : unit -> ITextSnapshotLine

/// Insert a line above the current cursor position and returns the resulting ITextSnapshotLine
abstract InsertLineAbove : unit -> ITextSnapshotLine
interface Modes.ICommonOperations
23 changes: 5 additions & 18 deletions VimCore/Modes_Normal_NormalMode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -236,21 +236,6 @@ type internal NormalMode( _bufferData : IVimBufferData, _operations : IOperation
d.VimBufferData.BlockCaret.Hide()
NeedMore2(inner)


/// Add a line below the current cursor position and switch back to insert mode
member this.AddLineBelow (view:ITextView) =

// Insert the line
let point = ViewUtil.GetCaretPoint view
let line = point.GetContainingLine()
let newLine = BufferUtil.AddLineBelow line

// Move the caret to the same indent position as the previous line
let indent = TssUtil.FindIndentPosition(line)
let point = new VirtualSnapshotPoint(newLine, indent)
ViewUtil.MoveCaretToVirtualPoint view point |> ignore
NormalModeResult.SwitchMode (ModeKind.Insert)

/// Core method for scrolling the editor up or down
member this.ScrollCore dir count =
let lines = VimSettingsUtil.GetScrollLineCount this.Settings this.TextView
Expand Down Expand Up @@ -429,11 +414,13 @@ type internal NormalMode( _bufferData : IVimBufferData, _operations : IOperation
this.VimHost.Undo this.TextBuffer d.Count
NormalModeResult.Complete) };
{ KeyInput=InputUtil.CharToKeyInput('o');
RunFunc=(fun d -> this.AddLineBelow this.TextView) };
RunFunc=(fun d ->
_operations.InsertLineBelow() |> ignore
NormalModeResult.SwitchMode ModeKind.Insert); }
{ KeyInput=InputUtil.CharToKeyInput('O');
RunFunc=(fun d ->
_operations.InsertLineAbove()
NormalModeResult.SwitchMode ModeKind.Insert); };
_operations.InsertLineAbove() |> ignore
NormalModeResult.SwitchMode ModeKind.Insert); };
{ KeyInput=InputUtil.KeyToKeyInput(Key.Enter);
RunFunc=(fun d -> this.MoveForEnter this.TextView d.VimBufferData.VimHost) };
{ KeyInput=KeyInput('u', Key.U, ModifierKeys.Control);
Expand Down
2 changes: 0 additions & 2 deletions VimCore/VimCore.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@
<Compile Include="IncrementalSearch.fs" />
<Compile Include="Register.fs" />
<Compile Include="IRegisterMap.fs" />
<Compile Include="BufferUtil.fsi" />
<Compile Include="BufferUtil.fs" />
<Compile Include="VimSettings.fs" />
<Compile Include="ModeKind.fs" />
<Compile Include="IMode.fs" />
Expand Down
120 changes: 0 additions & 120 deletions VimCoreTest/BufferUtilTest.cs

This file was deleted.

22 changes: 7 additions & 15 deletions VimCoreTest/NormalModeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,11 @@ public void Edit_o_1()
{
CreateBuffer("how is", "foo");
_view.Caret.MoveTo(new SnapshotPoint(_view.TextSnapshot, 0));
_operations.Setup(x => x.InsertLineBelow()).Returns<ITextSnapshotLine>(null).Verifiable();
var res = _mode.Process('o');
Assert.IsTrue(res.IsSwitchMode);
Assert.AreEqual(ModeKind.Insert, res.AsSwitchMode().Item);
Assert.AreEqual(3, _view.TextSnapshot.Lines.Count());
_operations.Verify();
}

[Test, Description("Use o at end of buffer")]
Expand All @@ -536,25 +537,16 @@ public void Edit_o_2()
CreateBuffer("foo", "bar");
var line = _view.TextSnapshot.Lines.Last();
_view.Caret.MoveTo(line.Start);
_operations.Setup(x => x.InsertLineBelow()).Returns<ITextSnapshotLine>(null).Verifiable();
_mode.Process('o');
}

[Test, Description("Make sure o will indent if the previous line was indented")]
public void Edit_o_3()
{
CreateBuffer(" foo");
_view.Caret.MoveTo(new SnapshotPoint(_view.TextSnapshot, 0));
_mode.Process('o');
var point = _view.Caret.Position.VirtualBufferPosition;
Assert.IsTrue(point.IsInVirtualSpace);
Assert.AreEqual(2, point.VirtualSpaces);
_operations.Verify();
}

[Test]
public void Edit_O_1()
{
CreateBuffer("foo");
_operations.Setup(x => x.InsertLineAbove()).Verifiable();
_operations.Setup(x => x.InsertLineAbove()).Returns<ITextSnapshotLine>(null).Verifiable();
_mode.Process('O');
_operations.Verify();
}
Expand All @@ -563,7 +555,7 @@ public void Edit_O_1()
public void Edit_O_2()
{
CreateBuffer("foo", "bar");
_operations.Setup(x => x.InsertLineAbove()).Verifiable();
_operations.Setup(x => x.InsertLineAbove()).Returns<ITextSnapshotLine>(null).Verifiable();
_view.Caret.MoveTo(_view.TextSnapshot.GetLineFromLineNumber(1).Start);
_mode.Process("O");
_operations.Verify();
Expand All @@ -573,7 +565,7 @@ public void Edit_O_2()
public void Edit_O_3()
{
CreateBuffer("foo");
_operations.Setup(x => x.InsertLineAbove()).Verifiable();
_operations.Setup(x => x.InsertLineAbove()).Returns<ITextSnapshotLine>(null).Verifiable();
var res = _mode.Process('O');
Assert.IsTrue(res.IsSwitchMode);
Assert.AreEqual(ModeKind.Insert, res.AsSwitchMode().item);
Expand Down
66 changes: 66 additions & 0 deletions VimCoreTest/Normal_DefaultOperationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,71 @@ public void InsertLineAbove2()
Assert.AreEqual(1, point.GetContainingLine().LineNumber);
Assert.AreEqual(String.Empty, point.GetContainingLine().GetText());
}

[Test]
public void InsertLineBelow()
{
Create("foo", "bar", "baz");
var newLine = _operations.InsertLineBelow();
Assert.AreEqual(1, newLine.LineNumber);
Assert.AreEqual(String.Empty, newLine.GetText());

}

[Test, Description("New line at end of buffer")]
public void InsertLineBelow2()
{
Create("foo", "bar");
_view.Caret.MoveTo(_view.TextSnapshot.GetLineFromLineNumber(_view.TextSnapshot.LineCount - 1).Start);
var newLine = _operations.InsertLineBelow();
Assert.IsTrue(String.IsNullOrEmpty(newLine.GetText()));
}

[Test, Description("Make sure the new is actually a newline")]
public void InsertLineBelow3()
{
Create("foo");
var newLine = _operations.InsertLineBelow();
Assert.AreEqual(Environment.NewLine, _view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(0).GetLineBreakText());
Assert.AreEqual(String.Empty, _view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(1).GetLineBreakText());
}

[Test, Description("Make sure line inserted in the middle has correct text")]
public void InsertLineBelow4()
{
Create("foo", "bar");
_operations.InsertLineBelow();
var count = _view.TextSnapshot.LineCount;
foreach (var line in _view.TextSnapshot.Lines.Take(count - 1))
{
Assert.AreEqual(Environment.NewLine, line.GetLineBreakText());
}
}

[Test]
public void InsertLineBelow5()
{
Create("foo bar", "baz");
_operations.InsertLineBelow();
var buffer = _view.TextBuffer;
var line = buffer.CurrentSnapshot.GetLineFromLineNumber(0);
Assert.AreEqual(Environment.NewLine, line.GetLineBreakText());
Assert.AreEqual(2, line.LineBreakLength);
Assert.AreEqual("foo bar", line.GetText());
Assert.AreEqual("foo bar" + Environment.NewLine, line.GetTextIncludingLineBreak());

line = buffer.CurrentSnapshot.GetLineFromLineNumber(1);
Assert.AreEqual(Environment.NewLine, line.GetLineBreakText());
Assert.AreEqual(2, line.LineBreakLength);
Assert.AreEqual(String.Empty, line.GetText());
Assert.AreEqual(String.Empty + Environment.NewLine, line.GetTextIncludingLineBreak());

line = buffer.CurrentSnapshot.GetLineFromLineNumber(2);
Assert.AreEqual(String.Empty, line.GetLineBreakText());
Assert.AreEqual(0, line.LineBreakLength);
Assert.AreEqual("baz", line.GetText());
Assert.AreEqual("baz", line.GetTextIncludingLineBreak());
}

}
}
Loading

0 comments on commit 1e7f805

Please sign in to comment.