From 9a922b1c2d63bcc8a2abb9b809f2fc490c5a8003 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Fri, 10 Feb 2012 10:41:02 -0800 Subject: [PATCH] Implemented MoveToNextParagraph motion --- spec/atom/vim-mode-spec.coffee | 17 +++++++++++++++++ src/atom/vim-mode.coffee | 26 ++++++++++++++------------ src/atom/vim-mode/motions.coffee | 27 ++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/spec/atom/vim-mode-spec.coffee b/spec/atom/vim-mode-spec.coffee index d926ef776..4fe9ff7f9 100644 --- a/spec/atom/vim-mode-spec.coffee +++ b/spec/atom/vim-mode-spec.coffee @@ -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") diff --git a/src/atom/vim-mode.coffee b/src/atom/vim-mode.coffee index f0b44ecea..d5c258697 100644 --- a/src/atom/vim-mode.coffee +++ b/src/atom/vim-mode.coffee @@ -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() diff --git a/src/atom/vim-mode/motions.coffee b/src/atom/vim-mode/motions.coffee index 9a84058ea..7535e28ab 100644 --- a/src/atom/vim-mode/motions.coffee +++ b/src/atom/vim-mode/motions.coffee @@ -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 }