From 88c4705f8a9e2f89094586c0f7eb6842d28b67ae Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Wed, 28 Mar 2012 17:34:20 -0700 Subject: [PATCH] Add move-to-end-of-word and move-to-beginning-of-word. Buffer.traverseRegexMatchesInRange matches text that at end of range, even if the match could have exceeded end of the range. --- spec/atom/editor-spec.coffee | 35 ++++++++++++++++++++----------- src/atom/buffer.coffee | 10 +++++++-- src/atom/composite-cursor.coffee | 7 +++++-- src/atom/cursor.coffee | 16 ++++++++------ src/atom/editor.coffee | 5 ++++- src/atom/keybindings/apple.coffee | 4 +++- src/atom/keybindings/emacs.coffee | 4 ++-- 7 files changed, 55 insertions(+), 26 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 60ea57727..808a7bbe4 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -444,6 +444,7 @@ describe "Editor", -> editor.moveCursorRight() expect(editor.getCursorScreenPosition()).toEqual(lastPosition) + describe "move-to-top ", -> it "moves cusor to the top of the buffer", -> editor.setCursorScreenPosition [11,1] @@ -508,21 +509,31 @@ describe "Editor", -> editor.trigger 'move-to-next-word' expect(cursor1.getBufferPosition()).toEqual [12, 5] - describe "move-to-previous-word", -> - it "moves the cursor to the previous word or the beginning of the file if there is no previous word", -> - editor.setCursorBufferPosition [2, 5] - editor.addCursorAtBufferPosition [3, 60] - [cursor1, cursor2] = editor.getCursors() + describe "move-to-beginning-of-word", -> + it "moves the cursor to the beginning of the word", -> + editor.setCursorBufferPosition [0, 8] + editor.addCursorAtBufferPosition [1, 12] + editor.addCursorAtBufferPosition [3, 0] + [cursor1, cursor2, cursor3] = editor.getCursors() - editor.trigger 'move-to-previous-word' + editor.trigger 'move-to-beginning-of-word' - expect(cursor1.getBufferPosition()).toEqual [1, 29] - expect(cursor2.getBufferPosition()).toEqual [3, 57] + expect(cursor1.getBufferPosition()).toEqual [0, 4] + expect(cursor2.getBufferPosition()).toEqual [1, 11] + expect(cursor3.getBufferPosition()).toEqual [2, 39] - buffer.insert([0, 0], ' ') - cursor1.setBufferPosition([0, 3]) - editor.trigger 'move-to-previous-word' - expect(cursor1.getBufferPosition()).toEqual [0, 0] + describe "move-to-end-of-word", -> + it "moves the cursor to the end of the word", -> + editor.setCursorBufferPosition [0, 6] + editor.addCursorAtBufferPosition [1, 10] + editor.addCursorAtBufferPosition [2, 40] + [cursor1, cursor2, cursor3] = editor.getCursors() + + editor.trigger 'move-to-end-of-word' + + expect(cursor1.getBufferPosition()).toEqual [0, 13] + expect(cursor2.getBufferPosition()).toEqual [1, 12] + expect(cursor3.getBufferPosition()).toEqual [3, 7] describe ".setCursorScreenPosition({row, column})", -> beforeEach -> diff --git a/src/atom/buffer.coffee b/src/atom/buffer.coffee index 48a6628ad..a4c619662 100644 --- a/src/atom/buffer.coffee +++ b/src/atom/buffer.coffee @@ -154,9 +154,15 @@ class Buffer matchLength = match[0].length matchStartIndex = match.index - matchEndIndex = match.index + matchLength + matchEndIndex = matchStartIndex + matchLength - return if matchEndIndex > endIndex + if matchEndIndex > endIndex + regex.lastIndex = 0 + if matchStartIndex < endIndex and match = regex.exec(text[matchStartIndex..endIndex]) + matchLength = match[0].length + matchEndIndex = matchStartIndex + matchLength + else + return startPosition = @positionForCharacterIndex(matchStartIndex + lengthDelta) endPosition = @positionForCharacterIndex(matchEndIndex + lengthDelta) diff --git a/src/atom/composite-cursor.coffee b/src/atom/composite-cursor.coffee index 9469cb9b4..4b0b5fdaa 100644 --- a/src/atom/composite-cursor.coffee +++ b/src/atom/composite-cursor.coffee @@ -60,8 +60,11 @@ class CompositeCursor moveToNextWord: -> @modifyCursors (cursor) -> cursor.moveToNextWord() - moveToPreviousWord: -> - @modifyCursors (cursor) -> cursor.moveToPreviousWord() + moveToBeginningOfWord: -> + @modifyCursors (cursor) -> cursor.moveToBeginningOfWord() + + moveToEndOfWord: -> + @modifyCursors (cursor) -> cursor.moveToEndOfWord() moveToTop: -> @modifyCursors (cursor) -> cursor.moveToTop() diff --git a/src/atom/cursor.coffee b/src/atom/cursor.coffee index 5d96793bf..62942cacf 100644 --- a/src/atom/cursor.coffee +++ b/src/atom/cursor.coffee @@ -117,16 +117,20 @@ class Cursor extends View @setBufferPosition(nextPosition or @editor.getEofPosition()) - moveToPreviousWord: -> + moveToBeginningOfWord: -> bufferPosition = @getBufferPosition() - range = [[0, 0], bufferPosition] - - nextPosition = null + range = [[0,0], bufferPosition] @editor.backwardsTraverseRegexMatchesInRange @wordRegex, range, (match, matchRange, { stop }) => - nextPosition = matchRange.start + @setBufferPosition matchRange.start stop() - @setBufferPosition(nextPosition or [0, 0]) + moveToEndOfWord: -> + bufferPosition = @getBufferPosition() + range = [bufferPosition, @editor.getEofPosition()] + + @editor.traverseRegexMatchesInRange @wordRegex, range, (match, matchRange, { stop }) => + @setBufferPosition matchRange.end + stop() moveToEndOfLine: -> { row } = @getBufferPosition() diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 9653fa4bf..03772c424 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -110,6 +110,8 @@ class Editor extends View @on 'move-to-beginning-of-line', => @moveCursorToBeginningOfLine() @on 'move-to-end-of-line', => @moveCursorToEndOfLine() @on 'move-to-first-character-of-line', => @moveCursorToFirstCharacterOfLine() + @on 'move-to-beginning-of-word', => @moveCursorToBeginningOfWord() + @on 'move-to-end-of-word', => @moveCursorToEndOfWord() @on 'select-to-top', => @selectToTop() @on 'select-to-bottom', => @selectToBottom() @on 'select-to-end-of-line', => @selectToEndOfLine() @@ -369,7 +371,8 @@ class Editor extends View moveCursorRight: -> @compositeCursor.moveRight() moveCursorLeft: -> @compositeCursor.moveLeft() moveCursorToNextWord: -> @compositeCursor.moveToNextWord() - moveCursorToPreviousWord: -> @compositeCursor.moveToPreviousWord() + moveCursorToBeginningOfWord: -> @compositeCursor.moveToBeginningOfWord() + moveCursorToEndOfWord: -> @compositeCursor.moveToEndOfWord() moveCursorToTop: -> @compositeCursor.moveToTop() moveCursorToBottom: -> @compositeCursor.moveToBottom() moveCursorToBeginningOfLine: -> @compositeCursor.moveToBeginningOfLine() diff --git a/src/atom/keybindings/apple.coffee b/src/atom/keybindings/apple.coffee index c74b72c97..64b7ef0c1 100644 --- a/src/atom/keybindings/apple.coffee +++ b/src/atom/keybindings/apple.coffee @@ -4,4 +4,6 @@ window.keymap.bindKeys '.editor' 'meta-down': 'move-to-bottom' 'meta-shift-down': 'select-to-bottom' 'meta-right': 'move-to-end-of-line' - 'meta-left': 'move-to-beginning-of-line' \ No newline at end of file + 'meta-left': 'move-to-beginning-of-line' + 'alt-left': 'move-to-beginning-of-word' + 'alt-right': 'move-to-end-of-word' \ No newline at end of file diff --git a/src/atom/keybindings/emacs.coffee b/src/atom/keybindings/emacs.coffee index e1705f4c8..b95ac5a28 100644 --- a/src/atom/keybindings/emacs.coffee +++ b/src/atom/keybindings/emacs.coffee @@ -3,8 +3,8 @@ window.keymap.bindKeys '.editor', 'ctrl-b': 'move-left' 'ctrl-p': 'move-up' 'ctrl-n': 'move-down' - 'alt-f': 'move-to-next-word' - 'alt-b': 'move-to-previous-word' + 'alt-f': 'move-to-end-of-word' + 'alt-b': 'move-to-beginning-of-word' 'ctrl-a': 'move-to-first-character-of-line' 'ctrl-e': 'move-to-end-of-line' 'ctrl-h': 'backspace'