Move setIndentationForBufferRow and indentationForBufferRow to EditSession

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-10-25 16:17:07 -07:00
parent 973c69a043
commit 29351ee5fc
4 changed files with 24 additions and 25 deletions

View File

@@ -671,13 +671,13 @@ describe "EditSession", ->
it "auto-indents the new line to one additional level of indentation beyond the preceding line", ->
editSession.setCursorBufferPosition([1, Infinity])
editSession.insertText('\n', autoIndent: true)
expect(buffer.indentationForRow(2)).toBe buffer.indentationForRow(1) + 2
expect(editSession.indentationForBufferRow(2)).toBe editSession.indentationForBufferRow(1) + 2
describe "when the newline is inserted on a normal line", ->
it "auto-indents the new line to the same level of indentation as the preceding line", ->
editSession.setCursorBufferPosition([5, 13])
editSession.insertText('\n', autoIndent: true)
expect(buffer.indentationForRow(6)).toBe buffer.indentationForRow(5)
expect(editSession.indentationForBufferRow(6)).toBe editSession.indentationForBufferRow(5)
describe "when text without newlines is inserted", ->
describe "when the current line matches an auto-outdent pattern", ->
@@ -686,24 +686,24 @@ describe "EditSession", ->
editSession.setCursorBufferPosition([2, 4])
editSession.insertText('\n', autoIndent: true)
editSession.setCursorBufferPosition([2, 4])
expect(buffer.indentationForRow(2)).toBe buffer.indentationForRow(1) + 2
expect(editSession.indentationForBufferRow(2)).toBe editSession.indentationForBufferRow(1) + 2
editSession.insertText(' }', autoIndent: true)
expect(buffer.indentationForRow(2)).toBe buffer.indentationForRow(1)
expect(editSession.indentationForBufferRow(2)).toBe editSession.indentationForBufferRow(1)
describe "when the preceding does not match an auto-indent pattern", ->
it "auto-decreases the indentation of the line to be one level below that of the preceding line", ->
editSession.setCursorBufferPosition([3, Infinity])
editSession.insertText('\n', autoIndent: true)
expect(buffer.indentationForRow(4)).toBe buffer.indentationForRow(3)
expect(editSession.indentationForBufferRow(4)).toBe editSession.indentationForBufferRow(3)
editSession.insertText(' }', autoIndent: true)
expect(buffer.indentationForRow(4)).toBe buffer.indentationForRow(3) - 2
expect(editSession.indentationForBufferRow(4)).toBe editSession.indentationForBufferRow(3) - 2
describe "when the current line does not match an auto-outdent pattern", ->
it "leaves the line unchanged", ->
editSession.setCursorBufferPosition([2, 4])
expect(buffer.indentationForRow(2)).toBe buffer.indentationForRow(1) + 2
expect(editSession.indentationForBufferRow(2)).toBe editSession.indentationForBufferRow(1) + 2
editSession.insertText('foo', autoIndent: true)
expect(buffer.indentationForRow(2)).toBe buffer.indentationForRow(1) + 2
expect(editSession.indentationForBufferRow(2)).toBe editSession.indentationForBufferRow(1) + 2
describe "when the `normalizeIndent` option is true", ->
describe "when the inserted text contains no newlines", ->

View File

@@ -352,14 +352,6 @@ class Buffer
return row unless @isRowBlank(row)
null
indentationForRow: (row) ->
@lineForRow(row).match(/^\s*/)?[0].length
setIndentationForRow: (bufferRow, newLevel) ->
currentLevel = @indentationForRow(bufferRow)
indentString = [0...newLevel].map(-> ' ').join('')
@change([[bufferRow, 0], [bufferRow, currentLevel]], indentString)
logLines: (start=0, end=@getLastRow())->
for row in [start..end]
line = @lineForRow(row)

View File

