Move most remaining logical methods from selection view into model

This commit is contained in:
Nathan Sobo
2012-06-08 15:26:54 -06:00
parent 31bd38dfcc
commit 873c80eaf2
6 changed files with 259 additions and 224 deletions

View File

@@ -102,74 +102,10 @@ class CompositeSeleciton
indentSelectedRows: ->
@mutateSelectedText (selection) -> selection.indentSelectedRows()
backspace: ->
@mutateSelectedText (selection) -> selection.backspace()
backspaceToBeginningOfWord: ->
@mutateSelectedText (selection) -> selection.backspaceToBeginningOfWord()
delete: ->
@mutateSelectedText (selection) -> selection.delete()
deleteToEndOfWord: ->
@mutateSelectedText (selection) -> selection.deleteToEndOfWord()
toggleLineComments: ->
@mutateSelectedText (selection) -> selection.toggleLineComments()
selectToScreenPosition: (position) ->
@getLastSelection().selectToScreenPosition(position)
selectRight: ->
@expandSelectionsForward (selection) => selection.selectRight()
selectLeft: ->
@expandSelectionsBackward (selection) => selection.selectLeft()
selectUp: ->
@expandSelectionsBackward (selection) => selection.selectUp()
selectDown: ->
@expandSelectionsForward (selection) => selection.selectDown()
selectToTop: ->
@expandSelectionsBackward (selection) => selection.selectToTop()
selectAll: ->
@expandSelectionsForward (selection) => selection.selectAll()
selectToBottom: ->
@expandSelectionsForward (selection) => selection.selectToBottom()
selectToBeginningOfLine: ->
@expandSelectionsBackward (selection) => selection.selectToBeginningOfLine()
selectToEndOfLine: ->
@expandSelectionsForward (selection) => selection.selectToEndOfLine()
selectToBeginningOfWord: ->
@expandSelectionsBackward (selection) => selection.selectToBeginningOfWord()
selectToEndOfWord: ->
@expandSelectionsForward (selection) => selection.selectToEndOfWord()
cutToEndOfLine: ->
maintainPasteboard = false
@mutateSelectedText (selection) ->
selection.cutToEndOfLine(maintainPasteboard)
maintainPasteboard = true
cut: ->
maintainPasteboard = false
@mutateSelectedText (selection) ->
selection.cut(maintainPasteboard)
maintainPasteboard = true
copy: ->
maintainPasteboard = false
for selection in @getSelections()
selection.copy(maintainPasteboard)
maintainPasteboard = true
mergeIntersectingSelections: (options) ->
@editor.activeEditSession.mergeIntersectingSelections(options)

View File

