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
This commit is contained in:
Kevin Sawicki 2014-03-18 18:19:59 -07:00
parent c2d603769e
commit 8aa2c40bbf
2 changed files with 16 additions and 2 deletions

View File

@ -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("<!-- test -->")
{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)

View File

@ -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))