Skip to content
This repository was archived by the owner on Apr 6, 2018. It is now read-only.

Commit 42047a5

Browse files
committed
fix #565 : cursor motion in insert mode should affect .
1 parent 272d174 commit 42047a5

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

keymaps/vim-mode.cson

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
'atom-text-editor.vim-mode.insert-mode':
1414
'ctrl-w': 'editor:delete-to-beginning-of-word'
1515
'ctrl-u': 'editor:delete-to-beginning-of-line'
16+
'left': 'vim-mode:move-left-insert'
17+
'right': 'vim-mode:move-right-insert'
18+
'up': 'vim-mode:move-up-insert'
19+
'down': 'vim-mode:move-down-insert'
1620

1721
'atom-text-editor.vim-mode:not(.insert-mode)':
1822
'h': 'vim-mode:move-left'

lib/operators/input.coffee

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ class Insert extends Operator
2828

2929
inputOperator: -> true
3030

31+
# an insert operation following cursor motion in insert mode can be cancelled
32+
# and forgotten like it never happened
33+
class InsertCancellable extends Insert
34+
35+
confirmTransaction: (transaction) ->
36+
super
37+
if @typedText?.length is 0
38+
@vimState.history.shift() if @vimState.history[0] is this
39+
3140
class InsertAfter extends Insert
3241
execute: ->
3342
@editor.moveRight() unless @editor.getLastCursor().isAtEndOfLine()
@@ -189,6 +198,7 @@ module.exports = {
189198
InsertAtBeginningOfLine,
190199
InsertAboveWithNewline,
191200
InsertBelowWithNewline,
201+
InsertCancellable,
192202
Change,
193203
Substitute,
194204
SubstituteLine

lib/vim-state.coffee

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ class VimState
102102
'move-up': => new Motions.MoveUp(@editor, this)
103103
'move-down': => new Motions.MoveDown(@editor, this)
104104
'move-right': => new Motions.MoveRight(@editor, this)
105+
'move-left-insert': => @interruptInsertMode(); [new Motions.MoveLeft(@editor, this), new Operators.InsertCancellable(@editor, this)]
106+
'move-up-insert': => @interruptInsertMode(); [new Motions.MoveUp(@editor, this), new Operators.InsertCancellable(@editor, this)]
107+
'move-down-insert': => @interruptInsertMode(); [new Motions.MoveDown(@editor, this), new Operators.InsertCancellable(@editor, this)]
108+
'move-right-insert': => @interruptInsertMode(); [new Motions.MoveRight(@editor, this), new Operators.InsertCancellable(@editor, this)]
105109
'move-to-next-word': => new Motions.MoveToNextWord(@editor, this)
106110
'move-to-next-whole-word': => new Motions.MoveToNextWholeWord(@editor, this)
107111
'move-to-end-of-word': => new Motions.MoveToEndOfWord(@editor, this)
@@ -412,6 +416,17 @@ class VimState
412416
for cursor in @editor.getCursors()
413417
cursor.moveLeft() unless cursor.isAtBeginningOfLine()
414418

419+
interruptInsertMode: ->
420+
return unless @mode is 'insert'
421+
@editor.groupChangesSinceCheckpoint(@insertionCheckpoint)
422+
changes = getChangesSinceCheckpoint(@editor.buffer, @insertionCheckpoint)
423+
item = @inputOperator(@history[0])
424+
@insertionCheckpoint = null
425+
if item?
426+
item.confirmChanges(changes)
427+
@setInsertionCheckpoint()
428+
429+
415430
deactivateVisualMode: ->
416431
return unless @mode is 'visual'
417432
for selection in @editor.getSelections()

spec/operators-spec.coffee

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,17 @@ describe "Operators", ->
15731573
keydown '.'
15741574
expect(editor.getText()).toBe "abababccc123\nabababccc4567"
15751575

1576+
it "stores for repeating only the last batch of characters", ->
1577+
keydown 'i'
1578+
editor.insertText("abc")
1579+
atom.commands.dispatch editorElement, 'vim-mode:move-left-insert'
1580+
editor.insertText("de")
1581+
keydown 'escape'
1582+
expect(editor.getText()).toBe "abdec123\nabdec4567"
1583+
1584+
keydown '.'
1585+
expect(editor.getText()).toBe "abddeec123\nabddeec4567"
1586+
15761587
describe 'the a keybinding', ->
15771588
beforeEach ->
15781589
editor.setText('')
@@ -1596,6 +1607,18 @@ describe "Operators", ->
15961607
expect(editor.getText()).toBe "abcabc"
15971608
expect(editor.getCursorScreenPosition()).toEqual [0, 5]
15981609

1610+
it "stores for repeating only the last batch of characters, repeats as insert", ->
1611+
keydown 'a'
1612+
editor.insertText("abc")
1613+
atom.commands.dispatch editorElement, 'vim-mode:move-left-insert'
1614+
editor.insertText("de")
1615+
keydown 'escape'
1616+
expect(editor.getText()).toBe "abdec"
1617+
expect(editor.getCursorScreenPosition()).toEqual [0, 3]
1618+
keydown '.'
1619+
expect(editor.getText()).toBe "abddeec"
1620+
expect(editor.getCursorScreenPosition()).toEqual [0, 4]
1621+
15991622
describe "the ctrl-a/ctrl-x keybindings", ->
16001623
beforeEach ->
16011624
editor.setText('123\nab45\ncd-67ef\nab-5\na-bcdef')

0 commit comments

Comments
 (0)