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

Add option to ignore current line when trimming whitespace #12

Merged
merged 19 commits into from
Mar 3, 2014
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Whitespace = require './whitespace'
module.exports =
configDefaults:
removeTrailingWhitespace: true
ignoreLeadingWhitespaceOnCurrentLine: true
ensureSingleTrailingNewline: true

activate: ->
Expand Down
11 changes: 9 additions & 2 deletions lib/whitespace.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,27 @@ class Whitespace
@subscribe buffer, 'will-be-saved', =>
buffer.transact =>
if atom.config.get('whitespace.removeTrailingWhitespace')
@removeTrailingWhitespace(buffer, editor.getGrammar().scopeName)
@removeTrailingWhitespace(editor, editor.getGrammar().scopeName)

if atom.config.get('whitespace.ensureSingleTrailingNewline')
@ensureSingleTrailingNewline(buffer)

@subscribe buffer, 'destroyed', =>
@unsubscribe(buffer)

removeTrailingWhitespace: (buffer, grammarScopeName) ->
removeTrailingWhitespace: (editor, grammarScopeName) ->
buffer = editor.getBuffer()
ignoreCurLine = atom.config.get('whitespace.ignoreLeadingWhitespaceOnCurrentLine')
buffer.scan /[ \t]+$/g, ({lineText, match, replace}) ->
whitespaceRow = buffer.positionForCharacterIndex(match.index).row
cursorRow = editor.getCursor().getBufferRow()
onlyWhitespace = lineText.match(/^[ \t]+$/)
if grammarScopeName is 'source.gfm'
# GitHub Flavored Markdown permits two spaces at the end of a line
[whitespace] = match
replace('') unless whitespace is ' ' and whitespace isnt lineText
else if ignoreCurLine and onlyWhitespace and whitespaceRow is cursorRow
Copy link

Choose a reason for hiding this comment

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

How about this instead to get rid of the empty statement?

else if not (ignoreCurLine and onlyWhitespace and whitespaceRow is cursorRow)
  replace('')

# don't touch this line
else
replace('')

Expand Down
29 changes: 29 additions & 0 deletions spec/whitespace-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ describe "Whitespace", ->
editor.save()
expect(editor.getText()).toBe "don't trim me \n"

describe "whitespace.ignoreLeadingWhitespaceOnCurrentLine config", ->
[originalConfigValue] = []
beforeEach ->
originalConfigValue = atom.config.get("whitespace.ignoreLeadingWhitespaceOnCurrentLine")
expect(originalConfigValue).toBe true

afterEach ->
atom.config.set("whitespace.ignoreLeadingWhitespaceOnCurrentLine", originalConfigValue)

it "doesn't trim whitespace on current line", ->
editor.insertText " "
editor.setCursorBufferPosition([0,2])
editor.save()
expect(editor.getText()).toBe " \n"

it "does trim whitespace on other lines", ->
editor.insertText " \n "
editor.setCursorBufferPosition([0,2])
editor.save()
expect(editor.getText()).toBe " \n"

it "trims whitespace on current line if ignoreLeadingWhitespaceOnCurrentLine is false", ->
atom.config.set("whitespace.ignoreLeadingWhitespaceOnCurrentLine", false)

editor.insertText " "
editor.setCursorBufferPosition([0,2])
editor.save()
expect(editor.getText()).toBe "\n"

describe "whitespace.ensureSingleTrailingNewline config", ->
[originalConfigValue] = []
beforeEach ->
Expand Down