Skip to content

Commit

Permalink
dG and dgg fixes #29
Browse files Browse the repository at this point in the history
  • Loading branch information
nosami committed May 5, 2017
1 parent 51eeac3 commit ae1ce50
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 29 deletions.
8 changes: 8 additions & 0 deletions XSVim.Tests/DeleteTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ module ``Delete tests`` =
let ``Delete to end of line``() =
assertText "abc$ def\nghi" "d$" "ab\n$ghi"

[<Test>]
let ``Delete to end of document``() =
assertText "abc\nde$f\nghi" "dG" "abc\n$"

[<Test>]
let ``Delete to start of document``() =
assertText "abc\nde$f\nghi" "dgg" "g$hi"

[<Test>]
let ``Delete to end of line using D``() =
assertText "abc$ def\nghi" "D" "ab\n$ghi"
Expand Down
5 changes: 4 additions & 1 deletion XSVim.Tests/TestHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ module TestHelpers =
if newState.mode = InsertMode then
editor.Text.Insert(editor.CaretOffset, "|")
else
editor.Text.Insert(editor.CaretOffset+1, "$")
if editor.CaretOffset = editor.Text.Length then
editor.Text + "$"
else
editor.Text.Insert(editor.CaretOffset+1, "$")
text, newState

let assertText (source:string) (keys:string) expected =
Expand Down
22 changes: 21 additions & 1 deletion XSVim.Tests/VisualTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,24 @@ module ``Visual tests`` =
[<Test>]
let ``Visual line``() =
let _, state = test "aaa\nbb$b\nddd" "Vy"
Vim.clipboard |> should equal "bbb\n"
Vim.clipboard |> should equal "bbb\n"

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

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

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

[<Test>]
let ``Visual line to start of document``() =
let _, state = test "abc\nde$f\nghi" "Vggy"
Vim.clipboard |> should equal "abc\ndef\n"
66 changes: 39 additions & 27 deletions XSVim/XSVim.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type CommandType =
| Yank
| Put of BeforeOrAfter
| Delete
| DeleteWholeLines
| DeleteLeft
| BlockInsert
| Change
Expand Down Expand Up @@ -50,6 +51,7 @@ type TextObject =
| InnerBlock of string * string
| WholeLine
| WholeLineIncludingDelimiter
| WholeLineToEndOfDocument
| LastLine
// motions
| Up
Expand Down Expand Up @@ -277,6 +279,7 @@ module VimHelpers =
| FirstNonWhitespace -> editor.CaretOffset, line.Offset + editor.GetLineIndent(editor.CaretLine).Length
| WholeLine -> line.Offset, line.EndOffset
| WholeLineIncludingDelimiter -> line.Offset, line.EndOffsetIncludingDelimiter
| WholeLineToEndOfDocument -> line.Offset, editor.Text.Length
| LastLine ->
let lastLine = editor.GetLine editor.LineCount
editor.CaretOffset, lastLine.Offset
Expand Down Expand Up @@ -458,6 +461,12 @@ module Vim =
newState

| Delete -> delete vimState start finish
| DeleteWholeLines ->
let min = Math.Min(start, finish)
let max = Math.Max(start, finish)
let start = editor.GetLineByOffset(min).Offset
let finish = editor.GetLineByOffset(max).EndOffsetIncludingDelimiter
delete vimState start finish
| DeleteLeft -> if editor.CaretColumn > 1 then delete vimState (editor.CaretOffset - 1) editor.CaretOffset else vimState
| Change ->
let state = delete vimState start finish
Expand Down Expand Up @@ -582,26 +591,26 @@ module Vim =

let (|Movement|_|) character =
match character with
| "h" -> Some Left
| "j" -> Some Down
| "k" -> Some Up
| "l" -> Some Right
| "$" -> Some EndOfLine
| "^" -> Some FirstNonWhitespace
| "0" -> Some StartOfLine
| "_" -> Some FirstNonWhitespace
| "w" -> Some WordForwards
| "b" -> Some WordBackwards
| "e" -> Some ForwardToEndOfWord
| "E" -> Some BackwardToEndOfWord

