/
stripws.js
124 lines (100 loc) · 3.29 KB
/
stripws.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Strip whitespace extension for the Cloud9 IDE client
*
* Strips whitespace at the end of each line in the current buffer.
*
* @author Sergi Mansilla
* @copyright 2011, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
define(function (require, exports, module) {
var ext = require("core/ext");
var ide = require("core/ide");
var editors = require("ext/editors/editors");
var menus = require("ext/menus/menus");
var extSettings = require("ext/settings/settings");
var markupSettings = require("text!ext/stripws/settings.xml");
var commands = require("ext/commands/commands");
var settings = require("ext/settings/settings");
// Attaching to exports.module for testing purposes
var strip = module.exports.strip = function () {
if (!editors.currentEditor.amlEditor)
return;
var editor = editors.currentEditor.amlEditor.$editor;
var session = editor.getSession();
var doc = session.getDocument();
var lines = doc.getAllLines();
for (var i = 0, l=lines.length; i < l; i++) {
var line = lines[i];
var index = line.search(/\s+$/);
if (index !== -1)
doc.removeInLine(i, index, line.length);
}
session.$syncInformUndoManager();
};
module.exports = ext.register("ext/stripws/stripws", {
name: "Strip Whitespace",
dev: "Ajax.org",
alone: true,
type: ext.GENERAL,
nodes: [],
init: function () {},
hook: function () {
var self = this;
commands.addCommand({
name: "stripws",
hint: "strip whitespace at the end of each line",
isAvailable : function(editor){
return editor && editor.ceEditor;
},
exec: function(){
ext.initExtension(self);
self.stripws();
}
});
this.nodes.push(
menus.addItemByPath("Tools/Strip Whitespace", new apf.item({
command : "stripws"
}), 200)
);
ide.addEventListener("beforefilesave", function(data) {
var node =
extSettings.model.data.selectSingleNode("editors/code/@stripws");
// If the 'Strip whitespace on save' option is enabled, we strip
// whitespaces from the node value just before the file is saved.
if (node && node.firstChild && node.firstChild.nodeValue == "true") {
self.stripws();
}
});
ide.addEventListener("settings.load", function(){
settings.setDefaults("editors/code", [
["stripws", "false"]
]);
});
settings.addSettings("General", markupSettings);
},
stripws: function() {
strip();
},
enable: function () {
this.nodes.each(function(item) {
item.enable();
});
this.stripws = function() { strip(); };
},
disable: function () {
this.nodes.each(function(item) {
item.disable();
});
this.stripws = function() {};
},
destroy: function () {
menus.remove("Tools/Strip Whitespace");
commands.removeCommandByName("stripws");
this.nodes.each(function (item) {
item.destroy(true, true);
});
this.nodes = [];
}
});
});