diff --git a/src/app/text-buffer.coffee b/src/app/text-buffer.coffee index cc1cc1901..2a20ea1cc 100644 --- a/src/app/text-buffer.coffee +++ b/src/app/text-buffer.coffee @@ -587,7 +587,7 @@ class Buffer # startIndex - The starting row {Number} # endIndex - The ending row {Number} # - # Returns an {Array} of {RegExp}s, representing the matches + # Returns an {Array} of {RegExp}s, representing the matches. matchesInCharacterRange: (regex, startIndex, endIndex) -> text = @getText() matches = [] diff --git a/src/packages/whitespace/lib/whitespace.coffee b/src/packages/whitespace/lib/whitespace.coffee index a16f9877b..1a68b1971 100644 --- a/src/packages/whitespace/lib/whitespace.coffee +++ b/src/packages/whitespace/lib/whitespace.coffee @@ -9,7 +9,10 @@ module.exports = buffer = editSession.buffer buffer.on 'will-be-saved', -> buffer.transact -> - buffer.scan /[ \t]+$/g, ({replace}) -> replace('') + buffer.scan /[ \t]+$/g, ({match, replace}) -> + # GFM permits two whitespaces at the end of a line--trim anything else + unless editSession.getGrammar().scopeName is "source.gfm" and match[0] is " " + replace('') if config.get('whitespace.ensureSingleTrailingNewline') if buffer.getLastLine() is '' diff --git a/src/packages/whitespace/spec/whitespace-spec.coffee b/src/packages/whitespace/spec/whitespace-spec.coffee index 35fe6a8ee..ab7146846 100644 --- a/src/packages/whitespace/spec/whitespace-spec.coffee +++ b/src/packages/whitespace/spec/whitespace-spec.coffee @@ -86,3 +86,30 @@ describe "Whitespace", -> editor.getBuffer().save() expect(editor.getText()).toBe "foo\n" expect(editor.getCursorBufferPosition()).toEqual([0,3]) + + describe "GFM whitespace trimming", -> + grammar = null + + beforeEach -> + spyOn(syntax, "addGrammar").andCallThrough() + atom.activatePackage("gfm") + expect(syntax.addGrammar).toHaveBeenCalled() + grammar = syntax.addGrammar.argsForCall[0][0] + + it "trims GFM text with a single space", -> + editor.activeEditSession.setGrammar(grammar) + editor.insertText "foo \nline break!" + editor.getBuffer().save() + expect(editor.getText()).toBe "foo\nline break!\n" + + it "leaves GFM text with double spaces alone", -> + editor.activeEditSession.setGrammar(grammar) + editor.insertText "foo \nline break!" + editor.getBuffer().save() + expect(editor.getText()).toBe "foo \nline break!\n" + + it "trims GFM text with a more than two spaces", -> + editor.activeEditSession.setGrammar(grammar) + editor.insertText "foo \nline break!" + editor.getBuffer().save() + expect(editor.getText()).toBe "foo\nline break!\n"