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.
This commit is contained in:
Corey Johnson & Nathan Sobo 2012-03-28 17:34:20 -07:00
parent a4c25dc678
commit 88c4705f8a
7 changed files with 55 additions and 26 deletions

View File

@ -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 ->

View File

@ -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)

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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'
'meta-left': 'move-to-beginning-of-line'
'alt-left': 'move-to-beginning-of-word'
'alt-right': 'move-to-end-of-word'

View File

@ -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'