More updates

This commit is contained in:
Garen Torikian
2013-04-09 01:18:12 -05:00
parent f2698bc6a9
commit d042fadab1
4 changed files with 191 additions and 15 deletions

View File

@@ -90,6 +90,9 @@ class DisplayBuffer
endRow = currentRow
return [startRow, endRow] if startRow isnt endRow
# Public: Given a buffer row, this folds it.
#
# bufferRow - A {Number} indicating the buffer row
foldBufferRow: (bufferRow) ->
for currentRow in [bufferRow..0]
rowRange = @rowRangeForCommentAtBufferRow(currentRow)
@@ -103,9 +106,16 @@ class DisplayBuffer
return
# Public: Given a buffer row, this unfolds it.
#
# bufferRow - A {Number} indicating the buffer row
unfoldBufferRow: (bufferRow) ->
@largestFoldContainingBufferRow(bufferRow)?.destroy()
# Public: Creates a new fold between two row numbers.
#
# startRow - The row {Number} to start folding at
# endRow - The row {Number} to end the fold
createFold: (startRow, endRow) ->
return fold if fold = @foldFor(startRow, endRow)
fold = new Fold(this, startRow, endRow)
@@ -154,6 +164,9 @@ class DisplayBuffer
@triggerChanged({ start, end, screenDelta, bufferDelta })
# Public: Removes any folds found that contain the given buffer row.
#
# bufferRow - The buffer row {Number} to check against
destroyFoldsContainingBufferRow: (bufferRow) ->
for row, folds of @activeFolds
for fold in new Array(folds...)
@@ -170,13 +183,37 @@ class DisplayBuffer
delete @foldsById[fold.id]
delete @activeFolds[bufferRow] if folds.length == 0
# Public: Given a buffer row, this returns the largest fold that starts there.
#
# Largest is defined as the fold whose difference between its start and end points
# are the greatest.
#
# bufferRow - A {Number} indicating the buffer row
#
# Returns a {Fold}.
largestFoldStartingAtBufferRow: (bufferRow) ->
return unless folds = @activeFolds[bufferRow]
(folds.sort (a, b) -> b.endRow - a.endRow)[0]
# Public: Given a screen row, this returns the largest fold that starts there.
#
# Largest is defined as the fold whose difference between its start and end points
# are the greatest.
#
# screenRow - A {Number} indicating the screen row
#
# Returns a {Fold}.
largestFoldStartingAtScreenRow: (screenRow) ->
@largestFoldStartingAtBufferRow(@bufferRowForScreenRow(screenRow))
# Public: Given a buffer row, this returns the largest fold that includes it.
#
# Largest is defined as the fold whose difference between its start and end points
# are the greatest.
#
# bufferRow - A {Number} indicating the buffer row
#
# Returns a {Fold}.
largestFoldContainingBufferRow: (bufferRow) ->
largestFold = null
for currentBufferRow in [bufferRow..0]
@@ -197,10 +234,18 @@ class DisplayBuffer
bufferRowForScreenRow: (screenRow) ->
@lineMap.bufferPositionForScreenPosition([screenRow, 0]).row
# Public: Given a buffer range, this converts it into a screen position.
#
# bufferRange - The {Range} to convert
#
# Returns a {Range}.
screenRangeForBufferRange: (bufferRange) ->
@lineMap.screenRangeForBufferRange(bufferRange)
# Public: Given a screen range, this converts it into a buffer position.
#
# screenRange - The {Range} to convert
#
# Returns a {Range}.
bufferRangeForScreenRange: (screenRange) ->
@lineMap.bufferRangeForScreenRange(screenRange)
@@ -213,9 +258,22 @@ class DisplayBuffer
maxLineLength: ->
@lineMap.maxScreenLineLength
# Public: Given a buffer position, this converts it into a screen position.
#
# bufferPosition - An object that represents a buffer position. It can be either
# an {Object} (`{row, column}`), {Array} (`[row, column]`), or {Point}
# options - The same options available to {LineMap.clipScreenPosition}.
#
# Returns a {Point}.
screenPositionForBufferPosition: (position, options) ->
@lineMap.screenPositionForBufferPosition(position, options)
# Public: Given a buffer range, this converts it into a screen position.
#
# screenPosition - An object that represents a buffer position. It can be either
# an {Object} (`{row, column}`), {Array} (`[row, column]`), or {Point}
# options - The same options available to {LineMap.clipScreenPosition}.
#
# Returns a {Point}.
bufferPositionForScreenPosition: (position, options) ->
@lineMap.bufferPositionForScreenPosition(position, options)

