Skip to content

Commit

Permalink
Implement the whitespace.suppressor service
Browse files Browse the repository at this point in the history
This service allows packages to suppress the automatic transformations
of the whitespace package. This is useful for packages that implement or
want to control such functionality themselves.

Fixes atom#160
  • Loading branch information
segevfiner committed Jul 20, 2017
1 parent e6815fa commit 414a3d6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ export default {
deactivate () {
if (this.whitespace) this.whitespace.destroy()
this.whitespace = null
},

consumeSuppressor (suppressor) {
return this.whitespace.addSuppressor(suppressor)
}
}
39 changes: 35 additions & 4 deletions lib/whitespace.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/** @babel */

import {CompositeDisposable, Point, Range} from 'atom'
import {Disposable, CompositeDisposable, Point, Range} from 'atom'

const TRAILING_WHITESPACE_REGEX = /[ \t]+(?=\r?$)/g

export default class Whitespace {
constructor () {
this.suppressors = []
this.subscriptions = new CompositeDisposable()

this.subscriptions.add(atom.workspace.observeTextEditors(editor => {
Expand Down Expand Up @@ -70,6 +71,35 @@ export default class Whitespace {
return this.subscriptions.dispose()
}

addSuppressor (suppressor) {
this.suppressors.push(suppressor)
return new Disposable(() => {
this.suppressors.splice(this.suppressors.findIndex(
x => x === suppressor
), 1)
})
}

shouldSuppressRemoveTrailingWhitespace (editor) {
for (let i = 0, n = this.suppressors.length; i < n; i++) {
if (this.suppressors[i].shouldSuppressRemoveTrailingWhitespace(editor)) {
return true;
}
}

return false;
}

shouldSuppressEnsureSingleTrailingNewline (editor) {
for (let i = 0, n = this.suppressors.length; i < n; i++) {
if (this.suppressors[i].shouldSuppressEnsureSingleTrailingNewline(editor)) {
return true;
}
}

return false;
}

handleEvents (editor) {
let buffer = editor.getBuffer()

Expand All @@ -79,11 +109,12 @@ export default class Whitespace {

if (atom.config.get('whitespace.removeTrailingWhitespace', {
scope: scopeDescriptor
}) && !this.ignore) {
}) && !this.ignore && !this.shouldSuppressRemoveTrailingWhitespace(editor)) {
this.removeTrailingWhitespace(editor, editor.getGrammar().scopeName)
}

if (atom.config.get('whitespace.ensureSingleTrailingNewline', {scope: scopeDescriptor})) {
if (atom.config.get('whitespace.ensureSingleTrailingNewline', {scope: scopeDescriptor}) &&
!this.shouldSuppressEnsureSingleTrailingNewline(editor)) {
return this.ensureSingleTrailingNewline(editor)
}
})
Expand All @@ -102,7 +133,7 @@ export default class Whitespace {

if (atom.config.get('whitespace.removeTrailingWhitespace', {
scope: scopeDescriptor
})) {
}) && !this.shouldSuppressRemoveTrailingWhitespace(editor)) {
if (!atom.config.get('whitespace.ignoreWhitespaceOnlyLines', {
scope: scopeDescriptor
})) {
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
"waitsForPromise"
]
},
"consumedServices": {
"whitespace.suppressor": {
"versions": {
"^1.0.0": "consumeSuppressor"
}
}
},
"configSchema": {
"removeTrailingWhitespace": {
"type": "boolean",
Expand Down

0 comments on commit 414a3d6

Please sign in to comment.