@@ -113,11 +113,18 @@ class EditSession
clipBufferPosition: (bufferPosition) ->
@buffer.clipPosition(bufferPosition)
indentationForBufferRow: (bufferRow) ->
@lineForBufferRow(bufferRow).match(/^\s*/)?[0].length
setIndentationForBufferRow: (bufferRow, newLevel) ->
currentLevel = @indentationForBufferRow(bufferRow)
indentString = [0...newLevel].map(-> ' ').join('')
@buffer.change([[bufferRow, 0], [bufferRow, currentLevel]], indentString)
getFileExtension: -> @buffer.getExtension()
getPath: -> @buffer.getPath()
isBufferRowBlank: (bufferRow) -> @buffer.isRowBlank(bufferRow)
nextNonBlankBufferRow: (bufferRow) -> @buffer.nextNonBlankRow(bufferRow)
indentationForBufferRow: (bufferRow) -> @buffer.indentationForRow(bufferRow)
getEofBufferPosition: -> @buffer.getEofPosition()
getLastBufferRow: -> @buffer.getLastRow()
bufferRangeForBufferRow: (row) -> @buffer.rangeForRow(row)

View File

@@ -98,7 +98,7 @@ class LanguageMode
[bufferRow, foldEndRow]
suggestedIndentForBufferRow: (bufferRow) ->
currentIndentation = @buffer.indentationForRow(bufferRow)
currentIndentation = @editSession.indentationForBufferRow(bufferRow)
scopes = @tokenizedBuffer.scopesForPosition([bufferRow, 0])
return currentIndentation unless increaseIndentPattern = TextMateBundle.indentRegexForScope(scopes[0])
@@ -108,7 +108,7 @@ class LanguageMode
precedingLine = @buffer.lineForRow(precedingRow)
desiredIndentation = @buffer.indentationForRow(precedingRow)
desiredIndentation = @editSession.indentationForBufferRow(precedingRow)
desiredIndentation += @editSession.tabLength if increaseIndentPattern.test(precedingLine)
return desiredIndentation unless decreaseIndentPattern = TextMateBundle.outdentRegexForScope(scopes[0])
@@ -132,11 +132,11 @@ class LanguageMode
increaseIndentPattern = TextMateBundle.indentRegexForScope(scopes[0])
return unless increaseIndentPattern
currentIndentation = @buffer.indentationForRow(bufferRow)
desiredIndentation = @buffer.indentationForRow(precedingRow)
currentIndentation = @editSession.indentationForBufferRow(bufferRow)
desiredIndentation = @editSession.indentationForBufferRow(precedingRow)
desiredIndentation += @editSession.tabLength if increaseIndentPattern.test(precedingLine)
if desiredIndentation > currentIndentation
@buffer.setIndentationForRow(bufferRow, desiredIndentation)
@editSession.setIndentationForBufferRow(bufferRow, desiredIndentation)
autoDecreaseIndentForBufferRow: (bufferRow) ->
scopes = @tokenizedBuffer.scopesForPosition([bufferRow, 0])
@@ -147,14 +147,14 @@ class LanguageMode
line = @buffer.lineForRow(bufferRow)
return unless decreaseIndentPattern.test(line)
currentIndentation = @buffer.indentationForRow(bufferRow)
currentIndentation = @editSession.indentationForBufferRow(bufferRow)
precedingRow = @buffer.previousNonBlankRow(bufferRow)
precedingLine = @buffer.lineForRow(precedingRow)
desiredIndentation = @buffer.indentationForRow(precedingRow)
desiredIndentation = @editSession.indentationForBufferRow(precedingRow)
desiredIndentation -= @editSession.tabLength unless increaseIndentPattern.test(precedingLine)
if desiredIndentation < currentIndentation
@buffer.setIndentationForRow(bufferRow, desiredIndentation)
@editSession.setIndentationForBufferRow(bufferRow, desiredIndentation)
getLineTokens: (line, stack) ->
{tokens, stack} = @grammar.getLineTokens(line, stack)