Ignore leading whitespace when auto-indenting a newline

Refs atom/language-less#1
This commit is contained in:
Kevin Sawicki
2014-03-18 19:02:35 -07:00
parent 532cfb5775
commit 8e62d772b3
4 changed files with 24 additions and 7 deletions

View File

@@ -373,10 +373,16 @@ class Editor extends Model
#
# bufferRow - A {Number} indicating the buffer row.
# newLevel - A {Number} indicating the new indentation level.
setIndentationForBufferRow: (bufferRow, newLevel) ->
currentIndentLength = @lineForBufferRow(bufferRow).match(/^\s*/)[0].length
# options - An {Object} with the following keys:
# :ignoreLeadingWhitespace - true to not replace any of the leading
# whitespace already on the line (default: false)
setIndentationForBufferRow: (bufferRow, newLevel, {ignoreLeadingWhitespace}={}) ->
if ignoreLeadingWhitespace
endColumn = 0
else
endColumn = @lineForBufferRow(bufferRow).match(/^\s*/)[0].length
newIndentString = @buildIndentString(newLevel)
@buffer.change([[bufferRow, 0], [bufferRow, currentIndentLength]], newIndentString)
@buffer.change([[bufferRow, 0], [bufferRow, endColumn]], newIndentString)
# Public: Get the indentation level of the given line of text.
#

View File

@@ -265,10 +265,11 @@ class LanguageMode
# Given a buffer row, this indents it.
#
# bufferRow - The row {Number}
autoIndentBufferRow: (bufferRow) ->
# bufferRow - The row {Number}.
# options - An options {Object} to pass to {Editor::setIndentationForBufferRow}.
autoIndentBufferRow: (bufferRow, options) ->
indentLevel = @suggestedIndentForBufferRow(bufferRow)
@editor.setIndentationForBufferRow(bufferRow, indentLevel)
@editor.setIndentationForBufferRow(bufferRow, indentLevel, options)
# Given a buffer row, this decreases the indentation.
#

View File

@@ -301,7 +301,7 @@ class Selection
if options.autoIndent
@editor.autoIndentBufferRow(row) for row in newBufferRange.getRows()
else if options.autoIndentNewline and text == '\n'
@editor.autoIndentBufferRow(newBufferRange.end.row)
@editor.autoIndentBufferRow(newBufferRange.end.row, ignoreLeadingWhitespace: true)
else if options.autoDecreaseIndent and /\S/.test text
@editor.autoDecreaseIndentForBufferRow(newBufferRange.start.row)