If a selection ends on a fold, backspace/delete delete all lines inside the fold

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-06-14 15:52:54 -06:00
parent c73eb97d72
commit f025bec910
5 changed files with 38 additions and 13 deletions

View File

@@ -783,6 +783,15 @@ describe "EditSession", ->
expect(buffer.lineForRow(7)).toBe " } return sort(left).concat(pivot).concat(sort(right));"
expect(buffer.lineForRow(8)).toBe " };"
describe "when the cursor is on a folded screen line", ->
it "deletes all of the folded lines along with the fold", ->
editSession.toggleFoldAtBufferRow(1)
editSession.setCursorScreenPosition([1, 0])
editSession.backspace()
expect(buffer.lineForRow(1)).toBe ""
expect(buffer.lineForRow(2)).toBe " return sort(Array.apply(this, arguments));"
expect(editSession.getCursorScreenPosition()).toEqual [1, 0]
describe "when there are multiple cursors", ->
describe "when cursors are on the same line", ->
it "removes the characters preceding each cursor", ->
@@ -843,10 +852,15 @@ describe "EditSession", ->
describe "when the selection ends on a folded line", ->
it "destroys the fold", ->
editSession.createFold(2,4)
editSession.setSelectedBufferRange([[1,0], [2,0]])
editSession.toggleFoldAtBufferRow(4)
editSession.setSelectedBufferRange([[3,0], [4,0]])
editSession.backspace()
expect(editSession.lineForScreenRow(2).fold).toBeUndefined()
buffer.logLines()
expect(buffer.lineForRow(3)).toBe " return sort(left).concat(pivot).concat(sort(right));"
expect(buffer.lineForRow(4)).toBe " };"
expect(editSession.getCursorScreenPosition()).toEqual [3, 0]
describe "when there are multiple selections", ->
it "removes all selected text", ->

View File

@@ -61,7 +61,7 @@ class DisplayBuffer
[startRow, endRow] = @tokenizedBuffer.rowRangeForFoldAtBufferRow(currentRow) ? []
continue unless startRow? and startRow <= bufferRow <= endRow
if fold = @largestFoldForBufferRow(startRow)
if fold = @largestFoldStartingAtBufferRow(startRow)
fold.destroy()
else
@createFold(startRow, endRow)
@@ -121,7 +121,7 @@ class DisplayBuffer
_.remove(folds, fold)
delete @foldsById[fold.id]
largestFoldForBufferRow: (bufferRow) ->
largestFoldStartingAtBufferRow: (bufferRow) ->
return unless folds = @activeFolds[bufferRow]
(folds.sort (a, b) -> b.endRow - a.endRow)[0]
@@ -200,7 +200,7 @@ class DisplayBuffer
screenLine = @tokenizedBuffer.lineForScreenRow(currentBufferRow)
screenLine.foldable = @tokenizedBuffer.isBufferRowFoldable(currentBufferRow)
if fold = @largestFoldForBufferRow(currentBufferRow)
if fold = @largestFoldStartingAtBufferRow(currentBufferRow)
screenLine = screenLine.copy()
screenLine.fold = fold
screenLine.bufferDelta = fold.getBufferDelta()

View File

@@ -194,7 +194,7 @@ class EditSession
@displayBuffer.destroyFoldsContainingBufferRow(bufferRow)
unfoldCurrentRow: (row) ->
@displayBuffer.largestFoldForBufferRow(@getLastCursor().getCurrentBufferRow())?.destroy()
@displayBuffer.largestFoldStartingAtBufferRow(@getLastCursor().getCurrentBufferRow())?.destroy()
destroyFold: (foldId) ->
fold = @displayBuffer.foldsById[foldId]
@@ -204,6 +204,9 @@ class EditSession
isFoldedAtScreenRow: (screenRow) ->
@lineForScreenRow(screenRow).fold?
largestFoldStartingAtBufferRow: (bufferRow) ->
@displayBuffer.largestFoldStartingAtBufferRow(bufferRow)
autoIndentTextAfterBufferPosition: (text, bufferPosition) ->
return { text } unless @autoIndent
@tokenizedBuffer.autoIndentTextAfterBufferPosition(text, bufferPosition)

View File

@@ -18,8 +18,13 @@ class Fold
inspect: ->
"Fold(#{@startRow}, #{@endRow})"
getBufferRange: ->
new Range([@startRow, 0], [@endRow, Infinity])
getBufferRange: ({includeNewline}={}) ->
if includeNewline
end = [@endRow + 1, 0]
else
end = [@endRow, Infinity]
new Range([@startRow, 0], end)
getBufferDelta: ->
new Point(@endRow - @startRow + 1, 0)

View File

@@ -132,9 +132,7 @@ class Selection
@autoOutdent() if shouldOutdent
backspace: ->
@editSession.destroyFoldsContainingBufferRow(@getBufferRange().end.row)
if @isEmpty()
if @isEmpty() and not @editSession.isFoldedAtScreenRow(@cursor.getCurrentScreenRow())
if @editSession.isFoldedAtScreenRow(@cursor.getCurrentScreenRow() - 1)
@selectToBufferPosition([@cursor.getCurrentBufferRow() - 1, Infinity])
else
@@ -156,8 +154,13 @@ class Selection
deleteSelectedText: ->
bufferRange = @getBufferRange()
if fold = @editSession.largestFoldStartingAtBufferRow(bufferRange.end.row)
bufferRange = bufferRange.union(fold.getBufferRange(includeNewline: true))
@editSession.buffer.delete(bufferRange) unless bufferRange.isEmpty()
@clear() if @cursor
if @cursor
@cursor.setBufferPosition(bufferRange.start)
@clear()
indentSelectedRows: ->
range = @getBufferRange()