diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index 51226a22b..f2945977a 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -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) diff --git a/src/selection.coffee b/src/selection.coffee index ea4b54034..971ddbdd9 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -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. diff --git a/src/text-editor.coffee b/src/text-editor.coffee index 548725cf6..23a612ee4 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -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