Skip to content

Commit

Permalink
Fix visual line put when yanking at EOF. Fixes #83
Browse files Browse the repository at this point in the history
  • Loading branch information
nosami committed Jun 11, 2017
1 parent 3737704 commit a00cc00
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
24 changes: 13 additions & 11 deletions XSVim.Tests/VisualTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,59 @@ open XSVim

[<TestFixture>]
module ``Visual tests`` =
let getClipboard() = Vim.registers.[EmptyRegister].content

[<Test>]
let ``Visual to end of line``() =
let _, state = test "abc$ def\nghi" "v$y"
Vim.registers.[EmptyRegister] |> should equal "c def"
getClipboard() |> should equal "c def"

[<Test>]
let ``Visual to end of word``() =
let _, state = test "ab$c def\nghi" "vey"
Vim.registers.[EmptyRegister] |> should equal "bc"
getClipboard() |> should equal "bc"

[<Test>]
let ``Visual to d``() =
let _, state = test "ab$cdef" "vtdy"
Vim.registers.[EmptyRegister] |> should equal "bc"
getClipboard() |> should equal "bc"

[<Test>]
let ``Visual to d inclusive``() =
let _, state = test "ab$cdef" "vfdy"
Vim.registers.[EmptyRegister] |> should equal "bcd"
getClipboard() |> should equal "bcd"

[<Test>]
let ``Visual supports multipler``() =
let _, state = test "a$bcdef" "3vy"
Vim.registers.[EmptyRegister] |> should equal "abc"
getClipboard() |> should equal "abc"

[<Test>]
let ``Visual line``() =
let _, state = test "aaa\nbb$b\nddd" "Vy"
Vim.registers.[EmptyRegister] |> should equal "bbb\n"
getClipboard() |> should equal "bbb\n"

[<Test>]
let ``Visual to end of document``() =
let _, state = test "abc\nde$f\nghi" "vGy"
Vim.registers.[EmptyRegister] |> should equal "ef\ng"
getClipboard() |> should equal "ef\ng"

[<Test>]
let ``Visual to start of document``() =
let _, state = test "abc\nde$f\nghi" "vggy"
Vim.registers.[EmptyRegister] |> should equal "abc\nde"
getClipboard() |> should equal "abc\nde"

[<Test>]
let ``Visual line to end of document``() =
let _, state = test "abc\nde$f\nghi" "VGy"
Vim.registers.[EmptyRegister] |> should equal "def\nghi"
getClipboard() |> should equal "def\nghi"

[<Test>]
let ``Visual line to start of document``() =
let _, state = test "abc\nde$f\nghi" "Vggy"
Vim.registers.[EmptyRegister] |> should equal "abc\ndef\n"
getClipboard() |> should equal "abc\ndef\n"

[<Test>]
let ``Visual line supports multipler``() =
let _, state = test "abc\nde$f\nghi" "2Vy"
Vim.registers.[EmptyRegister] |> should equal "def\nghi"
getClipboard() |> should equal "def\nghi"
10 changes: 6 additions & 4 deletions XSVim.Tests/YankAndPut.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

open XSVim
open NUnit.Framework
open XSVim

[<TestFixture>]
module ``Yank and put tests`` =
Expand All @@ -13,7 +12,7 @@ module ``Yank and put tests`` =
[<Test>]
let ``Yanking line supports multiplier``() =
let _, state = test "a$bc\ndef\nghi" "2yy"
Vim.registers.[EmptyRegister] |> should equal "abc\ndef\n"
Vim.registers.[EmptyRegister].content |> should equal "abc\ndef\n"

[<Test>]
let ``Yanking doesn't move caret when there is no selection'``() =
Expand All @@ -38,11 +37,14 @@ module ``Yank and put tests`` =
[<Test>]
let ``Can yank into a named register``() =
let _, state = test "ab$cd ef" "\"dyl"
System.Console.WriteLine(Vim.registers.Keys.Count)
Vim.registers.[Register 'd'] |> should equal "b"
Vim.registers.[Register 'd'].content |> should equal "b"

[<Test>]
[<Ignore>]
//Todo: fix this and remove ignore attribute.
let ``yw at the end of a line consumes entire line``()=
assertText "a$bc" "ywp" "aabc$bc"

