Add Editor::splitSelectionIntoLines

This commit is contained in:
Kevin Sawicki
2014-01-07 18:17:34 -08:00
parent deb4365d65
commit 0ef6757e65
3 changed files with 45 additions and 0 deletions

View File

@@ -1181,6 +1181,27 @@ describe "Editor", ->
[[10, 0], [10, 0]]
]
describe ".splitSelectionIntoLines()", ->
it "splits all multi-line selections into one selection per line", ->
editor.setSelectedBufferRange([[0, 3], [2, 4]])
editor.splitSelectionIntoLines()
expect(editor.getSelectedBufferRanges()).toEqual [
[[0, 3], [0, 29]]
[[1, 0], [1, 30]]
[[2, 0], [2, 4]]
]
editor.setSelectedBufferRange([[0, 3], [1, 10]])
editor.splitSelectionIntoLines()
expect(editor.getSelectedBufferRanges()).toEqual [
[[0, 3], [0, 29]]
[[1, 0], [1, 10]]
]
editor.setSelectedBufferRange([[0, 0], [0, 3]])
editor.splitSelectionIntoLines()
expect(editor.getSelectedBufferRanges()).toEqual [[[0, 0], [0, 3]]]
describe ".consolidateSelections()", ->
it "destroys all selections but the most recent, returning true if any selections were destroyed", ->
editor.setSelectedBufferRange([[3, 16], [3, 21]])

View File

@@ -183,6 +183,7 @@ class EditorView extends View
'editor:newline-above': @insertNewlineAbove
'editor:add-selection-below': @addSelectionBelow
'editor:add-selection-above': @addSelectionAbove
'editor:split-selection-into-lines': @splitSelectionIntoLines
'editor:toggle-soft-tabs': @toggleSoftTabs
'editor:toggle-soft-wrap': @toggleSoftWrap
'editor:fold-all': @foldAll
@@ -375,6 +376,9 @@ class EditorView extends View
# {Delegates to: Editor.addSelectionAbove}
addSelectionAbove: -> @editor.addSelectionAbove()
# {Delegates to: Editor.splitSelectionIntoLines}
splitSelectionIntoLines: -> @editor.splitSelectionIntoLines()
# {Delegates to: Editor.selectToBeginningOfWord}
selectToBeginningOfWord: -> @editor.selectToBeginningOfWord()

View File

@@ -1190,6 +1190,26 @@ class Editor extends Model
addSelectionAbove: ->
@expandSelectionsBackward (selection) => selection.addSelectionAbove()
# Public: Split any multi-line selections into one selection per line.
#
# This method breaks apart multi-line selections by adding new selections
# that select the entire line for all completely selected lines and leaves
# any partially selected lines at the start and end of the selection as-is.
#
# This method will not affect any single line selections.
splitSelectionIntoLines: ->
for selection in @getSelections()
range = selection.getBufferRange()
continue if range.isSingleLine()
selection.destroy()
{start, end} = range
@addSelectionForBufferRange([start, [start.row, Infinity]])
{row} = start
while ++row < end.row
@addSelectionForBufferRange([[row, 0], [row, Infinity]])
@addSelectionForBufferRange([[end.row, 0], [end.row, end.column]])
# Public: Transposes the current text selections.
#
# The text in each selection is reversed so `abcd` would become `dcba`. The