View File

@@ -58,7 +58,7 @@ class EditSession
@subscribe syntax, 'grammars-loaded', => @reloadGrammar()
# Internal
# Internal:
getViewClass: ->
require 'editor'
@@ -309,16 +309,16 @@ class EditSession
bufferPositionForScreenPosition: (screenPosition, options) -> @displayBuffer.bufferPositionForScreenPosition(screenPosition, options)
# Public: Given a buffer range, this converts it into a screen position.
#
# range - The {Range} to convert
# bufferRange - The {Range} to convert
#
# Returns a {Range}.
screenRangeForBufferRange: (range) -> @displayBuffer.screenRangeForBufferRange(range)
screenRangeForBufferRange: (bufferRange) -> @displayBuffer.screenRangeForBufferRange(bufferRange)
# Public: Given a screen range, this converts it into a buffer position.
#
# range - The {Range} to convert
# screenRange - The {Range} to convert
#
# Returns a {Range}.
bufferRangeForScreenRange: (range) -> @displayBuffer.bufferRangeForScreenRange(range)
bufferRangeForScreenRange: (screenRange) -> @displayBuffer.bufferRangeForScreenRange(screenRange)
clipScreenPosition: (screenPosition, options) -> @displayBuffer.clipScreenPosition(screenPosition, options)
# Public: Gets the line for the given screen row.
#
@@ -349,10 +349,14 @@ class EditSession
scopesForBufferPosition: (bufferPosition) -> @displayBuffer.scopesForBufferPosition(bufferPosition)
getCursorScopes: -> @getCursor().getScopes()
logScreenLines: (start, end) -> @displayBuffer.logLines(start, end)
# Public: Determines whether the {Editor} will auto indent rows.
#
# Returns a {Boolean}.
shouldAutoIndent: ->
config.get("editor.autoIndent")
# Public: Determines whether the {Editor} will auto indent pasted text.
#
# Returns a {Boolean}.
shouldAutoIndentPastedText: ->
config.get("editor.autoIndentOnPaste")
# Public: Inserts text at the current cursor positions.
@@ -474,6 +478,7 @@ class EditSession
redo: ->
@buffer.redo(this)
# Internal:
transact: (fn) ->
isNewTransaction = @buffer.transact()
oldSelectedRanges = @getSelectedBufferRanges()
@@ -485,6 +490,7 @@ class EditSession
@commit() if isNewTransaction
result
# Internal:
commit: ->
newSelectedRanges = @getSelectedBufferRanges()
@pushOperation
@@ -492,6 +498,7 @@ class EditSession
editSession?.setSelectedBufferRanges(newSelectedRanges)
@buffer.commit()
# Internal:
abort: ->
@buffer.abort()
@@ -508,6 +515,9 @@ class EditSession
bufferRow = @bufferPositionForScreenPosition(@getCursorScreenPosition()).row
@foldBufferRow(bufferRow)
# Public: Given a buffer row, this folds it.
#
# bufferRow - A {Number} indicating the buffer row
foldBufferRow: (bufferRow) ->
@displayBuffer.foldBufferRow(bufferRow)
@@ -516,22 +526,39 @@ class EditSession
bufferRow = @bufferPositionForScreenPosition(@getCursorScreenPosition()).row
@unfoldBufferRow(bufferRow)
# Public: Given a buffer row, this unfolds it.
#
# bufferRow - A {Number} indicating the buffer row
unfoldBufferRow: (bufferRow) ->
@displayBuffer.unfoldBufferRow(bufferRow)
# Public: Folds all selections.
foldSelection: ->
selection.fold() for selection in @getSelections()
# Public: Creates a new fold between two row numbers.
#
# startRow - The row {Number} to start folding at
# endRow - The row {Number} to end the fold
createFold: (startRow, endRow) ->
@displayBuffer.createFold(startRow, endRow)
# Public: Removes any folds found that contain the given buffer row.
#
# bufferRow - The buffer row {Number} to check against
destroyFoldsContainingBufferRow: (bufferRow) ->
@displayBuffer.destroyFoldsContainingBufferRow(bufferRow)
# Public: Removes any folds found that intersect the given buffer row.
#
# bufferRow - The buffer row {Number} to check against
destroyFoldsIntersectingBufferRange: (bufferRange) ->
for row in [bufferRange.start.row..bufferRange.end.row]
@destroyFoldsContainingBufferRow(row)
# Public: Given the id of a fold, this removes it.
#
# foldId - The fold id {Number} to remove
destroyFold: (foldId) ->
fold = @displayBuffer.foldsById[foldId]
fold.destroy()
@@ -542,14 +569,16 @@ class EditSession
# Returns `true` if the row is folded, `false` otherwise.
isFoldedAtCursorRow: ->
@isFoldedAtScreenRow(@getCursorScreenRow())
# Public: Determines if the given buffer row is folded.
#
# screenRow - A {Number} indicating the buffer row.
# bufferRow - A {Number} indicating the buffer row.
#
# Returns `true` if the buffer row is folded, `false` otherwise.
isFoldedAtBufferRow: (bufferRow) ->
screenRow = @screenPositionForBufferPosition([bufferRow]).row
@isFoldedAtScreenRow(screenRow)
# Public: Determines if the given screen row is folded.
#
# screenRow - A {Number} indicating the screen row.
@@ -558,27 +587,69 @@ class EditSession
isFoldedAtScreenRow: (screenRow) ->
@lineForScreenRow(screenRow)?.fold?
# Public: Given a buffer row, this returns the largest fold that includes it.
#
# Largest is defined as the fold whose difference between its start and end points
# are the greatest.
#
# bufferRow - A {Number} indicating the buffer row
#
# Returns a {Fold}.
largestFoldContainingBufferRow: (bufferRow) ->
@displayBuffer.largestFoldContainingBufferRow(bufferRow)
# Public: Given a screen row, this returns the largest fold that starts there.
#
# Largest is defined as the fold whose difference between its start and end points
# are the greatest.
#
# screenRow - A {Number} indicating the screen row
#
# Returns a {Fold}.
largestFoldStartingAtScreenRow: (screenRow) ->
@displayBuffer.largestFoldStartingAtScreenRow(screenRow)
# Public: Given a buffer row, this returns a suggested indentation level.
#
# The indentation level provided is based on the current {LangugaeMode}.
#
# bufferRow - A {Number} indicating the buffer row
#
# Returns a {Number}.
suggestedIndentForBufferRow: (bufferRow) ->
@languageMode.suggestedIndentForBufferRow(bufferRow)
# Public: Indents all the rows between two buffer rows.
#
# startRow - The row {Number} to start at
# endRow - The row {Number} to end at
autoIndentBufferRows: (startRow, endRow) ->
@languageMode.autoIndentBufferRows(startRow, endRow)
# Public: Given a buffer row, this indents it.
#
# bufferRow - The row {Number}
autoIndentBufferRow: (bufferRow) ->
@languageMode.autoIndentBufferRow(bufferRow)
# Public: Given a buffer row, this increases the indentation.
#
# bufferRow - The row {Number}
autoIncreaseIndentForBufferRow: (bufferRow) ->
@languageMode.autoIncreaseIndentForBufferRow(bufferRow)
# Public: Given a buffer row, this decreases the indentation.
#
# bufferRow - The row {Number}
autoDecreaseIndentForRow: (bufferRow) ->
@languageMode.autoDecreaseIndentForBufferRow(bufferRow)
# Public: Wraps the lines between two rows in comments.
#
# If the language doesn't have comments, nothing happens.
#
# startRow - The row {Number} to start at
# endRow - The row {Number} to end at
toggleLineCommentsForBufferRows: (start, end) ->
@languageMode.toggleLineCommentsForBufferRows(start, end)

