mirror of
https://github.com/atom/atom.git
synced 2026-01-24 22:38:20 -05:00
Merge pull request #15799 from atom/mb-fix-commenting-empty-lines
Preserve indentation when toggling comments on whitespace-only lines
This commit is contained in:
@@ -807,7 +807,7 @@ describe('TokenizedBuffer', () => {
|
||||
|
||||
buffer.setText(' ')
|
||||
tokenizedBuffer.toggleLineCommentsForBufferRows(0, 0)
|
||||
expect(buffer.lineForRow(0)).toBe('// ')
|
||||
expect(buffer.lineForRow(0)).toBe(' // ')
|
||||
|
||||
buffer.setText(' a\n \n b')
|
||||
tokenizedBuffer.toggleLineCommentsForBufferRows(0, 2)
|
||||
|
||||
@@ -199,17 +199,20 @@ class TokenizedBuffer {
|
||||
})
|
||||
}
|
||||
} else {
|
||||
let allBlank = true
|
||||
let allBlankOrCommented = true
|
||||
|
||||
let hasCommentedLines = false
|
||||
let hasUncommentedLines = false
|
||||
for (let row = start; row <= end; row++) {
|
||||
const line = this.buffer.lineForRow(row)
|
||||
const blank = line.match(/^\s*$/)
|
||||
if (!blank) allBlank = false
|
||||
if (!blank && !commentStartRegex.testSync(line)) allBlankOrCommented = false
|
||||
if (NON_WHITESPACE_REGEX.test(line)) {
|
||||
if (commentStartRegex.testSync(line)) {
|
||||
hasCommentedLines = true
|
||||
} else {
|
||||
hasUncommentedLines = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const shouldUncomment = allBlankOrCommented && !allBlank
|
||||
const shouldUncomment = hasCommentedLines && !hasUncommentedLines
|
||||
|
||||
if (shouldUncomment) {
|
||||
for (let row = start; row <= end; row++) {
|
||||
@@ -221,15 +224,22 @@ class TokenizedBuffer {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let minIndentLevel = null
|
||||
let minIndentLevel = Infinity
|
||||
let minBlankIndentLevel = Infinity
|
||||
for (let row = start; row <= end; row++) {
|
||||
const line = this.buffer.lineForRow(row)
|
||||
const indentLevel = this.indentLevelForLine(line)
|
||||
if (NON_WHITESPACE_REGEX.test(line)) {
|
||||
const indentLevel = this.indentLevelForLine(line)
|
||||
if (minIndentLevel == null || indentLevel < minIndentLevel) minIndentLevel = indentLevel
|
||||
if (indentLevel < minIndentLevel) minIndentLevel = indentLevel
|
||||
} else {
|
||||
if (indentLevel < minBlankIndentLevel) minBlankIndentLevel = indentLevel
|
||||
}
|
||||
}
|
||||
if (minIndentLevel == null) minIndentLevel = 0
|
||||
minIndentLevel = Number.isFinite(minIndentLevel)
|
||||
? minIndentLevel
|
||||
: Number.isFinite(minBlankIndentLevel)
|
||||
? minBlankIndentLevel
|
||||
: 0
|
||||
|
||||
const tabLength = this.getTabLength()
|
||||
const indentString = ' '.repeat(tabLength * minIndentLevel)
|
||||
|
||||
Reference in New Issue
Block a user