Merge pull request #511 from github/no-trim-whitespace

Leave Markdown files alone when trimming whitespace
This commit is contained in:
Garen Torikian
2013-04-23 17:22:37 -07:00
3 changed files with 32 additions and 2 deletions

View File

@@ -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 = []

View File

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

View File

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