Add toggle-fold event to editor

This commit is contained in:
Corey Johnson 2012-05-30 15:43:21 -07:00
parent 63a87fd83e
commit 76f5d35eb9
4 changed files with 41 additions and 8 deletions

View File

@ -2437,7 +2437,19 @@ describe "Editor", ->
expect(editor.buffer.lineForRow(0)).toBe "var first = function () {"
expect(buffer.lineForRow(1)).toBe " var first = function(items) {"
describe "folding", ->
describe "structural folding", ->
describe "when a toggle-fold event is triggered", ->
it "creates/destroys a structual fold based on cursor position", ->
editor.setCursorBufferPosition([1,0])
editor.trigger "toggle-fold"
expect(editor.screenLineForRow(2).fold).toBeDefined()
editor.moveCursorDown()
editor.trigger "toggle-fold"
expect(editor.screenLineForRow(2).fold).toBeUndefined()
describe "primitive folding", ->
beforeEach ->
editor.setBuffer(new Buffer(require.resolve('fixtures/two-hundred.txt')))
editor.attachToDom()

View File

@ -141,7 +141,7 @@ describe "Renderer", ->
expect(event.newRange).toEqual([[0, 0], [18, 2]])
expect(event.lineNumbersChanged).toBeTruthy()
describe "fold suggestion", ->
describe "structural folding", ->
describe "the foldable flag on screen lines", ->
it "sets 'foldable' to true for screen lines that start a foldable region", ->
expect(renderer.lineForRow(0).foldable).toBeTruthy()
@ -157,6 +157,17 @@ describe "Renderer", ->
expect(renderer.lineForRow(2).foldable).toBeTruthy()
expect(renderer.lineForRow(3).foldable).toBeFalsy()
describe ".toggleFoldAtBufferRow(bufferRow)", ->
describe "when bufferRow can be folded", ->
it "creates/destroys a fold based on the syntactic region starting at the given row", ->
renderer.toggleFoldAtBufferRow(1)
fold = renderer.lineForRow(2).fold
expect(fold.startRow).toBe 2
expect(fold.endRow).toBe 9
renderer.toggleFoldAtBufferRow(2)
expect(renderer.lineForRow(2).fold).toBeUndefined()
describe ".createFoldAtBufferRow(bufferRow)", ->
it "creates a fold based on the syntactic region starting at the given row", ->
renderer.createFoldAtBufferRow(1)
@ -164,7 +175,7 @@ describe "Renderer", ->
expect(fold.startRow).toBe 2
expect(fold.endRow).toBe 9
describe "folding", ->
describe "primitive folding", ->
beforeEach ->
buffer = new Buffer(require.resolve 'fixtures/two-hundred.txt')
renderer = new Renderer(buffer, {tabText})

View File

@ -120,6 +120,7 @@ class Editor extends View
'undo': @undo
'redo': @redo
'toggle-soft-wrap': @toggleSoftWrap
'toggle-fold': @toggleFold
'fold-selection': @foldSelection
'unfold': => @unfoldRow(@getCursorBufferPosition().row)
'split-left': @splitLeft
@ -710,11 +711,6 @@ class Editor extends View
copySelection: -> @compositeSelection.copy()
paste: -> @insertText($native.readFromPasteboard())
foldSelection: -> @getSelection().fold()
unfoldRow: (row) ->
@renderer.largestFoldForBufferRow(row)?.destroy()
undo: ->
if ranges = @buffer.undo()
@setSelectedBufferRanges(ranges)
@ -808,5 +804,13 @@ class Editor extends View
for cursor in @getCursors()
do (cursor) -> cursor.resetCursorAnimation()
toggleFold: ->
@renderer.toggleFoldAtBufferRow(@getCursorBufferPosition().row)
foldSelection: -> @getSelection().fold()
unfoldRow: (row) ->
@renderer.largestFoldForBufferRow(row)?.destroy()
logLines: (start, end) ->
@renderer.logLines(start, end)

View File

@ -53,6 +53,12 @@ class Renderer
bufferRowsForScreenRows: (startRow, endRow) ->
@lineMap.bufferRowsForScreenRows(startRow, endRow)
toggleFoldAtBufferRow: (bufferRow) ->
if fold = @largestFoldForBufferRow(bufferRow)
fold.destroy()
else
@createFoldAtBufferRow(bufferRow)
createFoldAtBufferRow: (bufferRow) ->
[startRow, endRow] = @foldSuggester.rowRangeForFoldAtBufferRow(bufferRow)
@createFold(startRow, endRow)