This repository has been archived by the owner on May 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
150 lines (127 loc) · 4.64 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
/*global $, brackets, define*/
"use strict";
define(function (require, exports, module) {
var CommandManager = brackets.getModule('command/CommandManager'),
Menus = brackets.getModule('command/Menus'),
DocumentManager = brackets.getModule('document/DocumentManager'),
EditorManager = brackets.getModule('editor/EditorManager'),
CodeInspection = brackets.getModule('language/CodeInspection'),
LanguageManager = brackets.getModule('language/LanguageManager'),
ExtensionUtils = brackets.getModule('utils/ExtensionUtils'),
NodeDomain = brackets.getModule('utils/NodeDomain'),
JS_LANGUAGE = LanguageManager.getLanguageForExtension('js'),
AUTOFIX_COMMAND_ID = "brackets-jscs.autofix",
AUTOFIX_COMMAND_NAME = "Auto-fix with JSCS",
LINTER_NAME = "JSCS",
ExtDomain = ExtensionUtils.getModulePath(module, 'domain'),
nodeDomain = new NodeDomain('globexdesigns.brackets-jscs', ExtDomain);
// =================================================================================
/**
* This will map JSCS error output to match format expected by Brackets linter API.
*
* @param {?array} [results] - Results object from JSCS's checkFile method
*
* @returns {object} Brackets lint object
*/
var remapResults = function (results) {
return {
errors: (results || []).map(function (result) {
var message = result.message,
isConfigError = result.rule === "";
if (result.rule) message += ' [' + result.rule + ']';
return {
message: message,
pos: {
line: isConfigError ? -1 : result.line - 1,
ch: isConfigError ? -1 : result.column
},
type: CodeInspection.Type.ERROR
};
})
};
};
/**
* Handler for the "Auto-fix with JSCS" command
* @returns {void}
*/
var handleAutoFix = function () {
var doc = DocumentManager.getCurrentDocument(),
language = doc.getLanguage(),
fileType = language._id,
fullPath = doc.file.fullPath,
editor = EditorManager.getCurrentFullEditor(),
cursor = editor.getCursorPos(),
scroll = editor.getScrollPos();
// Do nothing unless it's a Javascript file
if (fileType !== 'javascript') return;
nodeDomain.exec('fixFile', doc.getText(), fullPath)
.then(function (response) {
doc.setText(response);
// Reset editor back to previous cursor position
editor.setCursorPos(cursor);
editor.setScrollPos(scroll.x, scroll.y);
}/* TODO: Error handling?*/);
};
/**
* Handler for Brackets Linting API in synchronous mode
*
* @param {string} text - Code text to lint
* @param {string} fullPath - Path to the current file
*
*/
var handleLintSync = function (text, fullPath) {
throw new Error('JSCS is not available in synchronous mode, use async for ' + fullPath);
};
/**
* Handler for Brackets Linting API in asynchronous mode
*
* @param {string} text - Code text to lint
* @param {string} fullPath - Path to the current file
*
* @returns {Promise} Promise object
*/
var handleLintAsync = function (text, fullPath) {
var deferred = new $.Deferred();
nodeDomain.exec('lintFile', fullPath)
.then(function () {
return deferred.reject();
}, function (err) {
if (typeof err === 'string') {
var message = "Unexpected JSCS processing error: " + err;
if (err.indexOf('Unable to load one of the modules') >= 0) {
message = "JSCS has not been installed in the extension. See Installation steps here: https://github.com/globexdesigns/brackets-jscs";
}
return deferred.resolve({
errors: [{
pos: {
line: -1,
ch: -1
},
message: message
}]
});
}
// If we got back an empty error object - just convert it to an empty array
if (typeof err === 'object' && !Array.isArray(err)) err = [];
deferred.resolve(remapResults(err));
});
return deferred.promise();
};
// =================================================================================
// Register the auto-fix command
CommandManager.register(AUTOFIX_COMMAND_NAME, AUTOFIX_COMMAND_ID, handleAutoFix);
// Add to Edit menu
var editMenu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
editMenu.addMenuDivider();
editMenu.addMenuItem(AUTOFIX_COMMAND_ID);
// Add context-menu option (only for Javascript files)
var contextMenu = Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU);
contextMenu.addMenuItem(AUTOFIX_COMMAND_ID);
// Register a linter with CodeInspection
CodeInspection.register(JS_LANGUAGE.getId(), {
name: LINTER_NAME,
scanFile: handleLintSync,
scanFileAsync: handleLintAsync
});
LanguageManager.getLanguage('json').addFileName('.jscsrc');
});