-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
230 lines (192 loc) · 9.89 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright (c) 2013 Peter Flynn, Adobe Systems Incorporated, and other contributors.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4 */
/*global define, brackets, $ */
define(function (require, exports, module) {
"use strict";
// Brackets modules
var _ = brackets.getModule("thirdparty/lodash"),
DocumentManager = brackets.getModule("document/DocumentManager"),
MainViewManager = brackets.getModule("view/MainViewManager"),
ProjectManager = brackets.getModule("project/ProjectManager"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
StatusBar = brackets.getModule("widgets/StatusBar"),
Async = brackets.getModule("utils/Async"),
Dialogs = brackets.getModule("widgets/Dialogs"),
Menus = brackets.getModule("command/Menus"),
CommandManager = brackets.getModule("command/CommandManager");
// Extension modules
var Counter = require("Counter");
var prefs = PreferencesManager.getExtensionPrefs("pflynn.sloc-counter");
prefs.definePreference("exclusions", "Array", []);
/**
* Want to use the current project's settings even if user happens to have a file from outside the project open. Just passing
* CURRENT_PROJECT should be enough, but it's not - https://github.com/adobe/brackets/pull/10422#issuecomment-73654748
*/
function projPrefsContext() {
var context = _.cloneDeep(PreferencesManager.CURRENT_PROJECT);
context.path = ProjectManager.getProjectRoot().fullPath;
return context;
}
/* E.g., for Brackets core-team-owned source:
/extensions/dev/ (want to exclude any personal code...)
/thirdparty/
/3rdparty/
/node_modules/
/widgets/bootstrap-
/unittest-files/
/spec/JSUtils-test-files/
/perf/OpenFile-perf-files/
For Brackets runtime source:
/extensions/dev/
/node_modules/ (although leave out to include the Brackets-node process too)
/unittest-files/
/brackets/test/
/unittests.js
/unittest-files/
/test/
*/
var filterStrings = [];
function filter(file) {
var path = file.fullPath;
var i;
for (i = 0; i < filterStrings.length; i++) {
if (path.indexOf(filterStrings[i]) !== -1) {
return false;
}
}
return true;
}
/**
* Finds all JS files, loads all those that pass the filter(), counts lines, and shows final
* result in a dialog.
*/
function countAllFiles() {
var totalLines = 0,
totalSloc = 0,
totalBytes = 0,
totalFiles = 0;
var warnings = [];
// Code based on FileIndexManager & a bit of JSUtils
StatusBar.showBusyIndicator(true);
ProjectManager.getAllFiles()
.done(function (fileListResult) {
var jsFiles = fileListResult.filter(function (file) {
return (/\.js$/i).test(file.fullPath);
});
Async.doInParallel(jsFiles, function (file) {
var result = new $.Deferred();
if (!filter(file)) {
result.resolve();
} else {
// Search one file
DocumentManager.getDocumentForPath(file.fullPath)
.done(function (doc) {
var text = doc.getText();
try {
var lineCounts = Counter.countSloc(text);
totalLines += lineCounts.total;
totalSloc += lineCounts.sloc;
totalBytes += text.length;
totalFiles++;
} catch (err) {
if (err instanceof Counter.Unsupported) {
warnings.push({ reason: err.message, fullPath: file.fullPath, lineNum: err.lineNum });
} else {
var wrap = new Error("Rethrowing: " + err.message);
wrap.innerException = err;
throw wrap;
}
}
result.resolve();
})
.fail(function (error) {
// Error reading this file
// Resolve anyway so we can still do a partial count
warnings.push({ reason: "Unable to read file", fullPath: file.fullPath });
result.resolve();
});
}
return result.promise();
})
.always(function () {
StatusBar.hideBusyIndicator();
})
.done(function () {
// Done processing all files: show results
var totalKb = Math.round(totalBytes / 1024);
var message = "<div style='-webkit-user-select:text; cursor: auto'>";
message +=
"Scanned " + totalFiles + " .js files (" + totalKb + " KB).<br>" +
"Raw total lines: " + totalLines + "<br>" +
"<b>Lines of code: " + totalSloc + "</b> (excluding whitespace & comments)";
if (warnings.length) {
message += "<div style='border:1px solid #dfb200; background-color: #fffad8; margin-top:20px; padding:10px; max-height:250px; overflow:auto'>";
warnings.forEach(function (warning) {
message += "Ignored '" + warning.fullPath + "': " + warning.reason + " at line " + (warning.lineNum + 1) + "<br>";
});
message += "</div>";
}
message += "</div>";
Dialogs.showModalDialog(Dialogs.DIALOG_ID_ERROR, "JavaScript Lines of Code", message)
.done(function () { MainViewManager.focusActivePane(); });
});
});
}
function getExclusions() {
var $textarea;
var message = "Exclude files/folders containing any of these substrings (one per line):<br><textarea id='sloc-excludes' style='width:400px;height:160px'></textarea>";
var promise = Dialogs.showModalDialog(Dialogs.DIALOG_ID_ERROR, "JavaScript Lines of Code", message);
promise.done(function (btnId) {
if (btnId === Dialogs.DIALOG_BTN_OK) { // as opposed to dialog's "X" button
var substrings = $textarea.val();
filterStrings = substrings.split("\n");
filterStrings = filterStrings.map(function (substr) {
return substr.trim();
}).filter(function (substr) {
return substr !== "";
});
// Save to project-specific prefs if setting exists there; else global prefs
prefs.set("exclusions", filterStrings, {context: projPrefsContext()});
}
});
// store now since it'll be orphaned by the time done() handler runs
$textarea = $("#sloc-excludes");
// prepopulate with last-used filter within session
$textarea.val(prefs.get("exclusions", projPrefsContext()).join("\n"));
$textarea.focus();
return promise;
}
function beginCount() {
getExclusions().done(function (btnId) {
if (btnId !== Dialogs.DIALOG_BTN_OK) { // i.e. dialog's "X" button
return;
}
countAllFiles();
});
}
// Register command
var COMMAND_ID = "pflynn.count_sloc";
CommandManager.register("Count Lines of Code", COMMAND_ID, beginCount);
var menu = Menus.getMenu(Menus.AppMenuBar.VIEW_MENU);
menu.addMenuItem(COMMAND_ID, null, Menus.LAST);
});