Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Move special Markdown line break whitespace handling to configuration #82

Merged
merged 3 commits into from
Jul 30, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports =
scopes:
'.source.jade':
default: false
ignoreMarkdownLineBreak:
type: 'boolean'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs a description or an improved name, it would take me awhile to think about whether checked or unchecked will trim trailing whitespace in markdown files since "ignoring the line break" means removing it in the context of this package.

Maybe something like:

  • keepTrailingTwoSpacesInMarkdownFiles with a description like Markdown treats two spaces at the end of a line as a line break

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the name to keepMarkdownLineBreakWhitespace and added the following description:

Markdown uses two or more spaces at the end of a line to signify a line break. Enable this option to keep this whitespace, even if other settings would remove it.

default: true
ignoreWhitespaceOnCurrentLine:
type: 'boolean'
default: true
Expand Down
6 changes: 3 additions & 3 deletions lib/whitespace.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class Whitespace
[whitespace] = match
return if ignoreWhitespaceOnlyLines and whitespace is lineText

if grammarScopeName is 'source.gfm'
# GitHub Flavored Markdown permits two spaces at the end of a line
replace('') unless whitespace is ' ' and whitespace isnt lineText
if grammarScopeName is 'source.gfm' and atom.config.get('whitespace.ignoreMarkdownLineBreak')
# GitHub Flavored Markdown permits two or more spaces at the end of a line
replace('') unless whitespace.length >= 2 and whitespace isnt lineText
else
replace('')

Expand Down
124 changes: 88 additions & 36 deletions spec/whitespace-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -182,53 +182,105 @@ describe "Whitespace", ->
expect(editor.getText()).toBe "no trailing newline"

describe "GFM whitespace trimming", ->
beforeEach ->
atom.config.set("whitespace.ignoreWhitespaceOnCurrentLine", false)
describe 'when ignoreMarkdownLineBreak is true', ->
beforeEach ->
atom.config.set("whitespace.ignoreWhitespaceOnCurrentLine", false)
atom.config.set("whitespace.ignoreMarkdownLineBreak", true)

waitsForPromise ->
atom.packages.activatePackage("language-gfm")
waitsForPromise ->
atom.packages.activatePackage("language-gfm")

runs ->
editor.setGrammar(atom.grammars.grammarForScopeName("source.gfm"))
runs ->
editor.setGrammar(atom.grammars.grammarForScopeName("source.gfm"))

it "trims GFM text with a single space", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo\nline break!\n"
it "trims GFM text with a single space", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo\nline break!\n"

it "leaves GFM text with double spaces alone", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo \nline break!\n"
it "leaves GFM text with double spaces alone", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo \nline break!\n"

it "trims GFM text with a more than two spaces", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo\nline break!\n"
it "leaves GFM text with a more than two spaces", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo \nline break!\n"

it "trims empty lines", ->
editor.insertText "foo\n "
editor.save()
expect(editor.getText()).toBe "foo\n"
it "trims empty lines", ->
editor.insertText "foo\n "
editor.save()
expect(editor.getText()).toBe "foo\n"

editor.setText "foo\n "
editor.save()
expect(editor.getText()).toBe "foo\n"
editor.setText "foo\n "
editor.save()
expect(editor.getText()).toBe "foo\n"

it "respects 'whitespace.ignoreWhitespaceOnCurrentLine' setting", ->
atom.config.set("whitespace.ignoreWhitespaceOnCurrentLine", true)
it "respects 'whitespace.ignoreWhitespaceOnCurrentLine' setting", ->
atom.config.set("whitespace.ignoreWhitespaceOnCurrentLine", true)

editor.insertText "foo \nline break!"
editor.setCursorBufferPosition([0, 4])
editor.save()
expect(editor.getText()).toBe "foo \nline break!\n"
editor.insertText "foo \nline break!"
editor.setCursorBufferPosition([0, 4])
editor.save()
expect(editor.getText()).toBe "foo \nline break!\n"

it "respects 'whitespace.ignoreWhitespaceOnlyLines' setting", ->
atom.config.set("whitespace.ignoreWhitespaceOnlyLines", true)
it "respects 'whitespace.ignoreWhitespaceOnlyLines' setting", ->
atom.config.set("whitespace.ignoreWhitespaceOnlyLines", true)

editor.insertText "\t \nline break!"
editor.save()
expect(editor.getText()).toBe "\t \nline break!\n"
editor.insertText "\t \nline break!"
editor.save()
expect(editor.getText()).toBe "\t \nline break!\n"

describe 'when ignoreMarkdownLineBreak is false', ->
beforeEach ->
atom.config.set("whitespace.ignoreWhitespaceOnCurrentLine", false)
atom.config.set("whitespace.ignoreMarkdownLineBreak", false)

waitsForPromise ->
atom.packages.activatePackage("language-gfm")

runs ->
editor.setGrammar(atom.grammars.grammarForScopeName("source.gfm"))

it "trims GFM text with a single space", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo\nline break!\n"

it "trims GFM text with two spaces", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo\nline break!\n"

it "trims GFM text with a more than two spaces", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo\nline break!\n"

it "trims empty lines", ->
editor.insertText "foo\n "
editor.save()
expect(editor.getText()).toBe "foo\n"

editor.setText "foo\n "
editor.save()
expect(editor.getText()).toBe "foo\n"

it "respects 'whitespace.ignoreWhitespaceOnCurrentLine' setting", ->
atom.config.set("whitespace.ignoreWhitespaceOnCurrentLine", true)

editor.insertText "foo \nline break!"
editor.setCursorBufferPosition([0, 4])
editor.save()
expect(editor.getText()).toBe "foo \nline break!\n"

it "respects 'whitespace.ignoreWhitespaceOnlyLines' setting", ->
atom.config.set("whitespace.ignoreWhitespaceOnlyLines", true)

editor.insertText "\t \nline break!"
editor.save()
expect(editor.getText()).toBe "\t \nline break!\n"

describe "when the editor is split", ->
it "does not throw exceptions when the editor is saved after the split is closed (regression)", ->
Expand Down