From a2c9c21cfbe3f8f7bab6bc026144cecc63137bdf Mon Sep 17 00:00:00 2001 From: Lee Dohm Date: Mon, 9 Jun 2014 21:31:46 -0700 Subject: [PATCH] Change line commenting to ignore blanks if any non-blanks Fixes #2526 This slightly alters the heuristic for deciding whether to uncomment or to comment blocks of code. Previously, it would key off the first line of code and only if that was commented would it check the other lines. Now it checks all lines of code and uncomments the block if all non-blank lines are commented out. --- src/language-mode.coffee | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/language-mode.coffee b/src/language-mode.coffee index 83d50a74b..fad18d645 100644 --- a/src/language-mode.coffee +++ b/src/language-mode.coffee @@ -41,9 +41,9 @@ class LanguageMode buffer = @editor.buffer commentStartRegexString = _.escapeRegExp(commentStartString).replace(/(\s+)$/, '(?:$1)?') commentStartRegex = new OnigRegExp("^(\\s*)(#{commentStartRegexString})") - shouldUncomment = commentStartRegex.test(buffer.lineForRow(start)) if commentEndString + shouldUncomment = commentStartRegex.test(buffer.lineForRow(start)) if shouldUncomment commentEndRegexString = _.escapeRegExp(commentEndString).replace(/^(\s+)/, '(?:$1)?') commentEndRegex = new OnigRegExp("(#{commentEndRegexString})(\\s*)$") @@ -64,10 +64,18 @@ class LanguageMode buffer.insert([start, indentLength], commentStartString) buffer.insert([end, buffer.lineLengthForRow(end)], commentEndString) else - if shouldUncomment and start isnt end - shouldUncomment = [start+1..end].every (row) -> - line = buffer.lineForRow(row) - not line or commentStartRegex.test(line) + blankRegex = new OnigRegExp("^\\s*$") + + allBlank = true + allBlankOrCommented = [start..end].every (row) -> + line = buffer.lineForRow(row) + blank = not line or blankRegex.test(line) + + allBlank = allBlank and blank + blank or commentStartRegex.test(line) + + shouldUncomment = allBlankOrCommented and not allBlank + if shouldUncomment for row in [start..end] if match = commentStartRegex.search(buffer.lineForRow(row))