Add foldAllAtIndentLevel()

This commit is contained in:
Ben Ogle
2013-07-18 11:27:04 -07:00
parent 89e3d2efec
commit d4d3426b4e
3 changed files with 38 additions and 0 deletions

View File

@@ -191,6 +191,29 @@ describe "LanguageMode", ->
fold4 = editSession.lineForScreenRow(3).fold
expect([fold4.getStartRow(), fold4.getEndRow()]).toEqual [6, 8]
describe ".foldAllAtIndentLevel()", ->
it "folds every foldable range at a given indentLevel", ->
editSession.foldAllAtIndentLevel(2)
fold1 = editSession.lineForScreenRow(6).fold
expect([fold1.getStartRow(), fold1.getEndRow()]).toEqual [6, 8]
fold1.destroy()
fold2 = editSession.lineForScreenRow(11).fold
expect([fold2.getStartRow(), fold2.getEndRow()]).toEqual [11, 14]
fold2.destroy()
it "does not fold anything but the indentLevel", ->
editSession.foldAllAtIndentLevel(0)
fold1 = editSession.lineForScreenRow(0).fold
expect([fold1.getStartRow(), fold1.getEndRow()]).toEqual [0, 19]
fold1.destroy()
fold2 = editSession.lineForScreenRow(5).fold
expect(fold2).toBeFalsy()

View File

@@ -531,6 +531,9 @@ class EditSession
unfoldAll: ->
@languageMode.unfoldAll()
foldAllAtIndentLevel: (indentLevel) ->
@languageMode.foldAllAtIndentLevel(indentLevel)
# Folds the current row.
foldCurrentRow: ->
bufferRow = @bufferPositionForScreenPosition(@getCursorScreenPosition()).row

View File

@@ -98,6 +98,18 @@ class LanguageMode
for row in [@buffer.getLastRow()..0]
fold.destroy() for fold in @editSession.displayBuffer.foldsStartingAtBufferRow(row)
# Fold all comment and code blocks at a given indentLevel
#
# indentLevel - A {Number} indicating indentLevel; 0 based.
foldAllAtIndentLevel: (indentLevel) ->
for currentRow in [0..@buffer.getLastRow()]
[startRow, endRow] = @rowRangeForFoldAtBufferRow(currentRow) ? []
continue unless startRow?
# assumption: startRow will always be the min indent level for the entire range
if @editSession.indentationForBufferRow(startRow) == indentLevel
@editSession.createFold(startRow, endRow)
# Given a buffer row, creates a fold at it.
#
# bufferRow - A {Number} indicating the buffer row