diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index 47f8ac23d..7c5961161 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -313,6 +313,21 @@ describe "EditSession", -> editSession.moveCursorToBeginningOfWord() expect(editSession.getCursorBufferPosition()).toEqual [9, 2] + fdescribe ".moveCursorToPreviousWordBoundry()", -> + it "moves the cursor to the previous word boundry", -> + editSession.setCursorBufferPosition [0, 8] + editSession.addCursorAtBufferPosition [2, 0] + editSession.addCursorAtBufferPosition [2, 4] + editSession.addCursorAtBufferPosition [3, 14] + [cursor1, cursor2, cursor3, cursor4] = editSession.getCursors() + + editSession.moveCursorToPreviousWordBoundry() + + expect(cursor1.getBufferPosition()).toEqual [0, 4] + expect(cursor2.getBufferPosition()).toEqual [1, 30] + expect(cursor3.getBufferPosition()).toEqual [2, 0] + expect(cursor4.getBufferPosition()).toEqual [3, 13] + describe ".moveCursorToEndOfWord()", -> it "moves the cursor to the end of the word", -> editSession.setCursorBufferPosition [0, 6] diff --git a/src/app/cursor.coffee b/src/app/cursor.coffee index 6db87b6e3..54e2921df 100644 --- a/src/app/cursor.coffee +++ b/src/app/cursor.coffee @@ -244,6 +244,11 @@ class Cursor if position = @getBeginningOfNextWordBufferPosition() @setBufferPosition(position) + # Moves the cursor to the previous word boundry. + moveToPreviousWordBoundry: -> + if position = @getMovePreviousWordBoundryBufferPosition() + @setBufferPosition(position) + # Retrieves the buffer position of where the current word starts. # # options - A hash with one option: @@ -265,6 +270,28 @@ class Cursor beginningOfWordPosition or currentBufferPosition + # Retrieves buffer position of previous word boiundry. It might be on the + # current word, or the previous word. + getMovePreviousWordBoundryBufferPosition: (options = {}) -> + currentBufferPosition = @getBufferPosition() + previousNonBlankRow = @editSession.buffer.previousNonBlankRow(currentBufferPosition.row) + scanRange = [[previousNonBlankRow, 0], currentBufferPosition] + + beginningOfWordPosition = null + @editSession.backwardsScanInBufferRange (options.wordRegex ? @wordRegExp()), scanRange, ({range, stop}) => + if range.start.row < currentBufferPosition.row and currentBufferPosition.column > 0 + # force it to stop at the beginning of each line + beginningOfWordPosition = new Point(currentBufferPosition.row, 0) + else if range.end.isLessThan(currentBufferPosition) + beginningOfWordPosition = range.end + else + beginningOfWordPosition = range.start + + if not beginningOfWordPosition?.isEqual(currentBufferPosition) + stop() + + beginningOfWordPosition or currentBufferPosition + # Retrieves the buffer position of where the current word ends. # # options - A hash with one option: diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index c38ba063d..dbb4f9d61 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -1101,6 +1101,9 @@ class EditSession moveCursorToBeginningOfNextWord: -> @moveCursors (cursor) -> cursor.moveToBeginningOfNextWord() + moveCursorToPreviousWordBoundry: -> + @moveCursors (cursor) -> cursor.moveToPreviousWordBoundry() + # Internal: moveCursors: (fn) -> fn(cursor) for cursor in @getCursors() diff --git a/src/app/editor.coffee b/src/app/editor.coffee index a6a1709c5..6e01f3c69 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -141,6 +141,7 @@ class Editor extends View 'editor:move-to-beginning-of-word': @moveCursorToBeginningOfWord 'editor:move-to-end-of-word': @moveCursorToEndOfWord 'editor:move-to-beginning-of-next-word': @moveCursorToBeginningOfNextWord + 'editor:move-to-previous-word-boundry': @moveCursorToPreviousWordBoundry 'editor:select-to-end-of-line': @selectToEndOfLine 'editor:select-to-beginning-of-line': @selectToBeginningOfLine 'editor:select-to-end-of-word': @selectToEndOfWord @@ -237,6 +238,9 @@ class Editor extends View # {Delegates to: EditSession.moveCursorToFirstCharacterOfLine} moveCursorToFirstCharacterOfLine: -> @activeEditSession.moveCursorToFirstCharacterOfLine() + # {Delegates to: EditSession.moveCursorToPreviousWordBoundry} + moveCursorToPreviousWordBoundry: -> @activeEditSession.moveCursorToPreviousWordBoundry() + # {Delegates to: EditSession.moveCursorToEndOfLine} moveCursorToEndOfLine: -> @activeEditSession.moveCursorToEndOfLine()