Measure indent prefix on each line being commented

Previously a single indent string was used to replace all lines
which could corrupt lines using hard tabs since the indent string
used for replacement was two spaces causing the first character
of the line to be deleted.

Closes atom/language-c#1
This commit is contained in:
Kevin Sawicki
2013-10-24 15:43:57 -07:00
parent 7a3dc2c3b8
commit 05c3d89381
2 changed files with 11 additions and 1 deletions

View File

@@ -32,6 +32,10 @@ describe "LanguageMode", ->
expect(buffer.lineForRow(6)).toBe " // current < pivot ? left.push(current) : right.push(current);"
expect(buffer.lineForRow(7)).toBe " // }"
buffer.setText('\tvar i;')
languageMode.toggleLineCommentsForBufferRows(0, 0)
expect(buffer.lineForRow(0)).toBe "\t// var i;"
describe "fold suggestion", ->
describe ".doesBufferRowStartFold(bufferRow)", ->
it "returns true only when the buffer row starts a foldable region", ->

View File

@@ -84,8 +84,14 @@ class LanguageMode
else
indent = @minIndentLevelForRowRange(start, end)
indentString = @editSession.buildIndentString(indent)
tabLength = @editSession.getTabLength()
indentRegex = new RegExp("(\t|[ ]{#{tabLength}}){#{Math.floor(indent)}}")
for row in [start..end]
buffer.change([[row, 0], [row, indentString.length]], indentString + commentStartString)
line = buffer.lineForRow(row)
if indentLength = line.match(indentRegex)?[0].length
buffer.insert([row, indentLength], commentStartString)
else
buffer.change([[row, 0], [row, indentString.length]], indentString + commentStartString)
# Folds all the foldable lines in the buffer.
foldAll: ->