@@ -102,9 +102,48 @@ class EditSession
insertText: (text) ->
@mutateSelectedText (selection) -> selection.insertText(text)
backspace: ->
@mutateSelectedText (selection) -> selection.backspace()
backspaceToBeginningOfWord: ->
@mutateSelectedText (selection) -> selection.backspaceToBeginningOfWord()
delete: ->
@mutateSelectedText (selection) -> selection.delete()
deleteToEndOfWord: ->
@mutateSelectedText (selection) -> selection.deleteToEndOfWord()
cutToEndOfLine: ->
maintainPasteboard = false
@mutateSelectedText (selection) ->
selection.cutToEndOfLine(maintainPasteboard)
maintainPasteboard = true
cut: ->
maintainPasteboard = false
@mutateSelectedText (selection) ->
selection.cut(maintainPasteboard)
maintainPasteboard = true
copy: ->
maintainPasteboard = false
for selection in @getSelections()
selection.copy(maintainPasteboard)
maintainPasteboard = true
foldSelection: ->
selection.fold() for selection in @getSelections()
createFold: (startRow, endRow) ->
@renderer.createFold(startRow, endRow)
destroyFoldsContainingBufferRow: (bufferRow) ->
@renderer.destroyFoldsContainingBufferRow(bufferRow)
toggleLineCommentsInRange: (range) ->
@renderer.toggleLineCommentsInRange(range)
mutateSelectedText: (fn) ->
selections = @getSelections()
@buffer.startUndoBatch(@getSelectedBufferRanges())
@@ -144,6 +183,9 @@ class EditSession
getLastCursor: ->
_.last(@cursors)
getLastSelection: ->
_.last(@selections)
setCursorScreenPosition: (position) ->
@moveCursors (cursor) -> cursor.setScreenPosition(position)
@@ -199,6 +241,42 @@ class EditSession
fn(cursor) for cursor in @getCursors()
@mergeCursors()
selectToScreenPosition: (position) ->
@getLastSelection().selectToScreenPosition(position)
selectRight: ->
@expandSelectionsForward (selection) => selection.selectRight()
selectLeft: ->
@expandSelectionsBackward (selection) => selection.selectLeft()
selectUp: ->
@expandSelectionsBackward (selection) => selection.selectUp()
selectDown: ->
@expandSelectionsForward (selection) => selection.selectDown()
selectToTop: ->
@expandSelectionsBackward (selection) => selection.selectToTop()
selectAll: ->
@expandSelectionsForward (selection) => selection.selectAll()
selectToBottom: ->
@expandSelectionsForward (selection) => selection.selectToBottom()
selectToBeginningOfLine: ->
@expandSelectionsBackward (selection) => selection.selectToBeginningOfLine()
selectToEndOfLine: ->
@expandSelectionsForward (selection) => selection.selectToEndOfLine()
selectToBeginningOfWord: ->
@expandSelectionsBackward (selection) => selection.selectToBeginningOfWord()
selectToEndOfWord: ->
@expandSelectionsForward (selection) => selection.selectToEndOfWord()
mergeCursors: ->
positions = []
for cursor in new Array(@getCursors()...)
@@ -208,6 +286,14 @@ class EditSession
else
positions.push(position)
expandSelectionsForward: (fn) ->
fn(selection) for selection in @getSelections()
@mergeIntersectingSelections()
expandSelectionsBackward: (fn) ->
fn(selection) for selection in @getSelections()
@mergeIntersectingSelections(reverse: true)
mergeIntersectingSelections: (options) ->
for selection in @getSelections()
otherSelections = @getSelections()

View File

