mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Improve auto indenting by removing a hard coded restriction and adding a 3rd regexp
Currently there are only two regexps that can influence the indents. Those are `increaseIndentPattern` (which tells Atom the indent of next line should be increased) and `decreaseIndentPattern` (which tells Atom the indent of the current line should be decreased). But I found that a couple of languages would need a 3rd regexp in order to support their use cases. This 3rd regexp should be a mixture of the existing two so it tells Atom that the indent of the next line should decrease. I’ll add a screencast to show a use case for this 3rd regexp which I would like to call `decreaseNextIndentPattern`.
This commit is contained in:
committed by
Sander van Harmelen
parent
1ebc8db123
commit
eaf814e5be
@@ -246,8 +246,12 @@ class LanguageMode
|
||||
iterator.next()
|
||||
scopeDescriptor = new ScopeDescriptor(scopes: iterator.getScopes())
|
||||
|
||||
increaseIndentRegex = @increaseIndentRegexForScopeDescriptor(scopeDescriptor)
|
||||
decreaseIndentRegex = @decreaseIndentRegexForScopeDescriptor(scopeDescriptor)
|
||||
decreaseNextIndentRegex = @decreaseNextIndentRegexForScopeDescriptor(scopeDescriptor)
|
||||
|
||||
currentIndentLevel = @editor.indentationForBufferRow(bufferRow)
|
||||
return currentIndentLevel unless increaseIndentRegex = @increaseIndentRegexForScopeDescriptor(scopeDescriptor)
|
||||
return currentIndentLevel unless increaseIndentRegex
|
||||
|
||||
if options?.skipBlankLines ? true
|
||||
precedingRow = @buffer.previousNonBlankRow(bufferRow)
|
||||
@@ -256,13 +260,17 @@ class LanguageMode
|
||||
precedingRow = bufferRow - 1
|
||||
return currentIndentLevel if precedingRow < 0
|
||||
|
||||
precedingLine = @buffer.lineForRow(precedingRow)
|
||||
desiredIndentLevel = @editor.indentationForBufferRow(precedingRow)
|
||||
desiredIndentLevel += 1 if increaseIndentRegex.testSync(precedingLine) and not @editor.isBufferRowCommented(precedingRow)
|
||||
return desiredIndentLevel if @buffer.isRowBlank(precedingRow)
|
||||
|
||||
return desiredIndentLevel unless decreaseIndentRegex = @decreaseIndentRegexForScopeDescriptor(scopeDescriptor)
|
||||
line = @buffer.lineForRow(bufferRow)
|
||||
desiredIndentLevel -= 1 if decreaseIndentRegex.testSync(line)
|
||||
unless @editor.isBufferRowCommented(precedingRow)
|
||||
precedingLine = @buffer.lineForRow(precedingRow)
|
||||
desiredIndentLevel += 1 if increaseIndentRegex?.testSync(precedingLine)
|
||||
desiredIndentLevel -= 1 if decreaseNextIndentRegex?.testSync(precedingLine)
|
||||
|
||||
unless @editor.isBufferRowCommented(bufferRow)
|
||||
bufferLine = @buffer.lineForRow(bufferRow)
|
||||
desiredIndentLevel -= 1 if decreaseIndentRegex?.testSync(bufferLine)
|
||||
|
||||
Math.max(desiredIndentLevel, 0)
|
||||
|
||||
@@ -298,21 +306,26 @@ class LanguageMode
|
||||
# bufferRow - The row {Number}
|
||||
autoDecreaseIndentForBufferRow: (bufferRow) ->
|
||||
scopeDescriptor = @editor.scopeDescriptorForBufferPosition([bufferRow, 0])
|
||||
increaseIndentRegex = @increaseIndentRegexForScopeDescriptor(scopeDescriptor)
|
||||
decreaseIndentRegex = @decreaseIndentRegexForScopeDescriptor(scopeDescriptor)
|
||||
return unless increaseIndentRegex and decreaseIndentRegex
|
||||
return unless decreaseIndentRegex = @decreaseIndentRegexForScopeDescriptor(scopeDescriptor)
|
||||
|
||||
line = @buffer.lineForRow(bufferRow)
|
||||
return unless decreaseIndentRegex.testSync(line)
|
||||
|
||||
currentIndentLevel = @editor.indentationForBufferRow(bufferRow)
|
||||
return if currentIndentLevel is 0
|
||||
|
||||
precedingRow = @buffer.previousNonBlankRow(bufferRow)
|
||||
return unless precedingRow?
|
||||
precedingLine = @buffer.lineForRow(precedingRow)
|
||||
|
||||
precedingLine = @buffer.lineForRow(precedingRow)
|
||||
desiredIndentLevel = @editor.indentationForBufferRow(precedingRow)
|
||||
desiredIndentLevel -= 1 unless increaseIndentRegex.testSync(precedingLine)
|
||||
|
||||
if increaseIndentRegex = @increaseIndentRegexForScopeDescriptor(scopeDescriptor)
|
||||
desiredIndentLevel -= 1 unless increaseIndentRegex.testSync(precedingLine)
|
||||
|
||||
if decreaseNextIndentRegex = @decreaseNextIndentRegexForScopeDescriptor(scopeDescriptor)
|
||||
desiredIndentLevel -= 1 if decreaseNextIndentRegex.testSync(precedingLine)
|
||||
|
||||
if desiredIndentLevel >= 0 and desiredIndentLevel < currentIndentLevel
|
||||
@editor.setIndentationForBufferRow(bufferRow, desiredIndentLevel)
|
||||
|
||||
@@ -326,6 +339,9 @@ class LanguageMode
|
||||
decreaseIndentRegexForScopeDescriptor: (scopeDescriptor) ->
|
||||
@getRegexForProperty(scopeDescriptor, 'editor.decreaseIndentPattern')
|
||||
|
||||
decreaseNextIndentRegexForScopeDescriptor: (scopeDescriptor) ->
|
||||
@getRegexForProperty(scopeDescriptor, 'editor.decreaseNextIndentPattern')
|
||||
|
||||
foldEndRegexForScopeDescriptor: (scopeDescriptor) ->
|
||||
@getRegexForProperty(scopeDescriptor, 'editor.foldEndPattern')
|
||||
|
||||
|
||||
@@ -395,10 +395,7 @@ class Selection extends Model
|
||||
@editor.setIndentationForBufferRow(oldBufferRange.start.row, desiredIndentLevel)
|
||||
|
||||
if options.autoIndentNewline and text is '\n'
|
||||
currentIndentation = @editor.indentationForBufferRow(newBufferRange.start.row)
|
||||
@editor.autoIndentBufferRow(newBufferRange.end.row, preserveLeadingWhitespace: true, skipBlankLines: false)
|
||||
if @editor.indentationForBufferRow(newBufferRange.end.row) < currentIndentation
|
||||
@editor.setIndentationForBufferRow(newBufferRange.end.row, currentIndentation)
|
||||
else if options.autoDecreaseIndent and NonWhitespaceRegExp.test(text)
|
||||
@editor.autoDecreaseIndentForBufferRow(newBufferRange.start.row)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user