-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
92 lines (75 loc) · 2.94 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
/*jslint indent: 4, nomen: true */
/*global define, brackets, $ */
/**
* Provides phplint results via the core linting extension point
*/
define(function (require, exports, module) {
"use strict";
var AppInit = brackets.getModule("utils/AppInit"),
DocumentManager = brackets.getModule('document/DocumentManager'),
CodeInspection = brackets.getModule('language/CodeInspection'),
ExtensionUtils = brackets.getModule('utils/ExtensionUtils'),
NodeConnection = brackets.getModule('utils/NodeConnection'),
EditorManager = brackets.getModule('editor/EditorManager'),
codeInspectionErrors = [];
function compile(compiler, file) {
var tasks = [];
tasks.push(compiler.compile(file));
return $.when.apply($, tasks);
}
// load a named node module
function connectToNodeModule(moduleName) {
var connection = new NodeConnection();
return connection.connect(true).pipe(function () {
var path = ExtensionUtils.getModulePath(module, 'node/' + moduleName);
return connection.loadDomains([path], true);
}).pipe(function () {
return connection.domains[moduleName];
});
}
function convertError(error) {
if (typeof error === 'string') {
return { pos: {}, message: error };
}
if (error.message) {
var message = error.message.split('|'),
pos = message[0].split(':');
return { pos: {line: pos[1] - 1, ch: pos[2]}, message: message[message.length - 1] };
} else {
return { pos: {line: error.lineno - 1, ch: error.column}, message: 'Error in line: ' + error.lineno };
}
}
function compileStylus(documentPath) {
var deferred = new $.Deferred(),
connection = connectToNodeModule('StylusCompiler');
$.when(connection).then(function (compiler, options) {
compile(compiler, documentPath).then(function () {
codeInspectionErrors = [];
deferred.resolve();
CodeInspection.requestRun();
}, function (error) {
codeInspectionErrors = [convertError(error)];
CodeInspection.requestRun();
});
}, function (error) {
codeInspectionErrors = [error];
CodeInspection.requestRun();
});
return deferred.promise();
}
CodeInspection.register('stylus', {
name: 'Stylus Auto Compile',
scanFile: function (text, fullPath) {
return {
errors: codeInspectionErrors
};
}
});
AppInit.appReady(function () {
$(DocumentManager).on('documentSaved', function (event, document) {
if (document.getLanguage().getId() === 'stylus') {
compileStylus(document.file.fullPath);
}
});
});
});