@@ -191,14 +191,14 @@ class Editor extends View
@setCursorScreenPosition(screenPosition)
else if clickCount == 2
if e.shiftKey
@compositeSelection.getLastSelection().expandOverWord()
@activeEditSession.getLastSelection().expandOverWord()
else
@compositeSelection.getLastSelection().selectWord()
@activeEditSession.getLastSelection().selectWord()
else if clickCount >= 3
if e.shiftKey
@compositeSelection.getLastSelection().expandOverLine()
@activeEditSession.getLastSelection().expandOverLine()
else
@compositeSelection.getLastSelection().selectLine()
@activeEditSession.getLastSelection().selectLine()
@selectOnMousemoveUntilMouseup()
@@ -246,8 +246,8 @@ class Editor extends View
@on 'mousemove', moveHandler
$(document).one 'mouseup', =>
@off 'mousemove', moveHandler
reverse = @compositeSelection.getLastSelection().isReversed()
@compositeSelection.mergeIntersectingSelections({reverse})
reverse = @activeEditSession.getLastSelection().isReversed()
@activeEditSession.mergeIntersectingSelections({reverse})
@syncCursorAnimations()
prepareForScrolling: ->
@@ -456,7 +456,7 @@ class Editor extends View
Math.ceil((@scrollTop() + @scrollView.height()) / @lineHeight) - 1
handleBufferChange: (e) ->
@compositeSelection.handleBufferChange(e)
# @compositeSelection.handleBufferChange(e)
handleRendererChange: (e) ->
oldScreenRange = e.oldRange
@@ -676,24 +676,24 @@ class Editor extends View
setSelectionBufferRange: (bufferRange, options) -> @compositeSelection.setBufferRange(bufferRange, options)
setSelectedBufferRanges: (bufferRanges) -> @compositeSelection.setBufferRanges(bufferRanges)
addSelectionForBufferRange: (bufferRange, options) -> @compositeSelection.addSelectionForBufferRange(bufferRange, options)
selectRight: -> @compositeSelection.selectRight()
selectLeft: -> @compositeSelection.selectLeft()
selectUp: -> @compositeSelection.selectUp()
selectDown: -> @compositeSelection.selectDown()
selectToTop: -> @compositeSelection.selectToTop()
selectToBottom: -> @compositeSelection.selectToBottom()
selectAll: -> @compositeSelection.selectAll()
selectToBeginningOfLine: -> @compositeSelection.selectToBeginningOfLine()
selectToEndOfLine: -> @compositeSelection.selectToEndOfLine()
selectToBeginningOfWord: -> @compositeSelection.selectToBeginningOfWord()
selectToEndOfWord: -> @compositeSelection.selectToEndOfWord()
selectToScreenPosition: (position) -> @compositeSelection.selectToScreenPosition(position)
selectRight: -> @activeEditSession.selectRight()
selectLeft: -> @activeEditSession.selectLeft()
selectUp: -> @activeEditSession.selectUp()
selectDown: -> @activeEditSession.selectDown()
selectToTop: -> @activeEditSession.selectToTop()
selectToBottom: -> @activeEditSession.selectToBottom()
selectAll: -> @activeEditSession.selectAll()
selectToBeginningOfLine: -> @activeEditSession.selectToBeginningOfLine()
selectToEndOfLine: -> @activeEditSession.selectToEndOfLine()
selectToBeginningOfWord: -> @activeEditSession.selectToBeginningOfWord()
selectToEndOfWord: -> @activeEditSession.selectToEndOfWord()
selectToScreenPosition: (position) -> @activeEditSession.selectToScreenPosition(position)
clearSelections: -> @compositeSelection.clearSelections()
backspace: -> @compositeSelection.backspace()
backspaceToBeginningOfWord: -> @compositeSelection.backspaceToBeginningOfWord()
delete: -> @compositeSelection.delete()
deleteToEndOfWord: -> @compositeSelection.deleteToEndOfWord()
cutToEndOfLine: -> @compositeSelection.cutToEndOfLine()
backspace: -> @activeEditSession.backspace()
backspaceToBeginningOfWord: -> @activeEditSession.backspaceToBeginningOfWord()
delete: -> @activeEditSession.delete()
deleteToEndOfWord: -> @activeEditSession.deleteToEndOfWord()
cutToEndOfLine: -> @activeEditSession.cutToEndOfLine()
setText: (text) -> @buffer.setText(text)
getText: -> @buffer.getText()
@@ -728,8 +728,8 @@ class Editor extends View
indentSelectedRows: -> @compositeSelection.indentSelectedRows()
outdentSelectedRows: -> @compositeSelection.outdentSelectedRows()
cutSelection: -> @compositeSelection.cut()
copySelection: -> @compositeSelection.copy()
cutSelection: -> @activeEditSession.cut()
copySelection: -> @activeEditSession.copy()
paste: -> @insertText($native.readFromPasteboard())
undo: ->
@@ -835,7 +835,7 @@ class Editor extends View
row = @renderer.bufferPositionForScreenPosition(@getCursorScreenPosition()).row
@renderer.toggleFoldAtBufferRow(row)
foldSelection: -> @getSelection().fold()
foldSelection: -> @activeEditSession.foldSelection()
unfoldRow: (row) ->
@renderer.largestFoldForBufferRow(row)?.destroy()
@@ -846,9 +846,6 @@ class Editor extends View
toggleLineCommentsInSelection: ->
@compositeSelection.toggleLineComments()
toggleLineCommentsInRange: (range) ->
@renderer.toggleLineCommentsInRange(range)
logRenderedLines: ->
@renderedLines.find('.line').each (n) ->
console.log n, $(this).text()

View File