View File

@@ -21,6 +21,12 @@ class LanguageMode
throw new Error("No grammar found for path: #{path}") unless @grammar
previousGrammar isnt @grammar
# Public: Wraps the lines between two rows in comments.
#
# If the language doesn't have comment, nothing happens.
#
# startRow - The row {Number} to start at
# endRow - The row {Number} to end at
toggleLineCommentsForBufferRows: (start, end) ->
scopes = @editSession.scopesForBufferPosition([start, 0])
return unless commentStartString = syntax.getProperty(scopes, "editor.commentStart")
@@ -83,6 +89,13 @@ class LanguageMode
[bufferRow, foldEndRow]
# Public: Given a buffer row, this returns a suggested indentation level.
#
# The indentation level provided is based on the current {LangugaeMode}.
#
# bufferRow - A {Number} indicating the buffer row
#
# Returns a {Number}.
suggestedIndentForBufferRow: (bufferRow) ->
currentIndentLevel = @editSession.indentationForBufferRow(bufferRow)
scopes = @editSession.scopesForBufferPosition([bufferRow, 0])
@@ -102,13 +115,23 @@ class LanguageMode
Math.max(desiredIndentLevel, currentIndentLevel)
# Public: Indents all the rows between two buffer row numbers.
#
# startRow - The row {Number} to start at
# endRow - The row {Number} to end at
autoIndentBufferRows: (startRow, endRow) ->
@autoIndentBufferRow(row) for row in [startRow..endRow]
# Public: Given a buffer row, this indents it.
#
# bufferRow - The row {Number}
autoIndentBufferRow: (bufferRow) ->
@autoIncreaseIndentForBufferRow(bufferRow)
@autoDecreaseIndentForBufferRow(bufferRow)
# Public: Given a buffer row, this increases the indentation.
#
# bufferRow - The row {Number}
autoIncreaseIndentForBufferRow: (bufferRow) ->
precedingRow = @buffer.previousNonBlankRow(bufferRow)
return unless precedingRow?
@@ -124,6 +147,9 @@ class LanguageMode
if desiredIndentLevel > currentIndentLevel
@editSession.setIndentationForBufferRow(bufferRow, desiredIndentLevel)
# Public: Given a buffer row, this decreases the indentation.
#
# bufferRow - The row {Number}
autoDecreaseIndentForBufferRow: (bufferRow) ->
scopes = @editSession.scopesForBufferPosition([bufferRow, 0])
increaseIndentRegex = @increaseIndentRegexForScopes(scopes)

