Skip to content

Commit

Permalink
Move to desired column when moving up or down. Fixes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
nosami committed Apr 25, 2017
1 parent f57f332 commit 4be048b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
6 changes: 5 additions & 1 deletion XSVim.Tests/Movement.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ module ``Movement tests`` =

[<Test>]
let ``Move to start of document``() =
assertText "aaaaaa\nbb$bbbb" "gg" "a$aaaaa\nbbbbbb"
assertText "aaaaaa\nbb$bbbb" "gg" "a$aaaaa\nbbbbbb"

[<Test>]
let ``Move down to desired column``() =
assertText "12345$6\n123\n123456" "jj" "123456\n123\n12345$6"
2 changes: 1 addition & 1 deletion XSVim.Tests/TestHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ module TestHelpers =
editor.CaretOffset <- caret-1
//editor.Caret.UpdateCaretOffset()
let plugin = new XSVim()
let state = { keys=[]; mode=NormalMode; visualStartOffset=0; findCharCommand=None; lastAction=[]; clipboard="" }
let state = { keys=[]; mode=NormalMode; visualStartOffset=0; findCharCommand=None; lastAction=[]; clipboard=""; desiredColumn=0 }
let newState =
keys |> Seq.fold(fun state c ->
let descriptor = KeyDescriptor.FromGtk(Gdk.Key.a (* important? *), c, Gdk.ModifierType.None)
Expand Down
40 changes: 34 additions & 6 deletions XSVim/XSVim.fs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type VimState = {
findCharCommand: VimAction option // f,F,t or T command to be repeated with ;
lastAction: VimAction list // used by . command to repeat the last action
clipboard: string
desiredColumn: int
}

[<AutoOpen>]
Expand Down Expand Up @@ -226,13 +227,25 @@ module VimHelpers =
| Up ->
editor.CaretOffset,
if editor.CaretLine > DocumentLocation.MinLine then
editor.LocationToOffset (new DocumentLocation(editor.CaretLine - 1, editor.CaretColumn))
let column =
if vimState.desiredColumn > editor.CaretColumn && vimState.desiredColumn <= editor.Length then
vimState.desiredColumn
else
editor.CaretColumn

editor.LocationToOffset (new DocumentLocation(editor.CaretLine - 1, column))
else
editor.CaretOffset
| Down ->
editor.CaretOffset,
if editor.CaretLine < editor.LineCount then
editor.LocationToOffset (new DocumentLocation(editor.CaretLine + 1, editor.CaretColumn))
let column =
if vimState.desiredColumn > editor.CaretColumn && vimState.desiredColumn <= editor.Length then
vimState.desiredColumn
else
editor.CaretColumn

editor.LocationToOffset (new DocumentLocation(editor.CaretLine + 1, column))
else
editor.CaretOffset
| EndOfLine -> editor.CaretOffset, line.EndOffset-1
Expand Down Expand Up @@ -360,6 +373,12 @@ module Vim =
editor.SetSelection(startLine.Offset, endLine.EndOffsetIncludingDelimiter)
| _ -> editor.SetSelection(start, finish)

let (|MoveUpOrDown|_|) command =
match command with
| { commandType=Move; textObject=Up}
| { commandType=Move; textObject=Down} -> Some MoveUpOrDown
| _ -> None

let runCommand vimState editor command =
let delete state start finish =
let finish =
Expand Down Expand Up @@ -390,17 +409,25 @@ module Vim =
let newState =
match command.commandType with
| Move ->
editor.CaretOffset <- finish
match vimState.mode with
| VisualModes ->
editor.CaretOffset <- finish
let finish =
match command.textObject with
| EndOfLine -> finish + 1
| _ -> finish

setSelection vimState editor command start finish
| _ -> ()
vimState
let newState =
match command, vimState.lastAction with
// don't change desired column if we already started moving up or down
| MoveUpOrDown, [ MoveUpOrDown ] -> vimState
| MoveUpOrDown, _ -> { vimState with desiredColumn = editor.CaretColumn }
| _ -> vimState
editor.CaretOffset <- finish
newState

| Delete -> delete vimState start finish
| DeleteLeft -> if editor.CaretColumn > 1 then delete vimState (editor.CaretOffset - 1) editor.CaretOffset else vimState
| Change ->
Expand Down Expand Up @@ -724,15 +751,16 @@ module Vim =
let newState = runCommand state editorData h
performActions t { newState with keys = [] } true

performActions action { newState with lastAction = action } false
let newState, handled = performActions action newState false
{ newState with lastAction = action }, handled

type XSVim() =
inherit TextEditorExtension()
static let editorStates = Dictionary<FilePath, VimState>()

override x.Initialize() =
if not (editorStates.ContainsKey x.Editor.FileName) then
editorStates.Add(x.Editor.FileName, { keys=[]; mode=NormalMode; visualStartOffset=0; findCharCommand=None; lastAction=[]; clipboard="" })
editorStates.Add(x.Editor.FileName, { keys=[]; mode=NormalMode; visualStartOffset=0; findCharCommand=None; lastAction=[]; clipboard=""; desiredColumn=0 })
EditActions.SwitchCaretMode x.Editor

override x.KeyPress descriptor =
Expand Down

0 comments on commit 4be048b

Please sign in to comment.