From 4975f659c0932a96f931acf063655f1bdb2861ee Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 3 Oct 2017 11:03:37 -0700 Subject: [PATCH] :art: toggleLineCommentsForBufferRows --- src/tokenized-buffer.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/tokenized-buffer.js b/src/tokenized-buffer.js index 8b7569cca..4eed4110a 100644 --- a/src/tokenized-buffer.js +++ b/src/tokenized-buffer.js @@ -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,20 +224,22 @@ class TokenizedBuffer { } } } else { - let minIndentLevel = null - let minBlankIndentLevel + 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 - } else if (minIndentLevel == null) { - const indentLevel = this.indentLevelForLine(line) - if (minBlankIndentLevel == null || indentLevel < minBlankIndentLevel) minBlankIndentLevel = indentLevel + if (indentLevel < minIndentLevel) minIndentLevel = indentLevel + } else { + if (indentLevel < minBlankIndentLevel) minBlankIndentLevel = indentLevel } } - if (minIndentLevel == null) minIndentLevel = minBlankIndentLevel - if (minIndentLevel == null) minIndentLevel = 0 + minIndentLevel = Number.isFinite(minIndentLevel) + ? minIndentLevel + : Number.isFinite(minBlankIndentLevel) + ? minBlankIndentLevel + : 0 const tabLength = this.getTabLength() const indentString = ' '.repeat(tabLength * minIndentLevel)