-
Notifications
You must be signed in to change notification settings - Fork 26
/
linterReporter.js
264 lines (202 loc) · 7.35 KB
/
linterReporter.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* Interactive Linter Copyright (c) 2015 Miguel Castillo.
*
* Licensed under MIT
*/
define(function (require /*, exports, module*/) {
"use strict";
var _ = brackets.getModule("thirdparty/lodash"),
EditorManager = brackets.getModule("editor/EditorManager"),
Promise = require("libs/js/spromise"),
ProblemWidget = require("ProblemWidget");
function Reporter() {
this.marks = {};
}
/**
* Routine that goes through all the linter messages and adds all the gutter
* symbols and the underlines.
*
* @param messages {Array} linter messages
* @param cm {CodeMirror} codemirror instance
*/
Reporter.prototype.report = function(cm, messages) {
var _self = this;
this.lastMessages = messages;
if (this.lastRequest && this.lastRequest.state() === "pending") {
return;
}
this.lastRequest = this._runReport(cm, messages)
.always(function () {
_self.lastRequest = null;
if (_self.lastMessages !== messages) {
_self.report(cm, _self.lastMessages);
}
});
};
/**
* Add reporting information in code mirror's document
*
* Message requires:
* - type,
* - code,
* - raw,
* - reason,
* - href
*
* Token is a CodeMirror token that specifies start/end information for
* the string in CodeMirror's document
*/
Reporter.prototype.addGutterMarks = function(message, token) {
var mark = this.marks[token.start.line];
// gutterMark is the placehoder for the lightbulb
// lineMarks are the underlines in the places the errors are reported for
if (!mark) {
mark = {
warnings: [],
errors: [],
lineMarks: [],
gutterMark: {
element: $("<div class='interactive-linter-gutter-messages' title='Click for details'> </div>")
}
};
this.marks[token.start.line] = mark;
mark.gutterMark.line = this.cm.setGutterMarker(token.start.line, "interactive-linter-gutter", mark.gutterMark.element[0]);
}
// Add message to warnings or errors array
if (mark[message.type + "s"]) {
mark[message.type + "s"].push(message);
}
// If the message is an error message, then add the error class to gutter
if (message.type === "error") {
mark.gutterMark.element.addClass("interactive-linter-gutter-errors");
}
};
/**
* Routine to underline problems in documents
*/
Reporter.prototype.addLineMarks = function (message, token) {
var mark = this.marks[token.start.line];
// Add line marks
mark.lineMarks.push({
line: this.cm.markText(token.start, token.end, {className: "interactive-linter-" + message.type}),
message: message
});
};
/**
* Clears all warning/errors from codemirror's document
*/
Reporter.prototype.clearMarks = function () {
var _self = this;
if (!this.cm) {
return;
}
this.cm.clearGutter("interactive-linter-gutter");
_.forEach(this.marks, function (mark) {
mark.lineMarks.forEach(function (textMark) {
textMark.line.clear();
});
if (mark.inlineWidget) {
_self.hideLineDetails(mark.inlineWidget);
delete mark.inlineWidget;
}
delete mark.errors;
delete mark.warnings;
delete mark.lineMarks;
delete mark.gutterMarks;
});
this.marks = {};
};
Reporter.prototype.getWidgetForLine = function (line) {
var activeEditor = EditorManager.getActiveEditor();
var inlineWidgets = activeEditor.getInlineWidgets();
return _.find(inlineWidgets, function (widget) {
return widget instanceof ProblemWidget && widget.line === line;
});
};
Reporter.prototype.toggleLineDetails = function (line) {
var activeEditor = EditorManager.getActiveEditor();
var cursorPos = line === undefined ? activeEditor.getCursorPos() : { line: line, ch: 0 };
var lineWidget = this.getWidgetForLine(cursorPos.line);
if (lineWidget) {
this.hideLineDetails(lineWidget);
}
else {
this.showLineDetails(line);
}
};
Reporter.prototype.hideLineDetails = function (widget) {
EditorManager.getActiveEditor().removeInlineWidget(widget);
};
Reporter.prototype.showLineDetails = function (line) {
var activeEditor = EditorManager.getActiveEditor();
var cursorPos = line !== undefined ? {line: line, ch: 0} : activeEditor.getCursorPos();
var mark = this.marks[cursorPos.line];
if (mark) {
var problemWidget = new ProblemWidget(mark, cursorPos.line);
problemWidget.load(activeEditor);
activeEditor.addInlineWidget(cursorPos, problemWidget, true);
mark.inlineWidget = problemWidget;
}
};
/**
* Determines if there is a fatal error in the linting report
*/
Reporter.prototype.checkFatal = function (messages) {
// If the last message created by linter is falsy, that means
// that we have encoutered a fatal error...
if (messages.length > 2 && !messages[messages.length - 1]) {
$(linterReporter).triggerHandler("fatalError", messages[messages.length - 2]);
return true;
}
this.clearFatalError();
return false;
};
Reporter.prototype.clearFatalError = function () {
$(linterReporter).triggerHandler("fatalError", null);
};
Reporter.prototype._runReport = function (cm, messages) {
var _self = this;
var deferred = Promise.defer();
// Run as operation for best performance
setTimeout(function () {
cm.operation(function() {
_self.clearMarks();
$(linterReporter).triggerHandler("lintMessage", [messages]);
if (messages) {
_self.checkFatal(messages);
_self.cm = cm;
_self.messages = messages;
_.forEach(messages, function (message) {
if (!message) {
return;
}
if (message.token) {
_self.addGutterMarks(message, message.token);
_self.addLineMarks(message, message.token);
}
});
}
deferred.resolve();
});
}, 0);
return deferred.promise;
};
function linterReporter () {
var _reporter = new Reporter();
function report(cm, messages) {
return _reporter.report(cm, messages);
}
function toggleLineDetails(line) {
_reporter.toggleLineDetails(line);
}
function clear() {
_reporter.clearMarks();
}
return {
clear: clear,
report: report,
toggleLineDetails: toggleLineDetails
};
}
return linterReporter;
});