Add editor:add-selection-above command

This commit is contained in:
Nathan Sobo
2013-04-05 14:11:08 -06:00
parent 3e07351552
commit abc5ed5190
5 changed files with 87 additions and 0 deletions

View File

@@ -792,6 +792,70 @@ describe "EditSession", ->
[[10, 0], [10, 0]]
]
describe ".addSelectionAbove()", ->
describe "when the selection is non-empty", ->
it "selects the same region of the line above current selections if possible", ->
editSession.setSelectedBufferRange([[3, 16], [3, 21]])
editSession.addSelectionForBufferRange([[3, 37], [3, 44]])
editSession.addSelectionAbove()
expect(editSession.getSelectedBufferRanges()).toEqual [
[[2, 16], [2, 21]]
[[2, 37], [2, 40]]
[[3, 16], [3, 21]]
[[3, 37], [3, 44]]
]
for cursor in editSession.getCursors()
expect(cursor.isVisible()).toBeFalsy()
it "skips lines that are too short to create a non-empty selection", ->
editSession.setSelectedBufferRange([[6, 31], [6, 38]])
editSession.addSelectionAbove()
expect(editSession.getSelectedBufferRanges()).toEqual [
[[3, 31], [3, 38]]
[[6, 31], [6, 38]]
]
it "honors the original selection's range (goal range) when adding across shorter lines", ->
editSession.setSelectedBufferRange([[6, 22], [6, 38]])
editSession.addSelectionAbove()
editSession.addSelectionAbove()
editSession.addSelectionAbove()
expect(editSession.getSelectedBufferRanges()).toEqual [
[[3, 22], [3, 38]]
[[4, 22], [4, 29]]
[[5, 22], [5, 30]]
[[6, 22], [6, 38]]
]
describe "when the selection is empty", ->
it "does not skip lines that are shorter than the current column", ->
editSession.setCursorBufferPosition([6, 36])
editSession.addSelectionAbove()
editSession.addSelectionAbove()
editSession.addSelectionAbove()
expect(editSession.getSelectedBufferRanges()).toEqual [
[[3, 36], [3, 36]]
[[4, 29], [4, 29]]
[[5, 30], [5, 30]]
[[6, 36], [6, 36]]
]
it "skips empty lines when the column is non-zero", ->
editSession.setCursorBufferPosition([11, 4])
editSession.addSelectionAbove()
expect(editSession.getSelectedBufferRanges()).toEqual [
[[9, 4], [9, 4]]
[[11, 4], [11, 4]]
]
it "does not skip empty lines when the column is zero", ->
editSession.setCursorBufferPosition([10, 0])
editSession.addSelectionAbove()
expect(editSession.getSelectedBufferRanges()).toEqual [
[[9, 0], [9, 0]]
[[10, 0], [10, 0]]
]
describe ".consolidateSelections()", ->
it "destroys all selections but the most recent, returning true if any selections were destroyed", ->
editSession.setSelectedBufferRange([[3, 16], [3, 21]])

View File

@@ -770,6 +770,9 @@ class EditSession
addSelectionBelow: ->
@expandSelectionsForward (selection) => selection.addSelectionBelow()
addSelectionAbove: ->
@expandSelectionsBackward (selection) => selection.addSelectionAbove()
transpose: ->
@mutateSelectedText (selection) =>
if selection.isEmpty()

View File

@@ -124,6 +124,7 @@ class Editor extends View
'editor:select-to-end-of-word': @selectToEndOfWord
'editor:select-to-beginning-of-word': @selectToBeginningOfWord
'editor:add-selection-below': @addSelectionBelow
'editor:add-selection-above': @addSelectionAbove
'editor:select-line': @selectLine
'editor:transpose': @transpose
'editor:upper-case': @upperCase
@@ -214,6 +215,7 @@ class Editor extends View
selectToBeginningOfLine: -> @activeEditSession.selectToBeginningOfLine()
selectToEndOfLine: -> @activeEditSession.selectToEndOfLine()
addSelectionBelow: -> @activeEditSession.addSelectionBelow()
addSelectionAbove: -> @activeEditSession.addSelectionAbove()
selectToBeginningOfWord: -> @activeEditSession.selectToBeginningOfWord()
selectToEndOfWord: -> @activeEditSession.selectToEndOfWord()
selectWord: -> @activeEditSession.selectWord()

View File

@@ -10,6 +10,7 @@
'ctrl-{': 'editor:fold-all'
'ctrl-}': 'editor:unfold-all'
'ctrl-shift-down': 'editor:add-selection-below'
'ctrl-shift-up': 'editor:add-selection-above'
'alt-meta-ctrl-f': 'editor:fold-selection'
'shift-tab': 'editor:outdent-selected-rows'
'meta-[': 'editor:outdent-selected-rows'

View File

@@ -169,6 +169,23 @@ class Selection
@editSession.addSelectionForBufferRange(range, goalBufferRange: range, suppressMerge: true)
break
addSelectionAbove: ->
range = (@goalBufferRange ? @getBufferRange()).copy()
previousRow = range.end.row - 1
for row in [previousRow..0]
range.start.row = row
range.end.row = row
clippedRange = @editSession.clipBufferRange(range)
if range.isEmpty()
continue if range.end.column > 0 and clippedRange.end.column is 0
else
continue if clippedRange.isEmpty()
@editSession.addSelectionForBufferRange(range, goalBufferRange: range, suppressMerge: true)
break
insertText: (text, options={}) ->
oldBufferRange = @getBufferRange()
@editSession.destroyFoldsContainingBufferRow(oldBufferRange.end.row)