View File

@@ -76,7 +76,14 @@ class LineMap
else
column = screenLine.clipScreenColumn(column, options)
new Point(row, column)
# Public: Given a buffer position, this converts it into a screen position.
#
# bufferPosition - An object that represents a buffer position. It can be either
# an {Object} (`{row, column}`), {Array} (`[row, column]`), or {Point}
# options - The same options available to {LineMap.clipScreenPosition}.
#
# Returns a {Point}.
screenPositionForBufferPosition: (bufferPosition, options={}) ->
{ row, column } = Point.fromObject(bufferPosition)
[screenRow, screenLines] = @screenRowAndScreenLinesForBufferRow(row)
@@ -109,7 +116,13 @@ class LineMap
currentBufferRow = nextBufferRow
[screenRow, screenLines]
# Public: Given a buffer range, this converts it into a screen position.
#
# screenPosition - An object that represents a buffer position. It can be either
# an {Object} (`{row, column}`), {Array} (`[row, column]`), or {Point}
# options - The same options available to {LineMap.clipScreenPosition}.
#
# Returns a {Point}.
bufferPositionForScreenPosition: (screenPosition, options) ->
{ row, column } = @clipScreenPosition(Point.fromObject(screenPosition), options)
[bufferRow, screenLine] = @bufferRowAndScreenLineForScreenRow(row)
@@ -125,13 +138,21 @@ class LineMap
bufferRow += screenLine.bufferRows
[bufferRow, screenLine]
# Public: Given a buffer range, this converts it into a screen position.
#
# bufferRange - The {Range} to convert
#
# Returns a {Range}.
screenRangeForBufferRange: (bufferRange) ->
bufferRange = Range.fromObject(bufferRange)
start = @screenPositionForBufferPosition(bufferRange.start)
end = @screenPositionForBufferPosition(bufferRange.end)
new Range(start, end)
# Public: Given a screen range, this converts it into a buffer position.
#
# screenRange - The {Range} to convert
#
# Returns a {Range}.
bufferRangeForScreenRange: (screenRange) ->
screenRange = Range.fromObject(screenRange)
start = @bufferPositionForScreenPosition(screenRange.start)