-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
236 lines (211 loc) · 7.88 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
231
232
233
234
235
236
define(function(require, exports, module) {
'use strict';
var EXTENSION_ID = 'com.github.mrmeku.closure-linter',
GJSLINT_ID = 'gjslint',
FIXJSSTYLE_ID = 'fixjsstyle',
FIXJSSTYLE_ONSAVE_ID = FIXJSSTYLE_ID + '.onSave',
FIXJSSTYLE_SHORTCUT = [{
key: 'Ctrl-Shift-J',
platform: 'win'
}, {
key: 'Cmd-Shift-J',
platform: 'mac'
}],
LINT_ERROR_REGEXP = new RegExp('^Line (\\d+), ([E,W])(:[\\d]+: (.*))$'),
CONFIG_FILE_NAME = '.gjslintrc',
DEFAULT_CONFIG = {
flags: {
gjslint: '--quiet --nosummary --strict',
fixjsstyle: '--strict'
}
};
var AppInit = brackets.getModule('utils/AppInit'),
DocumentManager = brackets.getModule('document/DocumentManager'),
CommandManager = brackets.getModule('command/CommandManager'),
CodeInspection = brackets.getModule('language/CodeInspection'),
EditorManager = brackets.getModule('editor/EditorManager'),
ExtensionUtils = brackets.getModule('utils/ExtensionUtils'),
FileUtils = brackets.getModule('file/FileUtils'),
FileSystem = brackets.getModule('filesystem/FileSystem'),
Menus = brackets.getModule('command/Menus'),
NodeDomain = brackets.getModule('utils/NodeDomain'),
PreferencesManager = brackets.getModule('preferences/PreferencesManager'),
ProjectManager = brackets.getModule('project/ProjectManager');
// Access to node process with hooks to closure linter.
var closureLinter = new NodeDomain('closureLinter',
ExtensionUtils.getModulePath(module, 'node/closureLinterDomain'));
// Make DocumentManager into a jquery object for event binding.
var $DocumentManager = $(DocumentManager);
// Register commands to be made into menu items.
var fixjsstyleCommand = CommandManager.register(
'Fixjsstyle', FIXJSSTYLE_ID, fixjsstyleAsync),
fixjsstyleOnSaveCommand = CommandManager.register(
'Fixjsstyle On Save', FIXJSSTYLE_ONSAVE_ID, function() {
setFixjsstyleOnSave(!this.getChecked());
});
function _loadConfigHelper(rootPath, currentPath, deferredResult) {
var configFilePath = currentPath + CONFIG_FILE_NAME;
var file = FileSystem.getFileForPath(configFilePath);
FileUtils
.readAsText(file)
.then(
function(content) {
try {
var config = JSON.parse(content);
$.extend(DEFAULT_CONFIG, config);
deferredResult.resolve(config);
}
catch (e) {
console.error(EXTENSION_ID + ': invalid json ' + configFilePath);
deferredResult.reject(e);
}
},
function(err) {
if (rootPath === currentPath) {
deferredResult.resolve(DEFAULT_CONFIG);
}
else {
currentPath = FileUtils.getParentPath(currentPath);
_loadConfigHelper(rootPath, currentPath, deferredResult);
}
}
);
}
/**
* Loads closure-linter configuration for the specified file.
*
* The configuration file should have name .gjslint. If the specified file
* is outside the current project root, then defaultConfiguration is used.
* Otherwise, the configuration file is looked up starting from the directory
* where the specified file is located, going up to the project root,
* but no further.
*
* @param {string} fullPath Absolute path for the file linted.
*
* @return {$.Promise} Promise to return JSHint configuration object.
*
* @see <a href="http://www.jshint.com/docs/options/">JSHint option
* reference</a>.
*/
function _loadConfig(fullPath) {
var projectRoot = ProjectManager.getProjectRoot(),
deferredResult = new $.Deferred();
if (!projectRoot || !fullPath) {
return deferredResult.reject().promise();
}
var rootPath = projectRoot.fullPath,
currentPath = FileUtils.getParentPath(fullPath);
_loadConfigHelper(rootPath, currentPath, deferredResult);
return deferredResult.promise();
}
/**
* Makes an asynchronous call to gjslint on the potentially unsaved text.
* @param {string} text Text content of the potentially unsaved file.
* @param {string} fullPath Path to the potentially unsaved file being linted.
* @return {$.Promise} Promise to return an object mapping to an array of
* linting errors.
*/
function gjslintAsync(text, fullPath) {
var deferred = new $.Deferred();
_loadConfig(fullPath).then(
function(config) {
var flags = config.flags.gjslint;
closureLinter.exec('gjslint', text, fullPath, flags)
.done(function(stdout) {
deferred.resolve({errors: parseGjslintStdout(stdout)});
})
.fail(function(error, message) {
deferred.reject(error, message);
});
},
function() {
deferred.reject();
}
);
return deferred.promise();
}
/**
* Creates array of linting error objects from stdout of gjslint.
* @param {string} output Stdout produced by gjslint.
* @return {Array.<Object>} Array of brackets lint error objects.
*/
function parseGjslintStdout(stdout) {
var lines = stdout.split('\n'),
lintingErrors = [];
lines.forEach(function(line) {
var match = LINT_ERROR_REGEXP.exec(line);
if (match) {
lintingErrors.push({
pos: {line: parseInt(match[1], 10) - 1},
message: match[2] + match[3],
type: match[2] === 'E' ?
CodeInspection.Type.ERROR : CodeInspection.Type.WARNING
});
}
});
return lintingErrors;
}
/**
* Makes an asyncrhonous call to fixjsstyle on the potentially unsaved text.
* @return {$.Promise} Promise to return text with fixed style.
*/
function fixjsstyleAsync() {
var editor = EditorManager.getCurrentFullEditor(),
fullPath = EditorManager.getCurrentlyViewedPath();
if (editor && fullPath) {
var document = editor.document,
text = document.getText(),
cursor = editor.getCursorPos(),
scroll = editor.getScrollPos(),
flags;
// Keep a reference of document during async command.
document.addRef();
_loadConfig(fullPath).then(
function(config) {
var flags = config.flags.fixjsstyle;
closureLinter.exec('fixjsstyle', text, fullPath, flags)
.done(function(styledText) {
if (styledText !== text) {
// Replace the text and reset the viewport/cursor.
editor.document.setText(styledText);
editor.setScrollPos(scroll.x, scroll.y);
editor.setCursorPos(cursor.line, cursor.ch);
}
})
.always(function() {
// Release document reference since we are done with it.
document.releaseRef();
});
});
}
}
/**
* Turns Fixjsstyle On Save command on or off.
* @param {Boolean} checked True turns command on and vice versa.
*/
function setFixjsstyleOnSave(checked) {
$DocumentManager[checked ? 'on' : 'off'](
'documentSaved', fixjsstyleAsync);
fixjsstyleOnSaveCommand.setChecked(checked);
PreferencesManager.set(FIXJSSTYLE_ONSAVE_ID, checked);
PreferencesManager.save();
}
AppInit.appReady(function() {
// Set up fixjsstyle menu items.
var editMenu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
editMenu.addMenuDivider();
editMenu.addMenuItem(FIXJSSTYLE_ID, FIXJSSTYLE_SHORTCUT);
editMenu.addMenuItem(FIXJSSTYLE_ONSAVE_ID);
// Restore previous menu preferences.
setFixjsstyleOnSave(PreferencesManager.get(FIXJSSTYLE_ONSAVE_ID));
// Register gjslint to lint javascript and html files.
CodeInspection.register('javascript', {
name: GJSLINT_ID,
scanFileAsync: gjslintAsync
});
CodeInspection.register('html', {
name: GJSLINT_ID,
scanFileAsync: gjslintAsync
});
});
});