Add proper indents on auto comments

This commit is contained in:
Ben Ogle
2013-07-17 16:04:53 -07:00
parent 4a67bfcf15
commit 90e53ad3e2
2 changed files with 49 additions and 14 deletions

View File

@@ -14,19 +14,25 @@ describe "LanguageMode", ->
editSession = project.open('sample.js', autoIndent: false)
{buffer, languageMode} = editSession
describe ".calcMinIndent(startRow, endRow)", ->
it "returns indent levels for ranges", ->
expect(languageMode.calcMinIndent(4, 7)).toBe 2
expect(languageMode.calcMinIndent(5, 7)).toBe 2
expect(languageMode.calcMinIndent(5, 6)).toBe 3
describe ".toggleLineCommentsForBufferRows(start, end)", ->
it "comments/uncomments lines in the given range", ->
languageMode.toggleLineCommentsForBufferRows(4, 7)
expect(buffer.lineForRow(4)).toBe "// while(items.length > 0) {"
expect(buffer.lineForRow(5)).toBe "// current = items.shift();"
expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);"
expect(buffer.lineForRow(7)).toBe "// }"
expect(buffer.lineForRow(4)).toBe " // while(items.length > 0) {"
expect(buffer.lineForRow(5)).toBe " // current = items.shift();"
expect(buffer.lineForRow(6)).toBe " // current < pivot ? left.push(current) : right.push(current);"
expect(buffer.lineForRow(7)).toBe " // }"
languageMode.toggleLineCommentsForBufferRows(4, 5)
expect(buffer.lineForRow(4)).toBe " while(items.length > 0) {"
expect(buffer.lineForRow(5)).toBe " current = items.shift();"
expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);"
expect(buffer.lineForRow(7)).toBe "// }"
expect(buffer.lineForRow(6)).toBe " // current < pivot ? left.push(current) : right.push(current);"
expect(buffer.lineForRow(7)).toBe " // }"
describe "fold suggestion", ->
describe ".doesBufferRowStartFold(bufferRow)", ->
@@ -56,19 +62,35 @@ describe "LanguageMode", ->
editSession = project.open('coffee.coffee', autoIndent: false)
{buffer, languageMode} = editSession
describe ".calcMinIndent(startRow, endRow)", ->
it "returns indent levels for ranges", ->
expect(languageMode.calcMinIndent(4, 6)).toBe 2
expect(languageMode.calcMinIndent(4, 7)).toBe 2
describe ".toggleLineCommentsForBufferRows(start, end)", ->
it "comments/uncomments lines in the given range", ->
languageMode.toggleLineCommentsForBufferRows(4, 7)
expect(buffer.lineForRow(4)).toBe "# pivot = items.shift()"
expect(buffer.lineForRow(5)).toBe "# left = []"
expect(buffer.lineForRow(6)).toBe "# right = []"
expect(buffer.lineForRow(7)).toBe "# "
languageMode.toggleLineCommentsForBufferRows(4, 6)
expect(buffer.lineForRow(4)).toBe " # pivot = items.shift()"
expect(buffer.lineForRow(5)).toBe " # left = []"
expect(buffer.lineForRow(6)).toBe " # right = []"
languageMode.toggleLineCommentsForBufferRows(4, 5)
expect(buffer.lineForRow(4)).toBe " pivot = items.shift()"
expect(buffer.lineForRow(5)).toBe " left = []"
expect(buffer.lineForRow(6)).toBe "# right = []"
expect(buffer.lineForRow(7)).toBe "# "
expect(buffer.lineForRow(6)).toBe " # right = []"
it "comments/uncomments lines when empty line", ->
languageMode.toggleLineCommentsForBufferRows(4, 7)
expect(buffer.lineForRow(4)).toBe " # pivot = items.shift()"
expect(buffer.lineForRow(5)).toBe " # left = []"
expect(buffer.lineForRow(6)).toBe " # right = []"
expect(buffer.lineForRow(7)).toBe " # "
languageMode.toggleLineCommentsForBufferRows(4, 5)
expect(buffer.lineForRow(4)).toBe " pivot = items.shift()"
expect(buffer.lineForRow(5)).toBe " left = []"
expect(buffer.lineForRow(6)).toBe " # right = []"
expect(buffer.lineForRow(7)).toBe " # "
describe "fold suggestion", ->
describe ".doesBufferRowStartFold(bufferRow)", ->

View File

@@ -81,8 +81,10 @@ class LanguageMode
columnEnd = columnStart + match[2].length
buffer.change([[row, columnStart], [row, columnEnd]], "")
else
indent = @calcMinIndent(start, end)
indentString = @editSession.buildIndentString(indent)
for row in [start..end]
buffer.insert([row, 0], commentStartString)
buffer.change(new Range([row, 0], [row, indentString.length]), indentString+commentStartString)
# Folds all the foldable lines in the buffer.
foldAll: ->
@@ -180,6 +182,17 @@ class LanguageMode
desiredIndentLevel
# Calculate a minimum indent level for a range of lines.
#
# startRow - The row {Number} to start at
# endRow - The row {Number} to end at
#
# Returns a {Number} of the indent level of the block of lines.
calcMinIndent: (startRow, endRow) ->
buffer = @editSession.buffer
indents = (@editSession.indentationForBufferRow(row) for row in [startRow..endRow] when buffer.lineForRow(row).trim())
Math.min(indents...)
# Indents all the rows between two buffer row numbers.
#
# startRow - The row {Number} to start at