Implemented MoveToNextParagraph motion

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-02-10 10:41:02 -08:00
parent 6d39306fb2
commit 9a922b1c2d
3 changed files with 57 additions and 13 deletions

View File

@@ -235,6 +235,23 @@ describe "VimMode", ->
editor.trigger keydownEvent('w')
expect(editor.getCursorPosition()).toEqual([3,2])
describe "the { keybinding", ->
fit "moves the cursor to the beginning of the paragraph", ->
editor.buffer.setText("abcde\n\nfghij\nhijk\n xyz \n\nzip\n\n \nthe end")
editor.setCursorPosition([0,0])
editor.trigger keydownEvent('}')
expect(editor.getCursorPosition()).toEqual [1,0]
editor.trigger keydownEvent('}')
expect(editor.getCursorPosition()).toEqual [5,0]
editor.trigger keydownEvent('}')
expect(editor.getCursorPosition()).toEqual [7,0]
editor.trigger keydownEvent('}')
expect(editor.getCursorPosition()).toEqual [9,6]
describe "the b keybinding", ->
it "moves the cursor to the beginning of the previous word", ->
editor.buffer.setText(" ab cde1+- \n xyz\n\nzip }\n last")

View File

@@ -29,18 +29,19 @@ class VimMode
return false
@bindCommandModeKeys
i: 'insert'
d: 'delete'
x: 'delete-right'
h: 'move-left'
j: 'move-down'
k: 'move-up'
l: 'move-right'
w: 'move-to-next-word'
b: 'move-to-previous-word'
esc: 'reset-command-mode'
left: 'move-left'
right: 'move-right'
'i': 'insert'
'd': 'delete'
'x': 'delete-right'
'h': 'move-left'
'j': 'move-down'
'k': 'move-up'
'l': 'move-right'
'w': 'move-to-next-word'
'b': 'move-to-previous-word'
'}': 'move-to-next-paragraph'
'esc': 'reset-command-mode'
'left': 'move-left'
'right': 'move-right'
@handleCommands
'insert': => @activateInsertMode()
@@ -52,6 +53,7 @@ class VimMode
'move-right': => new motions.MoveRight @editor
'move-to-next-word': => new motions.MoveToNextWord(@editor)
'move-to-previous-word': => new motions.MoveToPreviousWord(@editor)
'move-to-next-paragraph': => new motions.MoveToNextParagraph(@editor)
'numeric-prefix': (e) => @numericPrefix(e)
'reset-command-mode': => @resetCommandMode()

View File

@@ -1,3 +1,4 @@
Point = require 'point'
getWordRegex = -> /(\w+)|([^\w\s]+)/g
class Motion
@@ -61,4 +62,28 @@ class MoveToNextWord extends Motion
column = nextLineMatch?.index or 0
{ row, column }
module.exports = { Motion, MoveLeft, MoveRight, MoveUp, MoveDown, MoveToNextWord, MoveToPreviousWord }
class MoveToNextParagraph extends Motion
execute: ->
@editor.setCursorPosition(@nextPosition())
select: ->
@editor.selectToPosition(@nextPosition())
nextPosition: ->
regex = /[^\n]\n^$/gm
row = null
column = 0
startRow = @editor.getCursorRow() + 1
for r in [startRow..@editor.buffer.lastRow()]
if @editor.buffer.getLine(r).length == 0
row = r
break
if not row
row = @editor.buffer.lastRow()
column = @editor.buffer.lastLine().length - 1
new Point(row, column)
module.exports = { Motion, MoveLeft, MoveRight, MoveUp, MoveDown, MoveToNextWord, MoveToPreviousWord, MoveToNextParagraph }