Editor supports move-to-next-word events

This commit is contained in:
Nathan Sobo
2012-03-28 13:50:52 -07:00
parent 777d4d9680
commit cce1218fda
8 changed files with 286 additions and 228 deletions

View File

@@ -158,9 +158,13 @@ class Buffer
startPosition = @positionForCharacterIndex(matchStartIndex + lengthDelta)
endPosition = @positionForCharacterIndex(matchEndIndex + lengthDelta)
range = new Range(startPosition, endPosition)
replacementText = iterator(match, range)
recurse = true
replacementText = null
stop = -> recurse = false
replace = (text) -> replacementText = text
iterator(match, range, { stop, replace })
if _.isString(replacementText)
if replacementText
@change(range, replacementText)
lengthDelta += replacementText.length - matchLength
@@ -168,7 +172,7 @@ class Buffer
matchStartIndex++
matchEndIndex++
if global
if global and recurse
traverseRecursively(text, matchEndIndex, endIndex, lengthDelta)
startIndex = @characterIndexForPosition(range.start)

View File

@@ -11,6 +11,6 @@ class Substitution extends Command
execute: (editor) ->
range = editor.getSelection().getBufferRange()
editor.buffer.traverseRegexMatchesInRange @regex, range, =>
@replacementText
editor.buffer.traverseRegexMatchesInRange @regex, range, (match, matchRange, { replace }) =>
replace(@replacementText)

View File

@@ -25,6 +25,10 @@ class CompositeCursor
cursor = @addCursor()
cursor.setScreenPosition(screenPosition)
addCursorAtBufferPosition: (bufferPosition) ->
cursor = @addCursor()
cursor.setBufferPosition(bufferPosition)
removeCursor: (cursor) ->
_.remove(@cursors, cursor)
@@ -53,6 +57,9 @@ class CompositeCursor
moveDown: ->
@modifyCursors (cursor) -> cursor.moveDown()
moveToNextWord: ->
@modifyCursors (cursor) -> cursor.moveToNextWord()
handleBufferChange: (e) ->
@modifyCursors (cursor) -> cursor.handleBufferChange(e)

View File

@@ -103,9 +103,22 @@ class Cursor extends View
@setScreenPosition({row: row + 1, column: column})
@goalColumn = column
moveToNextWord: ->
wordRegex = /(\w+)|([^\w\s]+)/g
bufferPosition = @getBufferPosition()
range = [bufferPosition, @editor.getEofPosition()]
nextPosition = null
@editor.traverseRegexMatchesInRange wordRegex, range, (match, matchRange, { stop }) =>
if matchRange.start.isGreaterThan(bufferPosition)
nextPosition = matchRange.start
stop()
@setBufferPosition(nextPosition or @editor.getEofPosition())
moveToLineEnd: ->
{ row } = @getScreenPosition()
@setScreenPosition({ row, column: @editor.buffer.lineForRow(row).length })
{ row } = @getBufferPosition()
@setBufferPosition({ row, column: @editor.buffer.lineForRow(row).length })
moveToLineStart: ->
{ row } = @getScreenPosition()

View File

@@ -82,6 +82,7 @@ class Editor extends View
@on 'move-left', => @moveCursorLeft()
@on 'move-down', => @moveCursorDown()
@on 'move-up', => @moveCursorUp()
@on 'move-to-next-word', => @moveCursorToNextWord()
@on 'select-right', => @selectRight()
@on 'select-left', => @selectLeft()
@on 'select-up', => @selectUp()
@@ -109,6 +110,9 @@ class Editor extends View
addCursorAtScreenPosition: (screenPosition) ->
@compositeCursor.addCursorAtScreenPosition(screenPosition)
addCursorAtBufferPosition: (bufferPosition) ->
@compositeCursor.addCursorAtBufferPosition(bufferPosition)
addSelectionForCursor: (cursor) ->
@compositeSelection.addSelectionForCursor(cursor)
@@ -347,11 +351,12 @@ class Editor extends View
@lineHeight = fragment.outerHeight()
fragment.remove()
getCursors: -> @compositeCursor.getCursors()
moveCursorUp: -> @compositeCursor.moveUp()
moveCursorDown: -> @compositeCursor.moveDown()
moveCursorRight: -> @compositeCursor.moveRight()
getCursors: -> @compositeCursor.getCursors()
moveCursorLeft: -> @compositeCursor.moveLeft()
moveCursorToNextWord: -> @compositeCursor.moveToNextWord()
setCursorScreenPosition: (position) -> @compositeCursor.setScreenPosition(position)
getCursorScreenPosition: -> @compositeCursor.getCursor().getScreenPosition()
setCursorBufferPosition: (position) -> @compositeCursor.setBufferPosition(position)
@@ -377,6 +382,7 @@ class Editor extends View
getTextInRange: (range) -> @buffer.getTextInRange(range)
getEofPosition: -> @buffer.getEofPosition()
lineForBufferRow: (row) -> @buffer.lineForRow(row)
traverseRegexMatchesInRange: (args...) -> @buffer.traverseRegexMatchesInRange(args...)
insertText: (text) ->
@compositeSelection.insertText(text)

View File

@@ -3,3 +3,4 @@ window.keymap.bindKeys '.editor',
'ctrl-b': 'move-left'
'ctrl-p': 'move-up'
'ctrl-n': 'move-down'
'alt-f': 'move-to-next-word'