From 8aa2c40bbfdf2706ce101b58ceaaa77a93dbf177 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 18 Mar 2014 18:19:59 -0700 Subject: [PATCH] Don't capture whitespace group in comment patterns This was causing corruption when uncommenting XML since the fact that it was being captured was throwing off the index used into the match. Closes atom/language-xml#3 --- spec/language-mode-spec.coffee | 14 ++++++++++++++ src/language-mode.coffee | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/spec/language-mode-spec.coffee b/spec/language-mode-spec.coffee index 35f143636..51cb7c36c 100644 --- a/spec/language-mode-spec.coffee +++ b/spec/language-mode-spec.coffee @@ -208,6 +208,20 @@ describe "LanguageMode", -> languageMode.toggleLineCommentsForBufferRows(0, 0) expect(buffer.lineForRow(0)).toBe "// @color: #4D926F;" + describe "xml", -> + beforeEach -> + editor = atom.project.openSync('sample.xml', autoIndent: false) + editor.setText("") + {buffer, languageMode} = editor + + waitsForPromise -> + atom.packages.activatePackage('language-xml') + + describe "when uncommenting lines", -> + it "removes the leading whitespace from the comment end pattern match", -> + languageMode.toggleLineCommentsForBufferRows(0, 0) + expect(buffer.lineForRow(0)).toBe "test" + describe "folding", -> beforeEach -> editor = atom.project.openSync('sample.js', autoIndent: false) diff --git a/src/language-mode.coffee b/src/language-mode.coffee index 0d7880cfe..093995947 100644 --- a/src/language-mode.coffee +++ b/src/language-mode.coffee @@ -39,13 +39,13 @@ class LanguageMode return unless commentStartString buffer = @editor.buffer - commentStartRegexString = _.escapeRegExp(commentStartString).replace(/(\s+)$/, '($1)?') + commentStartRegexString = _.escapeRegExp(commentStartString).replace(/(\s+)$/, '(?:$1)?') commentStartRegex = new OnigRegExp("^(\\s*)(#{commentStartRegexString})") shouldUncomment = commentStartRegex.test(buffer.lineForRow(start)) if commentEndString if shouldUncomment - commentEndRegexString = _.escapeRegExp(commentEndString).replace(/^(\s+)/, '($1)?') + commentEndRegexString = _.escapeRegExp(commentEndString).replace(/^(\s+)/, '(?:$1)?') commentEndRegex = new OnigRegExp("(#{commentEndRegexString})(\\s*)$") startMatch = commentStartRegex.search(buffer.lineForRow(start)) endMatch = commentEndRegex.search(buffer.lineForRow(end))