mirror of
https://github.com/atom/atom.git
synced 2026-01-24 06:18:03 -05:00
Merge intersecting selections by row before deleting lines
This commit is contained in:
@@ -3376,6 +3376,21 @@ describe "TextEditor", ->
|
||||
expect(buffer.lineForRow(0)).toBe(line2)
|
||||
expect(buffer.getLineCount()).toBe(count - 2)
|
||||
|
||||
it "deletes only the first line when it has multiple selections", ->
|
||||
line1 = buffer.lineForRow(1)
|
||||
count = buffer.getLineCount()
|
||||
editor.getLastCursor().moveToTop()
|
||||
editor.setSelectedBufferRanges([
|
||||
[[0, 1], [0, 2]],
|
||||
[[0, 4], [0, 5]]
|
||||
])
|
||||
expect(buffer.lineForRow(0)).not.toBe(line1)
|
||||
|
||||
editor.deleteLine()
|
||||
|
||||
expect(buffer.lineForRow(0)).toBe(line1)
|
||||
expect(buffer.getLineCount()).toBe(count - 1)
|
||||
|
||||
it "only deletes first line if only newline is selected on second line", ->
|
||||
editor.setSelectedBufferRange([[0, 2], [1, 0]])
|
||||
line1 = buffer.lineForRow(1)
|
||||
|
||||
@@ -174,6 +174,13 @@ class Selection extends Model
|
||||
intersectsScreenRow: (screenRow) ->
|
||||
@getScreenRange().intersectsRow(screenRow)
|
||||
|
||||
intersectsByRowWith: (otherSelection) ->
|
||||
otherScreenRange = otherSelection.getScreenRange()
|
||||
|
||||
@getScreenRange().intersectsRowRange(
|
||||
otherScreenRange.start.row, otherScreenRange.end.row
|
||||
)
|
||||
|
||||
# Public: Identifies if a selection intersects with another selection.
|
||||
#
|
||||
# * `otherSelection` A {Selection} to check against.
|
||||
|
||||
@@ -1114,6 +1114,7 @@ class TextEditor extends Model
|
||||
|
||||
# Extended: Delete all lines intersecting selections.
|
||||
deleteLine: ->
|
||||
@mergeIntersectingSelectionsByRow()
|
||||
@mutateSelectedText (selection) -> selection.deleteLine()
|
||||
|
||||
# Deprecated: Use {::deleteToBeginningOfWord} instead.
|
||||
@@ -2232,6 +2233,17 @@ class TextEditor extends Model
|
||||
# the function with merging suppressed, then merges intersecting selections
|
||||
# afterward.
|
||||
mergeIntersectingSelections: (args...) ->
|
||||
@mergeSelections args..., (previousSelection, currentSelection) ->
|
||||
exclusive = not currentSelection.isEmpty() and not previousSelection.isEmpty()
|
||||
|
||||
previousSelection.intersectsWith(currentSelection, exclusive)
|
||||
|
||||
mergeIntersectingSelectionsByRow: (args...) ->
|
||||
@mergeSelections args..., (previousSelection, currentSelection) ->
|
||||
previousSelection.intersectsByRowWith(currentSelection)
|
||||
|
||||
mergeSelections: (args...) ->
|
||||
mergePredicate = args.pop()
|
||||
fn = args.pop() if _.isFunction(_.last(args))
|
||||
options = args.pop() ? {}
|
||||
|
||||
@@ -2244,10 +2256,7 @@ class TextEditor extends Model
|
||||
|
||||
reducer = (disjointSelections, selection) ->
|
||||
adjacentSelection = _.last(disjointSelections)
|
||||
exclusive = not selection.isEmpty() and not adjacentSelection.isEmpty()
|
||||
intersects = adjacentSelection.intersectsWith(selection, exclusive)
|
||||
|
||||
if intersects
|
||||
if mergePredicate(adjacentSelection, selection)
|
||||
adjacentSelection.merge(selection, options)
|
||||
disjointSelections
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user