From 1e24a59ffe16ea411511c15c1f906262e6784474 Mon Sep 17 00:00:00 2001 From: nosami Date: Fri, 19 May 2017 14:10:44 +0100 Subject: [PATCH] dw and cw at last word shouldn't delete delimiter. Fixes #46 --- XSVim.Tests/DeleteTests.fs | 8 ++++++-- XSVim/Properties/AddinInfo.fs | 2 +- XSVim/Types.fs | 1 - XSVim/XSVim.fs | 33 ++++++++++++++++++--------------- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/XSVim.Tests/DeleteTests.fs b/XSVim.Tests/DeleteTests.fs index d5690ce..5b2bf35 100644 --- a/XSVim.Tests/DeleteTests.fs +++ b/XSVim.Tests/DeleteTests.fs @@ -40,7 +40,7 @@ module ``Delete tests`` = [] let ``Delete to end of line``() = - assertText "abc$ def\nghi" "d$" "ab\n$ghi" + assertText "abc$ def\nghi" "d$" "ab$\nghi" [] let ``Delete to end of document``() = @@ -52,7 +52,11 @@ module ``Delete tests`` = [] let ``Delete to end of line using D``() = - assertText "abc$ def\nghi" "D" "ab\n$ghi" + assertText "abc$ def\nghi" "D" "ab$\nghi" + + [] + let ``Deleting last word doesn't delete delimiter'``() = + assertText "abc d$ef\nghi" "dw" "abc $\nghi" [] let ``Delete char to left doesn't delete past start of line``() = diff --git a/XSVim/Properties/AddinInfo.fs b/XSVim/Properties/AddinInfo.fs index 95d7454..ae75be3 100644 --- a/XSVim/Properties/AddinInfo.fs +++ b/XSVim/Properties/AddinInfo.fs @@ -5,7 +5,7 @@ open MonoDevelop [] [] diff --git a/XSVim/Types.fs b/XSVim/Types.fs index f860f42..29c85d3 100644 --- a/XSVim/Types.fs +++ b/XSVim/Types.fs @@ -55,7 +55,6 @@ type TextObject = | Left | Right | RightIncludingDelimiter - | EnsureCursorBeforeDelimiter | FirstNonWhitespace | StartOfLine | StartOfLineNumber of int diff --git a/XSVim/XSVim.fs b/XSVim/XSVim.fs index 48188e2..0f1b5d1 100755 --- a/XSVim/XSVim.fs +++ b/XSVim/XSVim.fs @@ -54,10 +54,10 @@ module VimHelpers = else None - let findWordForwards (editor:TextEditor) = + let findWordForwards (editor:TextEditor) commandType = let findFromNonLetterChar index = - match editor.Text.[index] with - | InvisibleChar -> + match editor.Text.[index], commandType with + | InvisibleChar, Move -> seq { index+1 .. editor.Text.Length-1 } |> Seq.tryFind(fun index -> not (Char.IsWhiteSpace editor.Text.[index])) | _ -> Some index @@ -139,9 +139,9 @@ module VimHelpers = let eofOnLine (line: IDocumentLine) = line.EndOffset = line.EndOffsetIncludingDelimiter - let getRange (vimState:VimState) (editor:TextEditor) motion = + let getRange (vimState:VimState) (editor:TextEditor) (command:VimAction) = let line = editor.GetLine editor.CaretLine - match motion with + match command.textObject with | Right -> let line = editor.GetLine editor.CaretLine editor.CaretOffset, if editor.CaretColumn < line.Length then editor.CaretOffset + 1 else editor.CaretOffset @@ -152,9 +152,6 @@ module VimHelpers = editor.CaretOffset + 1 else editor.CaretOffset - | EnsureCursorBeforeDelimiter -> - let line = editor.GetLine editor.CaretLine - editor.CaretOffset, if editor.CaretColumn < line.Length then editor.CaretOffset else editor.CaretOffset - 1 | Left -> editor.CaretOffset, if editor.CaretColumn > DocumentLocation.MinColumn then editor.CaretOffset - 1 else editor.CaretOffset | Up -> editor.CaretOffset, @@ -233,7 +230,7 @@ module VimHelpers = | Some start, Some finish when finish < editor.Text.Length -> start, finish+1 | _, _ -> editor.CaretOffset, editor.CaretOffset | WordForwards -> - match findWordForwards editor with + match findWordForwards editor command.commandType with | Some index -> editor.CaretOffset, index | None -> editor.CaretOffset, editor.CaretOffset | WordBackwards -> editor.CaretOffset, findPrevWord editor @@ -338,6 +335,8 @@ module Vim = | ToCharInclusive _ | ToCharExclusive _ -> finish + 1 | _ -> finish + + if command.textObject <> SelectedText then setSelection state editor command start finish clipboard <- editor.SelectedText @@ -356,7 +355,7 @@ module Vim = { state with mode = InsertMode; statusMessage = "-- INSERT --" |> Some; keys = []; undoGroup = Some group } let rec processCommands count vimState command = - let start, finish = VimHelpers.getRange vimState editor command.textObject + let start, finish = VimHelpers.getRange vimState editor command let newState = match command.commandType with | Move -> @@ -385,7 +384,12 @@ module Vim = { vimState with desiredColumn = Some editor.CaretColumn } newState - | Delete -> delete vimState start finish + | Delete -> + let newState = delete vimState start finish + let line = editor.GetLine editor.CaretLine + let offsetBeforeDelimiter = if editor.CaretColumn < line.Length then editor.CaretOffset else editor.CaretOffset - 1 + editor.CaretOffset <- Math.Max(offsetBeforeDelimiter, 0) + newState | DeleteWholeLines -> let min = Math.Min(start, finish) let max = Math.Max(start, finish) @@ -486,7 +490,7 @@ module Vim = { vimState with mode = mode; undoGroup = None; statusMessage = None } | VisualMode | VisualLineMode | VisualBlockMode -> setCaretMode Block - let start, finish = VimHelpers.getRange vimState editor command.textObject + 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 match mode, editor.SelectionMode with @@ -688,9 +692,9 @@ module Vim = | NormalMode, [ "Y" ] -> [ run Yank WholeLineIncludingDelimiter ] | NormalMode, [ "C" ] -> [ run Change EndOfLine ] | NormalMode, [ "D" ] -> [ run Delete EndOfLine ] - | NormalMode, [ "x" ] -> [ run Delete CurrentLocation; run Move EnsureCursorBeforeDelimiter ] + | NormalMode, [ "x" ] -> [ run Delete CurrentLocation ] | NormalMode, [ "X" ] -> [ run DeleteLeft Nothing ] - | NormalMode, [ "s"] -> [ run Delete CurrentLocation; run Move EnsureCursorBeforeDelimiter; switchMode InsertMode ] + | NormalMode, [ "s"] -> [ run Delete CurrentLocation; switchMode InsertMode ] | NormalMode, [ "p" ] -> [ run (Put After) Nothing ] | NormalMode, [ "P" ] -> [ run (Put Before) Nothing ] | VisualModes, [ "p" ] -> [ run (Put OverSelection) Nothing ] @@ -797,7 +801,6 @@ module Vim = let newState, handled = performActions action newState false { newState with lastAction = action }, handled - type XSVim() = inherit TextEditorExtension() static let editorStates = Dictionary()