@@ -98,45 +98,6 @@ class SelectionView extends View
intersectsBufferRange: (bufferRange) ->
@getBufferRange().intersectsWith(bufferRange)
indentSelectedRows: ->
range = @getBufferRange()
for row in [range.start.row..range.end.row]
@editor.buffer.insert([row, 0], @editor.tabText) unless @editor.buffer.lineLengthForRow(row) == 0
outdentSelectedRows: ->
range = @getBufferRange()
buffer = @editor.buffer
leadingTabRegex = new RegExp("^#{@editor.tabText}")
for row in [range.start.row..range.end.row]
if leadingTabRegex.test buffer.lineForRow(row)
buffer.delete [[row, 0], [row, @editor.tabText.length]]
toggleLineComments: ->
@modifySelection =>
@editor.toggleLineCommentsInRange(@getBufferRange())
backspace: ->
@editor.destroyFoldsContainingBufferRow(@getBufferRange().end.row)
@selectLeft() if @isEmpty()
@deleteSelectedText()
backspaceToBeginningOfWord: ->
@selectToBeginningOfWord() if @isEmpty()
@deleteSelectedText()
delete: ->
@selectRight() if @isEmpty()
@deleteSelectedText()
deleteToEndOfWord: ->
@selectToEndOfWord() if @isEmpty()
@deleteSelectedText()
deleteSelectedText: ->
range = @getBufferRange()
@editor.buffer.delete(range) unless range.isEmpty()
@clearSelection()
remove: ->
@editor.compositeSelection.removeSelectionView(this)
@selection?.destroy()
@@ -145,69 +106,3 @@ class SelectionView extends View
modifySelection: (fn) ->
@selection.modifySelection(fn)
selectWord: ->
@setBufferRange(@cursor.getCurrentWordBufferRange())
expandOverWord: ->
@setBufferRange(@getBufferRange().union(@cursor.getCurrentWordBufferRange()))
selectLine: (row=@cursor.getBufferPosition().row) ->
@setBufferRange(@editor.rangeForBufferRow(row))
expandOverLine: ->
@setBufferRange(@getBufferRange().union(@cursor.getCurrentLineBufferRange()))
selectToScreenPosition: (position) ->
@modifySelection => @cursor.setScreenPosition(position)
selectRight: ->
@modifySelection => @cursor.moveRight()
selectLeft: ->
@modifySelection => @cursor.moveLeft()
selectUp: ->
@modifySelection => @cursor.moveUp()
selectDown: ->
@modifySelection => @cursor.moveDown()
selectToTop: ->
@modifySelection => @cursor.moveToTop()
selectToBottom: ->
@modifySelection => @cursor.moveToBottom()
selectAll: ->
@setBufferRange(@editor.buffer.getRange())
selectToBeginningOfLine: ->
@modifySelection => @cursor.moveToBeginningOfLine()
selectToEndOfLine: ->
@modifySelection => @cursor.moveToEndOfLine()
selectToBeginningOfWord: ->
@modifySelection => @cursor.moveToBeginningOfWord()
selectToEndOfWord: ->
@modifySelection => @cursor.moveToEndOfWord()
cutToEndOfLine: (maintainPasteboard) ->
@selectToEndOfLine() if @isEmpty()
@cut(maintainPasteboard)
cut: (maintainPasteboard=false) ->
@copy(maintainPasteboard)
@delete()
copy: (maintainPasteboard=false) ->
return if @isEmpty()
text = @editor.buffer.getTextInRange(@getBufferRange())
text = $native.readFromPasteboard() + "\n" + text if maintainPasteboard
$native.writeToPasteboard text
fold: ->
range = @getBufferRange()
@editor.createFold(range.start.row, range.end.row)
@cursor.setBufferPosition([range.end.row + 1, 0])

View File

