Preserve original selection's range when adding selection's below

Just like the cursor tries to stay in its "goal column" when moving
vertically, here we try to keep the same selection even when adding
across shorter lines.
This commit is contained in:
Nathan Sobo
2013-04-04 17:33:23 -06:00
parent 26e53584c1
commit af923cca9b
3 changed files with 19 additions and 5 deletions

View File

@@ -720,6 +720,18 @@ describe "EditSession", ->
[[4, 25], [4, 29]]
]
it "honors the original selection's region when adding across shorter lines", ->
editSession.setSelectedBufferRange([[3, 22], [3, 38]])
editSession.addSelectionBelow()
editSession.addSelectionBelow()
editSession.addSelectionBelow()
expect(editSession.getSelectedBufferRanges()).toEqual [
[[3, 22], [3, 38]]
[[4, 22], [4, 29]]
[[5, 22], [5, 30]]
[[6, 22], [6, 38]]
]
describe "when the cursor is moved while there is a selection", ->
makeSelection = -> selection.setBufferRange [[1, 2], [1, 5]]

View File

@@ -585,7 +585,7 @@ class EditSession
unless options.preserveFolds
@destroyFoldsIntersectingBufferRange(@getMarkerBufferRange(marker))
cursor = @addCursor(marker)
selection = new Selection({editSession: this, marker, cursor})
selection = new Selection(_.extend({editSession: this, marker, cursor}, options))
@selections.push(selection)
selectionBufferRange = selection.getBufferRange()
@mergeIntersectingSelections()
@@ -600,7 +600,7 @@ class EditSession
addSelectionForBufferRange: (bufferRange, options={}) ->
options = _.defaults({invalidationStrategy: 'never'}, options)
marker = @markBufferRange(bufferRange, options)
@addSelection(marker)
@addSelection(marker, options)
setSelectedBufferRange: (bufferRange, options) ->
@setSelectedBufferRanges([bufferRange], options)

View File

@@ -5,10 +5,12 @@ _ = require 'underscore'
module.exports =
class Selection
wordwise: false
editSession: null
initialScreenRange: null
goalBufferRange: null
needsAutoscroll: null
constructor: ({@cursor, @marker, @editSession}) ->
constructor: ({@cursor, @marker, @editSession, @goalBufferRange}) ->
@cursor.selection = this
@editSession.observeMarker @marker, => @screenRangeChanged()
@cursor.on 'destroyed.selection', =>
@@ -149,10 +151,10 @@ class Selection
@modifySelection => @cursor.moveToEndOfWord()
addSelectionBelow: ->
range = @getBufferRange().copy()
range = (@goalBufferRange ? @getBufferRange()).copy()
range.start.row++
range.end.row++
@editSession.addSelectionForBufferRange(range)
@editSession.addSelectionForBufferRange(range, goalBufferRange: range)
insertText: (text, options={}) ->
oldBufferRange = @getBufferRange()