| "{" -> Some ParagraphBackwards
| "}" -> Some ParagraphForwards
| "%" -> Some MatchingBrace
| "<C-d>" -> Some HalfPageDown
| "<C-u>" -> Some HalfPageUp
| "<C-f>" -> Some PageDown
| "<C-b>" -> Some PageUp
| ["h"] -> Some Left
| ["j"] -> Some Down
| ["k"] -> Some Up
| ["l"] -> Some Right
| ["$"] -> Some EndOfLine
| ["^"] -> Some FirstNonWhitespace
| ["0"] -> Some StartOfLine
| ["_"] -> Some FirstNonWhitespace
| ["w"] -> Some WordForwards
| ["b"] -> Some WordBackwards
| ["e"] -> Some ForwardToEndOfWord
| ["E"] -> Some BackwardToEndOfWord
| ["{"] -> Some ParagraphBackwards
| ["}"] -> Some ParagraphForwards
| ["%"] -> Some MatchingBrace
| ["G"] -> Some LastLine
| ["<C-d>"] -> Some HalfPageDown
| ["<C-u>"] -> Some HalfPageUp
| ["<C-f>"] -> Some PageDown
| ["<C-b>"] -> Some PageUp
| _ -> None

let (|FindChar|_|) character =
Expand Down Expand Up @@ -678,9 +687,16 @@ module Vim =
| VisualBlockMode, [ Escape ] -> [ run Move SelectionStart; switchMode NormalMode ]
| NormalMode, [ Escape ] -> [ run ResetKeys Nothing ]
| _, [ Escape ] -> [ run (SwitchMode NormalMode) Nothing; run Move Left ]
| NotInsertMode, [ Movement m ] -> [ run Move m ]
| NotInsertMode, [ "G" ] ->
match numericArgument with
| Some lineNumber -> [ runOnce Move (StartOfLineNumber lineNumber) ]
| None -> [ runOnce Move LastLine ]
| NormalMode, [ "d"; "G" ] -> [ runOnce DeleteWholeLines LastLine]
| NormalMode, [ "d"; "g" ] -> wait
| NormalMode, [ "d"; "g"; "g" ] -> [ runOnce DeleteWholeLines StartOfDocument]
| NotInsertMode, Movement m -> [ run Move m ]
| NotInsertMode, [ FindChar m; c ] -> [ run Move (m c) ]
| NormalMode, [ Action action; Movement m ] -> [ run action m ]
| NormalMode, Action action :: Movement m -> [ run action m ]
| NormalMode, [ "u" ] -> [ run Undo Nothing ]
| NormalMode, [ "<C-r>" ] -> [ run Redo Nothing ]
| NormalMode, [ "d"; "d" ] -> [ run Delete WholeLineIncludingDelimiter ]
Expand Down Expand Up @@ -725,10 +741,6 @@ module Vim =
| VisualMode, [ "i" ] | VisualMode, [ "a" ] -> wait
| NotInsertMode, [ FindChar _; ] -> wait
| NotInsertMode, [ Action _; FindChar _; ] -> wait
| NotInsertMode, [ "G" ] ->
match numericArgument with
| Some lineNumber -> [ runOnce Move (StartOfLineNumber lineNumber) ]
| None -> [ runOnce Move LastLine ]
| NotInsertMode, [ "g" ] -> wait
| NotInsertMode, [ "g"; "g" ] ->
let lineNumber = match numericArgument with Some n -> n | None -> 1
Expand All @@ -738,7 +750,7 @@ module Vim =
| NotInsertMode, [ "g"; "T" ] -> [ dispatch WindowCommands.PrevDocument ]
| NotInsertMode, [ "." ] -> state.lastAction
| NotInsertMode, [ ";" ] -> match state.findCharCommand with Some command -> [ command ] | None -> []
| VisualModes, [ Movement m ] -> [ run Move m ]
| VisualModes, Movement m -> [ run Move m ]
| VisualBlockMode, [ "I" ] -> [ run BlockInsert Nothing; ]
| VisualModes, [ "i"; BlockDelimiter c ] -> [ run Visual (InnerBlock c) ]
| VisualModes, [ "a"; BlockDelimiter c ] -> [ run Visual (ABlock c) ]
Expand Down

0 comments on commit ae1ce50

Please sign in to comment.