Skip to content

Commit 04c2365

Browse files
author
Miguel Castillo
committed
refactored where preferences are read and added 'onopen' preferences to enable/disable sanitizing files when they are first open
1 parent 8033b43 commit 04c2365

File tree

6 files changed

+104
-45
lines changed

6 files changed

+104
-45
lines changed

.eslintrc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es6":true,
5+
"mocha":true
6+
},
7+
"ecmaFeatures": {
8+
"modules": true,
9+
"jsx": true
10+
},
11+
"rules": {
12+
"semi": 2,
13+
"global-strict": 0,
14+
"quotes": [2, "single"],
15+
"key-spacing": 0,
16+
"no-trailing-spaces": 0,
17+
"eol-last": 0
18+
}
19+
}

.jshintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"window": true,
1313
"_": true,
1414
"brackets": true,
15-
"CodeMirror": true
15+
"CodeMirror": true,
16+
"setTimeout": true
1617
}
1718
}

main.js

+41-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
* Based on https://github.com/dsbonev/whitespace-normalizer
77
*/
88

9-
define(function (require /*, exports, module*/) {
9+
define(function (require) {
1010
'use strict';
1111

12+
var AppInit = brackets.getModule("utils/AppInit");
13+
var EditorManager = brackets.getModule("editor/EditorManager");
1214
var Commands = brackets.getModule('command/Commands');
1315
var CommandManager = brackets.getModule('command/CommandManager');
1416
var DocumentManager = brackets.getModule('document/DocumentManager');
@@ -18,6 +20,11 @@ define(function (require /*, exports, module*/) {
1820
var PREFERENCES_KEY = 'brackets-wsSanitizer';
1921
var prefs = PreferencesManager.getExtensionPrefs(PREFERENCES_KEY);
2022

23+
var SPACE_UNITS = "spaceUnits";
24+
var TAB_SIZE = "tabSize";
25+
var USE_TAB_CHAR = "useTabChar";
26+
27+
2128
var COMMAND_ID = PREFERENCES_KEY;
2229
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
2330
var command = CommandManager.register('Whitespace Sanitizer', COMMAND_ID, cmdToggleEnabled);
@@ -26,7 +33,8 @@ define(function (require /*, exports, module*/) {
2633
menu.addMenuItem(COMMAND_ID);
2734

2835
// Wire up preferences system
29-
prefs.definePreference("enabled", "boolean", "true").on("change", checkEnabled);
36+
prefs.definePreference("onopen", "boolean", false);
37+
prefs.definePreference("enabled", "boolean", true).on("change", checkEnabled);
3038
command.setChecked(prefs.get('enabled'));
3139
checkEnabled();
3240

@@ -51,7 +59,8 @@ define(function (require /*, exports, module*/) {
5159

5260
doc.__saving = true;
5361
doc.batchOperation(function () {
54-
sanitize(doc);
62+
var settings = getPreferences(doc);
63+
sanitize(doc, settings.useTabChar, settings.size);
5564

5665
setTimeout(function() {
5766
CommandManager.execute(Commands.FILE_SAVE, {doc: doc})
@@ -61,4 +70,33 @@ define(function (require /*, exports, module*/) {
6170
});
6271
});
6372
}
73+
74+
75+
function setDocument(evt, editor) {
76+
if (editor && prefs.get("onopen") === true) {
77+
var doc = editor.document;
78+
doc.batchOperation(function () {
79+
var settings = getPreferences(doc);
80+
sanitize(doc, settings.useTabChar, settings.size);
81+
});
82+
}
83+
}
84+
85+
86+
function getPreferences(doc) {
87+
var preferencesContext = doc.file.fullPath;
88+
var useTabChar = PreferencesManager.get(USE_TAB_CHAR, preferencesContext);
89+
var tabSize = PreferencesManager.get(TAB_SIZE, preferencesContext);
90+
var spaceUnit = PreferencesManager.get(SPACE_UNITS, preferencesContext);
91+
return {
92+
useTabChar: useTabChar,
93+
size: useTabChar ? tabSize : spaceUnit
94+
};
95+
}
96+
97+
98+
AppInit.appReady(function() {
99+
EditorManager.on("activeEditorChange.wsSanitizer", setDocument);
100+
setDocument(null, EditorManager.getActiveEditor());
101+
});
64102
});

src/sanitize.js

+31-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
define(function (require) {
22
'use strict';
33

4-
var PreferencesManager = brackets.getModule('preferences/PreferencesManager');
5-
var SPACE_UNITS = "spaceUnits";
6-
var TAB_SIZE = "tabSize";
7-
var USE_TAB_CHAR = "useTabChar";
8-
var getReplacePattern = require('./replacePatterns');
4+
var getReplacePattern = require('./replacePatterns');
95

10-
function sanitize(doc) {
6+
function sanitize(doc, useTab, units) {
117
var line, pattern, match;
12-
var lineIndex = 0;
13-
var preferencesContext = doc.file.fullPath;
14-
var useTabChar = PreferencesManager.get(USE_TAB_CHAR, preferencesContext);
15-
var tabSize = PreferencesManager.get(TAB_SIZE, preferencesContext);
16-
var spaceUnit = PreferencesManager.get(SPACE_UNITS, preferencesContext);
17-
var wsPattern = getReplacePattern(useTabChar, useTabChar ? tabSize : spaceUnit);
8+
var lineIndex = 0;
9+
var wsPattern = getReplacePattern(useTab, units);
1810

1911
while ((line = doc.getLine(lineIndex)) !== undefined) {
2012
//trim trailing whitespaces
@@ -41,7 +33,7 @@ define(function (require) {
4133
});
4234
}
4335

44-
lineIndex += 1;
36+
lineIndex++;
4537
}
4638

4739
//ensure newline at the end of file
@@ -55,5 +47,31 @@ define(function (require) {
5547
}
5648
}
5749

50+
51+
sanitize.verify = function(doc, useTab, units) {
52+
var line, pattern;
53+
var lineIndex = 0;
54+
var wsPattern = getReplacePattern(useTab, units);
55+
56+
while ((line = doc.getLine(lineIndex)) !== undefined) {
57+
pattern = /[ \t]+$/g;
58+
59+
if ((!!pattern.exec(line)) || (!!wsPattern.exec(line).replaceWith)) {
60+
return false;
61+
}
62+
63+
lineIndex++;
64+
}
65+
66+
//ensure newline at the end of file
67+
line = doc.getLine(lineIndex - 1);
68+
var lastN = line.slice(-1);
69+
if (line !== undefined && line.length > 0 && lastN !== '\n') {
70+
return false;
71+
}
72+
73+
return true;
74+
};
75+
5876
return sanitize;
5977
});

test/config.js

+4-21
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,12 @@ require = (function() {
1010
"mocha": {
1111
"exports": "mocha"
1212
}
13-
},
14-
"transforms": [{
15-
name: "cantransform",
16-
handler: cantransform,
17-
ignore:["chai"]
18-
}]
13+
}
1914
});
2015

21-
/**
22-
* Simple filter for excluding particular modules from being processed by the transformation pipeline.
23-
*/
24-
function cantransform(moduleMeta) {
25-
var ignoreList = this.ignore;
26-
var i, length;
27-
28-
if (ignoreList && ignoreList.length) {
29-
for (i = 0, length = ignoreList.length; i < length; i++) {
30-
if (ignoreList[i].indexOf(moduleMeta.name) !== -1) {
31-
return false;
32-
}
33-
}
34-
}
35-
}
16+
importer.ignore({
17+
match: ["chai"]
18+
});
3619

3720
return importer.require;
3821
})();

test/spec/replacePatterns.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe("Pattern Suites", function() {
1313
beforeEach(function() {
1414
result = pattern.getIndentation(2, 1);
1515
});
16-
16+
1717
it("then result is 2 tabs", function() {
1818
expect(result).to.equal('\t\t');
1919
});
@@ -24,7 +24,7 @@ describe("Pattern Suites", function() {
2424
beforeEach(function() {
2525
result = pattern.getIndentation(3, 1);
2626
});
27-
27+
2828
it("then result is 2 tabs and 1 space", function() {
2929
expect(result).to.equal('\t\t ');
3030
});
@@ -35,7 +35,7 @@ describe("Pattern Suites", function() {
3535
beforeEach(function() {
3636
result = pattern.getIndentation(4, 1);
3737
});
38-
38+
3939
it("then result is 3 tabs", function() {
4040
expect(result).to.equal('\t\t\t');
4141
});
@@ -46,7 +46,7 @@ describe("Pattern Suites", function() {
4646
beforeEach(function() {
4747
result = pattern.getIndentation(5, 1);
4848
});
49-
49+
5050
it("then result is 3 tabs and 1 space", function() {
5151
expect(result).to.equal('\t\t\t ');
5252
});
@@ -64,7 +64,7 @@ describe("Pattern Suites", function() {
6464
beforeEach(function() {
6565
result = pattern.getIndentation(2, 1);
6666
});
67-
67+
6868
it("then result is 1 tab and 2 spaces", function() {
6969
expect(result).to.equal('\t ');
7070
});
@@ -75,7 +75,7 @@ describe("Pattern Suites", function() {
7575
beforeEach(function() {
7676
result = pattern.getIndentation(3, 1);
7777
});
78-
78+
7979
it("then result is 1 tab and 3 spaces", function() {
8080
expect(result).to.equal('\t ');
8181
});
@@ -86,7 +86,7 @@ describe("Pattern Suites", function() {
8686
beforeEach(function() {
8787
result = pattern.getIndentation(5, 1);
8888
});
89-
89+
9090
it("then result is 2 tab and 1 spaces", function() {
9191
expect(result).to.equal('\t\t ');
9292
});

0 commit comments

Comments
 (0)