[<Test>]
let ``Visual line selection should work at EOF``() =
assertText "a$bc" "Vyp" "abc\na$bc"
2 changes: 1 addition & 1 deletion XSVim/Properties/AddinInfo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open MonoDevelop
[<assembly:Addin (
"XSVim",
Namespace = "XSVim",
Version = "0.28.1"
Version = "0.28.2"
)>]

[<assembly:AddinName ("Vim")>]
Expand Down
5 changes: 5 additions & 0 deletions XSVim/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ type MarkLocation = {
fileName: string
}

type Selection = {
linewise : bool
content: string
}

type Register =
|Register of Char
|EmptyRegister
Expand Down
36 changes: 27 additions & 9 deletions XSVim/XSVim.fs
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ module VimHelpers =
| _ -> editor.CaretOffset, editor.CaretOffset

module Vim =
let registers = new Dictionary<Register, string>()
registers.[EmptyRegister] <- ""
let registers = new Dictionary<Register, XSVim.Selection>()
registers.[EmptyRegister] <- { linewise=false; content="" }

let markDict = System.Collections.Generic.Dictionary<string,MarkLocation>()
let defaultState = { keys=[]; mode=NormalMode; visualStartOffset=0; findCharCommand=None; lastAction=[]; desiredColumn=None; undoGroup=None; statusMessage=None; }
Expand Down Expand Up @@ -402,12 +402,29 @@ module Vim =
| { commandType=Move; textObject=Down } -> Some MoveUpOrDown
| _ -> None

let (|LineWise|_|) = function
| { textObject=WholeLine }
| { textObject=WholeLineIncludingDelimiter }
| { textObject=LastLine } -> Some LineWise
| _ -> None

let getCommand repeat commandType textObject =
{ repeat=repeat; commandType=commandType; textObject=textObject }

let runOnce = getCommand (Some 1)
let typeChar c = runOnce (InsertChar c) Nothing

let getSelectedText vimState (editor: TextEditor) command =
let linewise =
match vimState.mode with
| VisualLineMode -> true
| _ ->
match command with
| LineWise -> true
| _ -> false

{ linewise=linewise; content=editor.SelectedText }

let runCommand vimState editor command =
let delete state start finish =
let finish =
Expand All @@ -421,7 +438,7 @@ module Vim =

if command.textObject <> SelectedText then
setSelection state editor command start finish
registers.[EmptyRegister] <- editor.SelectedText
registers.[EmptyRegister] <- getSelectedText state editor command
EditActions.ClipboardCut editor
state

Expand Down Expand Up @@ -579,7 +596,7 @@ module Vim =
| _ -> finish
if command.textObject <> SelectedText then
setSelection vimState editor command start finish
registers.[register] <- editor.SelectedText
registers.[register] <- getSelectedText vimState editor command
if register = EmptyRegister then
EditActions.ClipboardCopy editor
editor.ClearSelection()
Expand All @@ -588,22 +605,23 @@ module Vim =
| _ -> ()
processCommands 1 vimState (runOnce (SwitchMode NormalMode) Nothing) false
| Put Before ->
if registers.[EmptyRegister].EndsWith "\n" then
if registers.[EmptyRegister].linewise then
editor.CaretOffset <- editor.GetLine(editor.CaretLine).Offset
EditActions.ClipboardPaste editor
EditActions.MoveCaretUp editor
else
EditActions.ClipboardPaste editor
vimState
| Put After ->
if registers.[EmptyRegister].EndsWith "\n" then
if registers.[EmptyRegister].linewise then
if editor.CaretLine = editor.LineCount then
let line = editor.GetLine(editor.CaretLine-1)
let delimiter = NewLine.GetString line.UnicodeNewline
let line = editor.GetLine(editor.CaretLine)
let delimiter = editor.Options.DefaultEolMarker
editor.InsertText(editor.Text.Length, delimiter)
editor.CaretOffset <- editor.Text.Length
EditActions.ClipboardPaste editor
editor.RemoveText(editor.Text.Length-line.DelimiterLength, line.DelimiterLength)
if eofOnLine line && registers.[EmptyRegister].content.EndsWith delimiter then
editor.RemoveText(editor.Text.Length-delimiter.Length, delimiter.Length)
EditActions.MoveCaretToLineStart editor
else
editor.CaretOffset <- editor.GetLine(editor.CaretLine).EndOffset+1
Expand Down

0 comments on commit a00cc00

Please sign in to comment.