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

Commit 2afee81

Browse files
authored
Merge pull request #125 from atom/wl-save-commands
Add commands to save with/without whitespace
2 parents 80c91f3 + b670988 commit 2afee81

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

lib/whitespace.coffee

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ class Whitespace
1111
'whitespace:remove-trailing-whitespace': =>
1212
if editor = atom.workspace.getActiveTextEditor()
1313
@removeTrailingWhitespace(editor, editor.getGrammar().scopeName)
14+
'whitespace:save-with-trailing-whitespace': =>
15+
if editor = atom.workspace.getActiveTextEditor()
16+
@ignore = true
17+
editor.save()
18+
@ignore = false
19+
'whitespace:save-without-trailing-whitespace': =>
20+
if editor = atom.workspace.getActiveTextEditor()
21+
@removeTrailingWhitespace(editor, editor.getGrammar().scopeName)
22+
editor.save()
1423
'whitespace:convert-tabs-to-spaces': =>
1524
if editor = atom.workspace.getActiveTextEditor()
1625
@convertTabsToSpaces(editor)
@@ -29,7 +38,7 @@ class Whitespace
2938
bufferSavedSubscription = buffer.onWillSave =>
3039
buffer.transact =>
3140
scopeDescriptor = editor.getRootScopeDescriptor()
32-
if atom.config.get('whitespace.removeTrailingWhitespace', scope: scopeDescriptor)
41+
if atom.config.get('whitespace.removeTrailingWhitespace', scope: scopeDescriptor) and not @ignore
3342
@removeTrailingWhitespace(editor, editor.getGrammar().scopeName)
3443
if atom.config.get('whitespace.ensureSingleTrailingNewline', scope: scopeDescriptor)
3544
@ensureSingleTrailingNewline(editor)

menus/whitespace.cson

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
'label': 'Whitespace'
66
'submenu': [
77
{ 'label': 'Remove Trailing Whitespace', 'command': 'whitespace:remove-trailing-whitespace' }
8+
{ 'label': 'Save With Trailing Whitespace', 'command': 'whitespace:save-with-trailing-whitespace' }
9+
{ 'label': 'Save Without Trailing Whitespace', 'command': 'whitespace:save-without-trailing-whitespace' }
810
{ 'label': 'Convert Tabs To Spaces', 'command': 'whitespace:convert-tabs-to-spaces' }
911
{ 'label': 'Convert Spaces To Tabs', 'command': 'whitespace:convert-spaces-to-tabs' }
1012
{ 'label': 'Convert All Tabs To Spaces', 'command': 'whitespace:convert-all-tabs-to-spaces' }

spec/whitespace-spec.coffee

+22
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,28 @@ describe "Whitespace", ->
345345
atom.packages.deactivatePackage 'whitespace'
346346
expect(buffer.getText()).toBe "foo \nbar\t \n\nbaz"
347347

348+
describe "when the 'whitespace:save-with-trailing-whitespace' command is run", ->
349+
beforeEach ->
350+
atom.config.set("whitespace.removeTrailingWhitespace", true)
351+
atom.config.set("whitespace.ensureSingleTrailingNewline", false)
352+
buffer.setText("foo \nbar\t \n\nbaz")
353+
354+
it "saves the file without removing any trailing whitespace", ->
355+
atom.commands.dispatch(workspaceElement, 'whitespace:save-with-trailing-whitespace')
356+
expect(buffer.getText()).toBe "foo \nbar\t \n\nbaz"
357+
expect(buffer.isModified()).toBe false
358+
359+
describe "when the 'whitespace:save-without-trailing-whitespace' command is run", ->
360+
beforeEach ->
361+
atom.config.set("whitespace.removeTrailingWhitespace", false)
362+
atom.config.set("whitespace.ensureSingleTrailingNewline", false)
363+
buffer.setText("foo \nbar\t \n\nbaz")
364+
365+
it "saves the file and removes any trailing whitespace", ->
366+
atom.commands.dispatch(workspaceElement, 'whitespace:save-without-trailing-whitespace')
367+
expect(buffer.getText()).toBe "foo\nbar\n\nbaz"
368+
expect(buffer.isModified()).toBe false
369+
348370
describe "when the 'whitespace:convert-tabs-to-spaces' command is run", ->
349371
it "removes leading \\t characters and replaces them with spaces using the configured tab length", ->
350372
editor.setTabLength(2)

0 commit comments

Comments
 (0)