@@ -23,13 +23,19 @@ class Selection
@editSession.removeSelection(this)
@trigger 'destroy'
isEmpty: ->
@getBufferRange().isEmpty()
isReversed: ->
not @isEmpty() and @cursor.getBufferPosition().isLessThan(@anchor.getBufferPosition())
getScreenRange: ->
if @anchor
new Range(@anchor.getScreenPosition(), @cursor.getScreenPosition())
else
new Range(@cursor.getScreenPosition(), @cursor.getScreenPosition())
setScreenRange: (screenRange, options={})->
setScreenRange: (screenRange, options={}) ->
screenRange = Range.fromObject(screenRange)
{ start, end } = screenRange
[start, end] = [end, start] if options.reverse
@@ -40,20 +46,76 @@ class Selection
@anchor.setScreenPosition(start)
@cursor.setScreenPosition(end)
setBufferRange: (bufferRange, options={}) ->
bufferRange = Range.fromObject(bufferRange)
{ start, end } = bufferRange
[start, end] = [end, start] if options.reverse
@modifyScreenRange =>
@placeAnchor() unless @anchor
@modifySelection =>
@anchor.setBufferPosition(start)
@cursor.setBufferPosition(end)
getBufferRange: ->
if @anchor
new Range(@anchor.getBufferPosition(), @cursor.getBufferPosition())
else
new Range(@cursor.getBufferPosition(), @cursor.getBufferPosition())
getText: ->
@editSession.buffer.getTextInRange(@getBufferRange())
clear: ->
@modifyScreenRange => @anchor = null
isEmpty: ->
@getBufferRange().isEmpty()
selectWord: ->
@setBufferRange(@cursor.getCurrentWordBufferRange())
isReversed: ->
not @isEmpty() and @cursor.getBufferPosition().isLessThan(@anchor.getBufferPosition())
expandOverWord: ->
@setBufferRange(@getBufferRange().union(@cursor.getCurrentWordBufferRange()))
selectLine: (row=@cursor.getBufferPosition().row) ->
@setBufferRange(@editSession.bufferRangeForBufferRow(row))
expandOverLine: ->
@setBufferRange(@getBufferRange().union(@cursor.getCurrentLineBufferRange()))
selectToScreenPosition: (position) ->
@modifySelection => @cursor.setScreenPosition(position)
selectRight: ->
@modifySelection => @cursor.moveRight()
selectLeft: ->
@modifySelection => @cursor.moveLeft()
selectUp: ->
@modifySelection => @cursor.moveUp()
selectDown: ->
@modifySelection => @cursor.moveDown()
selectToTop: ->
@modifySelection => @cursor.moveToTop()
selectToBottom: ->
@modifySelection => @cursor.moveToBottom()
selectAll: ->
@setBufferRange(@editSession.buffer.getRange())
selectToBeginningOfLine: ->
@modifySelection => @cursor.moveToBeginningOfLine()
selectToEndOfLine: ->
@modifySelection => @cursor.moveToEndOfLine()
selectToBeginningOfWord: ->
@modifySelection => @cursor.moveToBeginningOfWord()
selectToEndOfWord: ->
@modifySelection => @cursor.moveToEndOfWord()
insertText: (text) ->
{ text, shouldOutdent } = @autoIndentText(text)
@@ -65,6 +127,64 @@ class Selection
@cursor.setBufferPosition(newBufferRange.end, skipAtomicTokens: true) if wasReversed
@autoOutdentText() if shouldOutdent
backspace: ->
@editSession.destroyFoldsContainingBufferRow(@getBufferRange().end.row)
@selectLeft() if @isEmpty()
@deleteSelectedText()
backspaceToBeginningOfWord: ->
@selectToBeginningOfWord() if @isEmpty()
@deleteSelectedText()
delete: ->
@selectRight() if @isEmpty()
@deleteSelectedText()
deleteToEndOfWord: ->
@selectToEndOfWord() if @isEmpty()
@deleteSelectedText()
deleteSelectedText: ->
bufferRange = @getBufferRange()
@editSession.buffer.delete(bufferRange) unless bufferRange.isEmpty()
@clear() if @cursor
indentSelectedRows: ->
range = @getBufferRange()
for row in [range.start.row..range.end.row]
@editSession.buffer.insert([row, 0], @editSession.tabText) unless @editSession.buffer.lineLengthForRow(row) == 0
outdentSelectedRows: ->
range = @getBufferRange()
buffer = @editSession.buffer
leadingTabRegex = new RegExp("^#{@editSession.tabText}")
for row in [range.start.row..range.end.row]
if leadingTabRegex.test buffer.lineForRow(row)
buffer.delete [[row, 0], [row, @editSession.tabText.length]]
toggleLineComments: ->
@modifySelection =>
@editSession.toggleLineCommentsInRange(@getBufferRange())
cutToEndOfLine: (maintainPasteboard) ->
@selectToEndOfLine() if @isEmpty()
@cut(maintainPasteboard)
cut: (maintainPasteboard=false) ->
@copy(maintainPasteboard)
@delete()
copy: (maintainPasteboard=false) ->
return if @isEmpty()
text = @editSession.buffer.getTextInRange(@getBufferRange())
text = $native.readFromPasteboard() + "\n" + text if maintainPasteboard
$native.writeToPasteboard text
fold: ->
range = @getBufferRange()
@editSession.createFold(range.start.row, range.end.row)
@cursor.setBufferPosition([range.end.row + 1, 0])
autoIndentText: (text) ->
if @editSession.autoIndentEnabled()
mode = @editSession.getCurrentMode()