'w' moves to next word (not fully functional for multi-line)

This commit is contained in:
Nathan Sobo
2012-01-13 19:17:56 -08:00
parent b2648723de
commit 7546ddc8cc
5 changed files with 40 additions and 1 deletions

View File

@@ -77,6 +77,15 @@ describe "VimMode", ->
editor.trigger keydownEvent('j')
expect(editor.getCursor()).toEqual(column: 1, row: 0)
describe "the w keybinding", ->
it "moves the cursor to the beginning of the next word", ->
editor.buffer.setText("ab cde 123+- \n xyz")
editor.setCursor(column: 0, row: 0)
editor.trigger keydownEvent('w')
expect(editor.getCursor()).toEqual(column: 3, row: 0)
describe "numeric prefix bindings", ->
it "repeats the following operation N times", ->
editor.buffer.setText("12345")

View File

@@ -41,3 +41,7 @@ class Buffer
save: ->
if not @url then throw new Error("Tried to save buffer with no url")
fs.write @url, @getText()
getLine: (row) ->
@aceDocument.getLine(row)

View File

@@ -56,11 +56,18 @@ class Editor extends Template
@buffer.save()
setCursor: ({column, row}) ->
@aceEditor.moveCursorToPosition({column, row})
@aceEditor.navigateTo(row, column)
getCursor: ->
@getAceSession().getSelection().getCursor()
getLineText: ->
@buffer.getLine(@getRow())
getRow: ->
{ row } = @getCursor()
row
deleteChar: ->
@aceEditor.remove 'right'

View File

@@ -1,5 +1,7 @@
_ = require 'underscore'
getWordRegex = -> /(\w+)|([^\w\s]+)/g
module.exports =
NumericPrefix: class
count: null
@@ -48,3 +50,18 @@ module.exports =
isComplete: -> true
MoveToNextWord: class
execute: (editor) ->
regex = getWordRegex()
{ row, column } = editor.getCursor()
rightOfCursor = editor.getLineText().substring(column)
match = regex.exec(rightOfCursor)
# If we're on top of part of a word, match the next one.
match = regex.exec(rightOfCursor) if match?.index is 0
column += match.index
editor.setCursor { row, column }
isComplete: -> true

View File

@@ -29,6 +29,7 @@ class VimMode
'x': 'delete-char'
'h': 'move-left'
'j': 'move-up'
'w': 'move-to-next-word'
@handleCommands
'insert': => @activateInsertMode()
@@ -36,6 +37,7 @@ class VimMode
'delete-char': => new op.DeleteChar
'move-left': => new op.MoveLeft
'move-up': => new op.MoveUp
'move-to-next-word': => new op.MoveToNextWord
'numeric-prefix': (e) => @numericPrefix(e)
bindCommandModeKeys: (bindings) ->