Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #15993 from atom/dont-destroy-contained-folds
Browse files Browse the repository at this point in the history
Preserve folds that are fully contained by the selection when changing selection ranges
  • Loading branch information
Nathan Sobo committed Oct 25, 2017
2 parents 1722273 + d715f3e commit 6e55b29
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -70,7 +70,7 @@
"service-hub": "^0.7.4",
"sinon": "1.17.4",
"temp": "^0.8.3",
"text-buffer": "13.5.8",
"text-buffer": "13.6.0",
"typescript-simple": "1.0.0",
"underscore-plus": "^1.6.6",
"winreg": "^1.2.1",
Expand Down
5 changes: 4 additions & 1 deletion spec/text-editor-spec.coffee
Expand Up @@ -1871,7 +1871,7 @@ describe "TextEditor", ->
expect(selection1.getBufferRange()).toEqual [[2, 2], [3, 3]]

describe "when the 'preserveFolds' option is false (the default)", ->
it "removes folds that contain the selections", ->
it "removes folds that contain one or both of the selection's end points", ->
editor.setSelectedBufferRange([[0, 0], [0, 0]])
editor.foldBufferRowRange(1, 4)
editor.foldBufferRowRange(2, 3)
Expand All @@ -1884,6 +1884,9 @@ describe "TextEditor", ->
expect(editor.isFoldedAtScreenRow(6)).toBeFalsy()
expect(editor.isFoldedAtScreenRow(10)).toBeTruthy()

editor.setSelectedBufferRange([[10, 0], [12, 0]])
expect(editor.isFoldedAtScreenRow(10)).toBeTruthy()

describe "when the 'preserveFolds' option is true", ->
it "does not remove folds that contain the selections", ->
editor.setSelectedBufferRange([[0, 0], [0, 0]])
Expand Down
2 changes: 1 addition & 1 deletion src/selection.coffee
Expand Up @@ -87,7 +87,7 @@ class Selection extends Model
setBufferRange: (bufferRange, options={}) ->
bufferRange = Range.fromObject(bufferRange)
options.reversed ?= @isReversed()
@editor.destroyFoldsIntersectingBufferRange(bufferRange) unless options.preserveFolds
@editor.destroyFoldsContainingBufferPositions([bufferRange.start, bufferRange.end], true) unless options.preserveFolds
@modifySelection =>
needsFlash = options.flash
delete options.flash if options.flash?
Expand Down
2 changes: 1 addition & 1 deletion src/text-editor-component.js
Expand Up @@ -1771,7 +1771,7 @@ class TextEditorComponent {

if (target && target.matches('.fold-marker')) {
const bufferPosition = model.bufferPositionForScreenPosition(screenPosition)
model.destroyFoldsIntersectingBufferRange(Range(bufferPosition, bufferPosition))
model.destroyFoldsContainingBufferPositions([bufferPosition], false)
return
}

Expand Down
12 changes: 8 additions & 4 deletions src/text-editor.coffee
Expand Up @@ -2499,8 +2499,9 @@ class TextEditor extends Model
#
# Returns the added {Selection}.
addSelectionForBufferRange: (bufferRange, options={}) ->
bufferRange = Range.fromObject(bufferRange)
unless options.preserveFolds
@destroyFoldsIntersectingBufferRange(bufferRange)
@displayLayer.destroyFoldsContainingBufferPositions([bufferRange.start, bufferRange.end], true)
@selectionsMarkerLayer.markBufferRange(bufferRange, {invalidate: 'never', reversed: options.reversed ? false})
@getLastSelection().autoscroll() unless options.autoscroll is false
@getLastSelection()
Expand Down Expand Up @@ -3321,8 +3322,7 @@ class TextEditor extends Model
# Essential: Unfold the most recent cursor's row by one level.
unfoldCurrentRow: ->
{row} = @getCursorBufferPosition()
position = Point(row, Infinity)
@displayLayer.destroyFoldsIntersectingBufferRange(Range(position, position))
@displayLayer.destroyFoldsContainingBufferPositions([Point(row, Infinity)], false)

# Essential: Fold the given row in buffer coordinates based on its indentation
# level.
Expand Down Expand Up @@ -3351,7 +3351,7 @@ class TextEditor extends Model
# * `bufferRow` A {Number}
unfoldBufferRow: (bufferRow) ->
position = Point(bufferRow, Infinity)
@displayLayer.destroyFoldsIntersectingBufferRange(Range(position, position))
@displayLayer.destroyFoldsContainingBufferPositions([position])

# Extended: For each selection, fold the rows it intersects.
foldSelectedLines: ->
Expand Down Expand Up @@ -3450,6 +3450,10 @@ class TextEditor extends Model
destroyFoldsIntersectingBufferRange: (bufferRange) ->
@displayLayer.destroyFoldsIntersectingBufferRange(bufferRange)

# Remove any {Fold}s found that contain the given array of buffer positions.
destroyFoldsContainingBufferPositions: (bufferPositions, excludeEndpoints) ->
@displayLayer.destroyFoldsContainingBufferPositions(bufferPositions, excludeEndpoints)

###
Section: Gutters
###
Expand Down

0 comments on commit 6e55b29

Please sign in to comment.