From 91cc9cc9a41b5f09266da78c7e72c49b56acb120 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sat, 14 Jan 2012 00:10:34 -0800 Subject: [PATCH] Implement dw (delete to beginning of next word) The w motion has a .select() method. When d composes with a motion it calls select() on it and then deletes the selected text. --- spec/atom/vim-mode-spec.coffee | 30 ++++++++++++++++++++---------- src/atom/editor.coffee | 8 ++++++++ src/atom/vim-mode-operators.coffee | 23 +++++++++++++++++++---- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/spec/atom/vim-mode-spec.coffee b/spec/atom/vim-mode-spec.coffee index e47eed8ea..50d31d655 100644 --- a/spec/atom/vim-mode-spec.coffee +++ b/spec/atom/vim-mode-spec.coffee @@ -46,17 +46,27 @@ describe "VimMode", -> expect(editor.getCursor()).toEqual(column: 1, row: 0) describe "the d keybinding", -> - it "deletes the line the cursor is on when 'd' is pressed again", -> - editor.buffer.setText("12345\nabcde\nABCDE") - editor.setCursor(column: 1, row: 1) - spyOn(editor, 'deleteLine').andCallThrough() + describe "when followed by a d", -> + it "deletes the current line", -> + editor.buffer.setText("12345\nabcde\nABCDE") + editor.setCursor(column: 1, row: 1) - editor.trigger keydownEvent('d') - editor.trigger keydownEvent('d') + editor.trigger keydownEvent('d') + editor.trigger keydownEvent('d') - expect(editor.deleteLine).toHaveBeenCalled() - expect(editor.buffer.getText()).toBe "12345\nABCDE" - expect(editor.getCursor()).toEqual(column: 0, row: 1) + expect(editor.buffer.getText()).toBe "12345\nABCDE" + expect(editor.getCursor()).toEqual(column: 0, row: 1) + + describe "when followed by a w", -> + it "deletes to the beginning of the next word", -> + editor.buffer.setText("abcd efg") + editor.setCursor(column: 2, row: 0) + + editor.trigger keydownEvent('d') + editor.trigger keydownEvent('w') + + expect(editor.buffer.getText()).toBe "abefg" + expect(editor.getCursor()).toEqual {column: 2, row: 0} describe "basic motion bindings", -> beforeEach -> @@ -78,7 +88,7 @@ describe "VimMode", -> expect(editor.getCursor()).toEqual(column: 1, row: 0) describe "the w keybinding", -> - fit "moves the cursor to the beginning of the next word", -> + it "moves the cursor to the beginning of the next word", -> editor.buffer.setText("ab cde1+- \n xyz\n\nzip") editor.setCursor(column: 0, row: 0) diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 049c0162a..ab5a41226 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -58,6 +58,14 @@ class Editor extends Template setCursor: ({column, row}) -> @aceEditor.navigateTo(row, column) + selectToPosition: (position) -> + { row, column } = @getCursor() + @aceEditor.selection.setSelectionAnchor(row, column) + @aceEditor.moveCursorToPosition(position) + + delete: -> + @getAceSession().remove(@aceEditor.getSelectionRange()) + getCursor: -> @getAceSession().getSelection().getCursor() diff --git a/src/atom/vim-mode-operators.coffee b/src/atom/vim-mode-operators.coffee index 26f0f3e4e..81ecfa7eb 100644 --- a/src/atom/vim-mode-operators.coffee +++ b/src/atom/vim-mode-operators.coffee @@ -24,9 +24,18 @@ module.exports = Delete: class complete: null + motion: null execute: (editor) -> - editor.deleteLine() + if @motion + @motion.select(editor) + editor.delete() + else + editor.deleteLine() + + compose: (motion) -> + @motion = motion + @complete = true isComplete: -> @complete @@ -51,7 +60,15 @@ module.exports = isComplete: -> true MoveToNextWord: class + isComplete: -> true + execute: (editor) -> + editor.setCursor(@nextWordPosition(editor)) + + select: (editor) -> + editor.selectToPosition(@nextWordPosition(editor)) + + nextWordPosition: (editor) -> regex = getWordRegex() { row, column } = editor.getCursor() rightOfCursor = editor.getLineText(row).substring(column) @@ -65,8 +82,6 @@ module.exports = else nextLineMatch = regex.exec(editor.getLineText(++row)) column = nextLineMatch?.index or 0 + { row, column } - editor.setCursor { row, column } - - isComplete: -> true