diff --git a/src/app/cursor-view.coffee b/src/app/cursor-view.coffee index d1b009773..60030dc07 100644 --- a/src/app/cursor-view.coffee +++ b/src/app/cursor-view.coffee @@ -128,24 +128,6 @@ class CursorView extends View getCurrentLineBufferRange: -> @editor.rangeForBufferRow(@getBufferPosition().row) - moveToEndOfLine: -> - { row } = @getBufferPosition() - @setBufferPosition({ row, column: @editor.buffer.lineForRow(row).length }) - - moveToBeginningOfLine: -> - { row } = @getScreenPosition() - @setScreenPosition({ row, column: 0 }) - - moveToFirstCharacterOfLine: -> - position = @getBufferPosition() - range = @editor.rangeForBufferRow(position.row) - newPosition = null - @editor.scanInRange /^\s*/, range, (match, matchRange) => - newPosition = matchRange.end - return unless newPosition - newPosition = [position.row, 0] if newPosition.isEqual(position) - @setBufferPosition(newPosition) - updateAppearance: -> screenPosition = @getScreenPosition() pixelPosition = @editor.pixelPositionForScreenPosition(screenPosition) diff --git a/src/app/cursor.coffee b/src/app/cursor.coffee index 9cce5fd32..3614ba83b 100644 --- a/src/app/cursor.coffee +++ b/src/app/cursor.coffee @@ -14,6 +14,10 @@ class Cursor @setScreenPosition(screenPosition) if screenPosition @setBufferPosition(bufferPosition) if bufferPosition + destroy: -> + @editSession.removeCursor(this) + @trigger 'destroy' + setScreenPosition: (screenPosition, options) -> @anchor.setScreenPosition(screenPosition, options) @goalColumn = null @@ -30,6 +34,9 @@ class Cursor getBufferPosition: -> @anchor.getBufferPosition() + getBufferRow: -> + @getBufferPosition().row + handleBufferChange: (e) -> @anchor.handleBufferChange(e) @trigger 'change-screen-position', @getScreenPosition(), bufferChange: true @@ -61,8 +68,20 @@ class Cursor moveToBottom: -> @setBufferPosition(@editSession.getEofBufferPosition()) - destroy: -> - @editSession.removeCursor(this) - @trigger 'destroy' + moveToBeginningOfLine: -> + @setBufferPosition([@getBufferRow(), 0]) + + moveToFirstCharacterOfLine: -> + position = @getBufferPosition() + range = @editSession.bufferRangeForBufferRow(position.row) + newPosition = null + @editSession.scanInRange /^\s*/, range, (match, matchRange) => + newPosition = matchRange.end + return unless newPosition + newPosition = [position.row, 0] if newPosition.isEqual(position) + @setBufferPosition(newPosition) + + moveToEndOfLine: -> + @setBufferPosition([@getBufferRow(), Infinity], clip: true) _.extend Cursor.prototype, EventEmitter diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index 57f6e2c99..2eee9e4f4 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -51,9 +51,18 @@ class EditSession clipScreenPosition: (screenPosition, options) -> @renderer.clipScreenPosition(screenPosition, options) + clipBufferPosition: (bufferPosition, options) -> + @renderer.clipBufferPosition(bufferPosition, options) + getEofBufferPosition: -> @buffer.getEofPosition() + bufferRangeForBufferRow: (row) -> + @buffer.rangeForRow(row) + + scanInRange: (args...) -> + @buffer.scanInRange(args...) + getCursors: -> @cursors addCursorAtScreenPosition: (screenPosition) -> @@ -103,6 +112,15 @@ class EditSession moveCursorToBottom: -> @moveCursors (cursor) -> cursor.moveToBottom() + moveCursorToBeginningOfLine: -> + @moveCursors (cursor) -> cursor.moveToBeginningOfLine() + + moveCursorToFirstCharacterOfLine: -> + @moveCursors (cursor) -> cursor.moveToFirstCharacterOfLine() + + moveCursorToEndOfLine: -> + @moveCursors (cursor) -> cursor.moveToEndOfLine() + moveCursors: (fn) -> fn(cursor) for cursor in @getCursors() @mergeCursors() diff --git a/src/app/editor.coffee b/src/app/editor.coffee index 049d68b19..a461811fc 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -649,9 +649,9 @@ class Editor extends View moveCursorToEndOfWord: -> @compositeCursor.moveToEndOfWord() moveCursorToTop: -> @activeEditSession.moveCursorToTop() moveCursorToBottom: -> @activeEditSession.moveCursorToBottom() - moveCursorToBeginningOfLine: -> @compositeCursor.moveToBeginningOfLine() - moveCursorToFirstCharacterOfLine: -> @compositeCursor.moveToFirstCharacterOfLine() - moveCursorToEndOfLine: -> @compositeCursor.moveToEndOfLine() + moveCursorToBeginningOfLine: -> @activeEditSession.moveCursorToBeginningOfLine() + moveCursorToFirstCharacterOfLine: -> @activeEditSession.moveCursorToFirstCharacterOfLine() + moveCursorToEndOfLine: -> @activeEditSession.moveCursorToEndOfLine() setCursorScreenPosition: (position) -> @activeEditSession.setCursorScreenPosition(position) getCursorScreenPosition: -> @activeEditSession.getCursorScreenPosition() setCursorBufferPosition: (position) -> @activeEditSession.setCursorBufferPosition(position) diff --git a/src/app/line-map.coffee b/src/app/line-map.coffee index a06c1254b..cd5498453 100644 --- a/src/app/line-map.coffee +++ b/src/app/line-map.coffee @@ -81,6 +81,9 @@ class LineMap clipScreenPosition: (screenPosition, options) -> @clipPosition('screenDelta', screenPosition, options) + clipBufferPosition: (bufferPosition, options) -> + @clipPosition('bufferDelta', bufferPosition, options) + clipPosition: (deltaType, position, options={}) -> options.clipToBounds = true @translatePosition(deltaType, deltaType, position, options) diff --git a/src/app/new-anchor.coffee b/src/app/new-anchor.coffee index 710fd8f64..aa3d93412 100644 --- a/src/app/new-anchor.coffee +++ b/src/app/new-anchor.coffee @@ -26,8 +26,12 @@ class Anchor getBufferPosition: -> @bufferPosition - setBufferPosition: (position, options) -> - @bufferPosition = Point.fromObject(position) + setBufferPosition: (position, options={}) -> + if options.clip + @bufferPosition = @editSession.clipBufferPosition(position) + else + @bufferPosition = Point.fromObject(position) + screenPosition = @editSession.screenPositionForBufferPosition(@bufferPosition, options) @setScreenPosition(screenPosition, clip: false, assignBufferPosition: false) diff --git a/src/app/renderer.coffee b/src/app/renderer.coffee index 57fa12de0..2802ad504 100644 --- a/src/app/renderer.coffee +++ b/src/app/renderer.coffee @@ -159,9 +159,12 @@ class Renderer bufferPositionForScreenPosition: (position, options) -> @lineMap.bufferPositionForScreenPosition(position, options) - clipScreenPosition: (position, options={}) -> + clipScreenPosition: (position, options) -> @lineMap.clipScreenPosition(position, options) + clipBufferPosition: (position, options) -> + @lineMap.clipBufferPosition(position, options) + handleBufferChange: (e) -> allFolds = [] # Folds can modify @activeFolds, so first make sure we have a stable array of folds allFolds.push(folds...) for row, folds of @activeFolds diff --git a/src/app/selection.coffee b/src/app/selection.coffee index 4e3a48bc7..17074ba82 100644 --- a/src/app/selection.coffee +++ b/src/app/selection.coffee @@ -223,10 +223,10 @@ class Selection extends View @setBufferRange(@editor.buffer.getRange()) selectToBeginningOfLine: -> - @modifySelection => @cursor.moveToBeginningOfLine() + @modifySelection => @cursor.cursor.moveToBeginningOfLine() selectToEndOfLine: -> - @modifySelection => @cursor.moveToEndOfLine() + @modifySelection => @cursor.cursor.moveToEndOfLine() selectToBeginningOfWord: -> @modifySelection => @cursor.moveToBeginningOfWord()