Use cursor:position-changed to make sure cursor never gets to end of line in command mode.

The exception being an empty line.
This commit is contained in:
Corey Johnson
2012-02-06 14:56:05 -08:00
parent f3eb6fc66c
commit bac59b7abf
4 changed files with 26 additions and 8 deletions

View File

@@ -25,6 +25,16 @@ describe "VimMode", ->
editor.trigger event
expect(event.stopPropagation).not.toHaveBeenCalled()
it "does not allow the cursor to be placed on the \n charachter, unless the line is empty", ->
editor.buffer.setText("012345\n\nabcdef")
editor.setCursorPosition([0, 5])
expect(editor.getCursorPosition()).toEqual [0,5]
editor.setCursorPosition([0, 6])
expect(editor.getCursorPosition()).toEqual [0,5]
editor.setCursorPosition([1, 0])
expect(editor.getCursorPosition()).toEqual [1,0]
describe "the i keybinding", ->
it "puts the editor into insert mode", ->
@@ -52,6 +62,13 @@ describe "VimMode", ->
expect(editor.buffer.getText()).toBe '012'
expect(editor.getCursorPosition()).toEqual([0, 2])
it "deletes nothing when cursor is on empty line", ->
editor.buffer.setText "012345\n\nabcdef"
editor.setCursorPosition [1, 0]
editor.trigger keydownEvent 'x'
expect(editor.buffer.getText()).toBe "012345\n\nabcdef"
describe "the d keybinding", ->
describe "when followed by a d", ->
it "deletes the current line", ->
@@ -177,7 +194,7 @@ describe "VimMode", ->
editor.setCursorPosition [3,0]
editor.trigger keydownEvent('w')
expect(editor.getCursorPosition()).toEqual([3,3])
expect(editor.getCursorPosition()).toEqual([3,2])
describe "numeric prefix bindings", ->
it "repeats the following operation N times", ->

View File

@@ -13,7 +13,7 @@ class VimMode
requireStylesheet 'vim-mode.css'
@opStack = []
@editor.addClass('command-mode')
@activateCommandMode()
atom.bindKeys '.editor', '<esc>': 'activate-command-mode'
@editor.on 'activate-command-mode', => @activateCommandMode()
@@ -70,6 +70,11 @@ class VimMode
@editor.removeClass('insert-mode')
@editor.addClass('command-mode')
@editor.on 'cursor:position-changed', =>
moveCursorBeforeNewline = not @editor.selection.modifyingSelection and @editor.cursor.isOnEOL() and @editor.getCurrentLine().length > 0
if moveCursorBeforeNewline
@editor.setCursorColumn(@editor.getCurrentLine().length - 1)
numericPrefix: (e) ->
num = parseInt(e.keyEvent.keystroke)
if @topOperator() instanceof operators.NumericPrefix

View File

@@ -4,10 +4,7 @@ class Command
class DeleteRight extends Command
execute: ->
@editor.delete()
isOnEOL = @editor.getCursorColumn() == @editor.getCurrentLine().length
if isOnEOL
@editor.setCursorColumn(@editor.getCursorColumn() - 1)
@editor.delete() unless @editor.getCurrentLine().length == 0
module.exports = { DeleteRight }

View File

@@ -17,8 +17,7 @@ class MoveLeft extends Motion
class MoveRight extends Motion
execute: ->
{column, row} = @editor.getCursorPosition()
isOnLastCharachter = @editor.getCursorColumn() == @editor.getCurrentLine().length - 1
@editor.moveCursorRight() unless isOnLastCharachter
@editor.moveCursorRight()
class MoveUp extends Motion
execute: ->