Skip to content

Commit

Permalink
Merge pull request #67 from nosami/c2w-fixes
Browse files Browse the repository at this point in the history
c2w fixes #62
  • Loading branch information
nosami committed May 28, 2017
2 parents 17297f7 + 05b2b4c commit 71c9040
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
16 changes: 16 additions & 0 deletions XSVim.Tests/ChangeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ module ``Change tests`` =
let ``cw changes word``() =
assertText "a$bc def" "cw" "| def"

[<Test>]
let ``cw changes space``() =
assertText "abc $def" "cw" "abc|def"

[<Test>]
let ``c2w changes two words``() =
assertText "a$bc def ghi" "c2w" "| ghi"

[<Test>]
let ``undo works after cw``() =
assertText "a$bc def ghi" "cw<esc>u" "a$bc def ghi"

[<Test>]
let ``undo works after c2w``() =
assertText "a$bc def ghi" "c2w<esc>u" "a$bc def ghi"

[<Test>]
let ``ce changes word``() =
assertText "a$bc def" "ce" "| def"
Expand Down
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.23.2"
Version = "0.23.3"
)>]

[<assembly:AddinName ("Vim")>]
Expand Down
47 changes: 27 additions & 20 deletions XSVim/XSVim.fs
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,21 @@ module Vim =
EditActions.ClipboardCut editor
state

let setCaretMode caretMode =
match vimState.mode, caretMode with
let setCaretMode state caretMode =
match state.mode, caretMode with
| NotInsertMode, Insert -> EditActions.SwitchCaretMode editor
| InsertMode, Block -> EditActions.SwitchCaretMode editor
| _ -> ()

let switchToInsertMode (editor:TextEditor) state =
let group = editor.OpenUndoGroup()
setCaretMode Insert
{ state with mode = InsertMode; statusMessage = "-- INSERT --" |> Some; keys = []; undoGroup = Some group }
let switchToInsertMode (editor:TextEditor) state isInitial =
//let group = state.undoGroup |> Option.orElse (editor.OpenUndoGroup() |> Some)
let group =
if isInitial
then editor.OpenUndoGroup() |> Some
else
state.undoGroup
setCaretMode state Insert
{ state with mode = InsertMode; statusMessage = "-- INSERT --" |> Some; keys = []; undoGroup = group }

let toggleCase state start finish =
if command.textObject <> SelectedText then
Expand All @@ -382,7 +387,7 @@ module Vim =
editor.CaretOffset <- state.visualStartOffset
state

let rec processCommands count vimState command =
let rec processCommands count vimState command isInitial =
let start, finish = VimHelpers.getRange vimState editor command
let newState =
match command.commandType with
Expand Down Expand Up @@ -434,13 +439,18 @@ module Vim =
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 ->
| Change ->
let finish =
if count <> 1 && editor.[finish] = ' ' then
finish + 1
else
finish
let state =
if start <> finish then
delete vimState start finish
else
vimState
switchToInsertMode editor state
switchToInsertMode editor state isInitial
| Yank ->
let finish =
match command.textObject with
Expand Down Expand Up @@ -521,16 +531,16 @@ module Vim =
editor.CaretColumn <- Math.Min(editor.CaretColumn, selectionStartLocation.Column)
editor.SetSelection(new DocumentLocation (topLine, selectionStartLocation.Column),new DocumentLocation (bottomLine, selectionStartLocation.Column))
if editor.SelectionMode = SelectionMode.Normal then dispatch TextEditorCommands.ToggleBlockSelectionMode
switchToInsertMode editor vimState
switchToInsertMode editor vimState isInitial
| SwitchMode mode ->
match mode with
| NormalMode ->
editor.ClearSelection()
setCaretMode Block
setCaretMode vimState Block
vimState.undoGroup |> Option.iter(fun d -> d.Dispose())
{ vimState with mode = mode; undoGroup = None; statusMessage = None }
| VisualMode | VisualLineMode | VisualBlockMode ->
setCaretMode Block
setCaretMode vimState Block
let start, finish = VimHelpers.getRange vimState editor command
let newState = { vimState with mode = mode; visualStartOffset = editor.CaretOffset; statusMessage = None }
setSelection newState editor command start finish
Expand All @@ -540,7 +550,7 @@ module Vim =
| _ -> ()
newState
| InsertMode ->
switchToInsertMode editor vimState
switchToInsertMode editor vimState isInitial
| Star After ->
match wordAtCaret editor with
| Some word ->
Expand All @@ -560,7 +570,7 @@ module Vim =
editor.CaretOffset <- offset
vimState
| None ->
processCommands 1 vimState (runOnce Move WordForwards)
processCommands 1 vimState (runOnce Move WordForwards) isInitial
| Star Before ->
match wordAtCaret editor with
| Some word ->
Expand All @@ -586,15 +596,12 @@ module Vim =
editor.InsertAtCaret c
vimState
| _ -> vimState
if count = 1 then newState else processCommands (count-1) newState command
let count =
match command.repeat with
| Some r -> r
| None -> 1
if count = 1 then newState else processCommands (count-1) newState command false
let count = command.repeat |> Option.defaultValue 1

use group = editor.OpenUndoGroup()

processCommands count vimState command
processCommands count vimState command true

let (|Digit|_|) character =
if character >= "0" && character <= "9" then
Expand Down

0 comments on commit 71c9040

Please sign in to comment.