-
Notifications
You must be signed in to change notification settings - Fork 2
/
CodeMirrorErrorDisplayController.js
280 lines (227 loc) · 15.3 KB
/
CodeMirrorErrorDisplayController.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, setTimeout, clearTimeout */
define(function (require, exports, module) {
"use strict";
var JSLintError = require("JSLintError");
var codeMirror,
linesWithErrorsAndWarnings,
errorHighlights = [],
lastStoppedAtLineHandle = null,
errorMessagesInLines = [];
var severityLevelsToDisplay = [JSLintError.SeverityLevelEnum.SYNTAX_ERROR,
JSLintError.SeverityLevelEnum.UNCLASSIFIED,
JSLintError.SeverityLevelEnum.BAD_CODE_OR_PRACTICE,
JSLintError.SeverityLevelEnum.WARNING,
JSLintError.SeverityLevelEnum.JUST_STYLE];
var DELAY_FOR_THROTTLING_ERROR_CHECKING = 500;
var cssClassesForSeverityLevel = {};
cssClassesForSeverityLevel[JSLintError.SeverityLevelEnum.UNCLASSIFIED] = "cc-JSLint-unclassified-error";
cssClassesForSeverityLevel[JSLintError.SeverityLevelEnum.SYNTAX_ERROR] = "cc-JSLint-syntax-error";
cssClassesForSeverityLevel[JSLintError.SeverityLevelEnum.BAD_CODE_OR_PRACTICE] = "cc-JSLint-bad-code-error";
cssClassesForSeverityLevel[JSLintError.SeverityLevelEnum.WARNING] = "cc-JSLint-warning";
cssClassesForSeverityLevel[JSLintError.SeverityLevelEnum.JUST_STYLE] = "cc-JSLint-just-style-warning";
function _toggleErrorMessageInLine(codeMirror, lineIndex, gutter) {
var errorsAndWarningsOnThisLine = linesWithErrorsAndWarnings[lineIndex];
if (errorsAndWarningsOnThisLine === undefined) {
// no errors to display
return;
}
var $lineElementInEditor = $(".CodeMirror:visible .CodeMirror-lines div div:last-child pre:eq(" + lineIndex + ")");
if (codeMirror.lineInfo(lineIndex).wrapClass && (codeMirror.lineInfo(lineIndex).wrapClass.indexOf("highlightErrorLine") > -1)) {
codeMirror.removeLineClass(lineIndex, "wrap", "highlightErrorLine");
errorMessagesInLines[lineIndex].clear();
errorMessagesInLines[lineIndex] = undefined;
} else {
codeMirror.addLineClass(lineIndex, "wrap", "highlightErrorLine");
// create a div that contains all error messages
var $errorMessageInEditor = $("<div class='cc-JSLint-error-message'/>");
severityLevelsToDisplay.forEach(function (severityLevel) {
var errorsOnThisSeverityLevelOnThisLine = errorsAndWarningsOnThisLine[severityLevel];
if (!errorsOnThisSeverityLevelOnThisLine) {
// no errors on this level
return;
}
errorsOnThisSeverityLevelOnThisLine.forEach(function (errorItem, index, array) {
var message = errorItem.reason;
message = message.replace(/'(.*)'/g, "<span class='cc-code'>$&</span>");
var $messageSpan = $("<span>" + message + "</span>");
$messageSpan.addClass(cssClassesForSeverityLevel[severityLevel]);
var $errorMessageDiv = $("<div></div>");
$errorMessageDiv.append($messageSpan);
$errorMessageInEditor.append($errorMessageDiv);
// move the error message to the right to position their left border under the error
var offsetPosition = errorItem.insertionMarkerPosition || errorItem.startPosition;
var offset = codeMirror.cursorCoords(offsetPosition, "local").left;
var $offsetSpan = $("<span class='cc-offset'> </span>");
$offsetSpan.css("width", offset);
$errorMessageDiv.prepend($offsetSpan);
});
});
var lineWidgetForThisLine = codeMirror.addLineWidget(lineIndex, $errorMessageInEditor[0], {coverGutter: false, noHScroll: false, above: false, showIfHidden: false});
$errorMessageInEditor.parent().addClass("cc-noscroll");
if (errorMessagesInLines[lineIndex] !== undefined) {
var oldLineWidget = errorMessagesInLines[lineIndex];
oldLineWidget.clear();
}
errorMessagesInLines[lineIndex] = lineWidgetForThisLine;
}
}
function _addHighlightMarkerForErrorInEditor(jslintError) {
var newErrorHighlight;
var markOptionsForNormalErrorHighlight = {className: "cc-JSLint-error-highlight " + cssClassesForSeverityLevel[jslintError.severityLevel]},
markOptionsForStoppedMarkerRight = {className: "cc-JSLint-error-stopped-right"},
markOptionsForStoppedMarkerLeft = {className: "cc-JSLint-error-stopped-left"},
markOptionsForInsertionMarkerRight = {className: "cc-JSLint-error-missing-right " + cssClassesForSeverityLevel[jslintError.severityLevel]},
markOptionsForInsertionMarkerLeft = {className: "cc-JSLint-error-missing-left " + cssClassesForSeverityLevel[jslintError.severityLevel]};
if (jslintError.type === JSLintError.TypesEnum.STOPPING) {
// this is the stopping error
// mark the line after which JSLint stopped
codeMirror.addLineClass(jslintError.startPosition.line, "wrap", "cc-JSLint-stop-line");
// and add a marker at the point where it stopped in the line itself
newErrorHighlight = codeMirror.markText({line: jslintError.startPosition.line, ch: 0}, jslintError.startPosition, markOptionsForStoppedMarkerRight);
errorHighlights.push(newErrorHighlight);
newErrorHighlight = codeMirror.markText(jslintError.startPosition, {line: jslintError.startPosition.line, ch: jslintError.startPosition.ch + 1}, markOptionsForStoppedMarkerLeft);
errorHighlights.push(newErrorHighlight);
return;
}
// highlight error
newErrorHighlight = codeMirror.markText(jslintError.startPosition, jslintError.endPosition, markOptionsForNormalErrorHighlight);
errorHighlights.push(newErrorHighlight);
if (jslintError.type === JSLintError.TypesEnum.MISSING) {
// if it's a highlight for something that's missing. Add classes for the insertion marker.
// ( the CSS classes are swiched (left assigned to right and vice versa) sine the position indicates the position left (or right) of the insertion point
// which means the insertion marker should be displayed on the right (or left) )
var insertionPos = jslintError.insertionMarkerPosition || jslintError.startPosition;
newErrorHighlight = codeMirror.markText({line: insertionPos.line, ch: insertionPos.ch - 1}, insertionPos, markOptionsForInsertionMarkerRight);
errorHighlights.push(newErrorHighlight);
newErrorHighlight = codeMirror.markText(insertionPos, {line: insertionPos.line, ch: insertionPos.ch + 1}, markOptionsForInsertionMarkerLeft);
errorHighlights.push(newErrorHighlight);
}
}
function _clearErrorDisplay() {
if (lastStoppedAtLineHandle) {
lastStoppedAtLineHandle.bgClassName = null;
lastStoppedAtLineHandle = null;
}
// remove displayed error messages
errorMessagesInLines.forEach(function (lineWidget, lineIndex, array) {
if (lineWidget) {
lineWidget.clear();
}
codeMirror.removeLineClass(lineIndex, "wrap", "highlightErrorLine");
});
errorMessagesInLines = [];
// clear error markers in line gutter
codeMirror.clearGutter("CodeMirror-linenumbers");
// remove error highlights
errorHighlights.forEach(function (anErrorHighlight, index, array) {
anErrorHighlight.clear();
});
errorHighlights = [];
}
function _showCurrentErrors() {
// reset error markers
_clearErrorDisplay();
//var $lineNumberBoxesInGutter = $(".CodeMirror:visible .CodeMirror-gutter-text pre");
// add error markers for current errors
if ((linesWithErrorsAndWarnings !== undefined) && (linesWithErrorsAndWarnings !== null)) {
linesWithErrorsAndWarnings.forEach(function (errorsAndWarningsOnThisLine, lineIndex, array) {
var $lineNumberBoxForError = $("<div class='cc-JSLint-error-in-line CodeMirror-linenumber'/>");
var containsStoppingError = false;
var severityLevelsWithErrors = [];
var firstErrors = {};
firstErrors[JSLintError.SeverityLevelEnum.SYNTAX_ERROR] = undefined;
firstErrors[JSLintError.SeverityLevelEnum.UNCLASSIFIED] = undefined;
firstErrors[JSLintError.SeverityLevelEnum.BAD_CODE_OR_PRACTICE] = undefined;
firstErrors[JSLintError.SeverityLevelEnum.WARNING] = undefined;
firstErrors[JSLintError.SeverityLevelEnum.JUST_STYLE] = undefined;
severityLevelsToDisplay.forEach(
function (severityLevel) {
var errorsForThisLevelOnThisLine = errorsAndWarningsOnThisLine[severityLevel];
if (!errorsForThisLevelOnThisLine) {
// no errors on this level
return;
}
var $errorMarkerInLineGutter = $("<span/>");
$errorMarkerInLineGutter.addClass(cssClassesForSeverityLevel[severityLevel]);
$lineNumberBoxForError.append($errorMarkerInLineGutter);
var firstErrorOnThisLine = errorsForThisLevelOnThisLine[0];
firstErrors[severityLevel] = firstErrorOnThisLine;
if (errorsForThisLevelOnThisLine.length === 1) {
severityLevelsWithErrors.push(severityLevel);
// add marker classes to line-number gutter in editor
$errorMarkerInLineGutter.addClass("cc-JSLint-one-error");
$lineNumberBoxForError.attr("title", firstErrorOnThisLine.reason);
$errorMarkerInLineGutter.text("!");
//$lineNumberBoxForError.prepend("<span class='cc-JSLint-error-marker'>!</span>");
} else if (errorsForThisLevelOnThisLine.length > 1) {
severityLevelsWithErrors.push(severityLevel);
$errorMarkerInLineGutter.addClass("cc-JSLint-several-errors");
$lineNumberBoxForError.attr("title", errorsForThisLevelOnThisLine.length + " errors. First: " + firstErrorOnThisLine.reason);
$errorMarkerInLineGutter.text(errorsForThisLevelOnThisLine.length);
//$lineNumberBoxForError.prepend("<span class='cc-JSLint-error-marker'>" + errorsOnThisLine.length + "</span>");
}
errorsForThisLevelOnThisLine.forEach(function (errorItem, index, array) {
if (errorItem.type === JSLintError.TypesEnum.STOPPING) {
// add marker class for stopping error to line-number gutter in editor
$errorMarkerInLineGutter.addClass("cc-JSLint-stopping-error");
containsStoppingError = true;
}
_addHighlightMarkerForErrorInEditor(errorItem);
});
}
);
if (severityLevelsWithErrors.length > 1) {
var tooltip = "";
tooltip += firstErrors[JSLintError.SeverityLevelEnum.SYNTAX_ERROR] ? "Syntax Errors: " + errorsAndWarningsOnThisLine[JSLintError.SeverityLevelEnum.SYNTAX_ERROR] + ", " : "";
tooltip += firstErrors[JSLintError.SeverityLevelEnum.UNCLASSIFIED] ? "Unclassified Errors: " + errorsAndWarningsOnThisLine[JSLintError.SeverityLevelEnum.SYNTAX_ERROR] + ", " : "";
tooltip += firstErrors[JSLintError.SeverityLevelEnum.BAD_CODE_OR_PRACTICE] ? "Probable Errors: " + errorsAndWarningsOnThisLine[JSLintError.SeverityLevelEnum.SYNTAX_ERROR] + ", " : "";
tooltip += firstErrors[JSLintError.SeverityLevelEnum.WARNING] ? "Warnings: " + errorsAndWarningsOnThisLine[JSLintError.SeverityLevelEnum.SYNTAX_ERROR] + ", " : "";
tooltip += firstErrors[JSLintError.SeverityLevelEnum.JUST_STYLE] ? "Style Warnings: " + errorsAndWarningsOnThisLine[JSLintError.SeverityLevelEnum.SYNTAX_ERROR] : "";
var firstError = firstErrors[JSLintError.SeverityLevelEnum.SYNTAX_ERROR] || firstErrors[JSLintError.SeverityLevelEnum.UNCLASSIFIED] || firstErrors[JSLintError.SeverityLevelEnum.BAD_CODE_OR_PRACTICE] || firstErrors[JSLintError.SeverityLevelEnum.WARNING];
$lineNumberBoxForError.attr("title", tooltip + ". First: " + firstError.reason);
}
if (containsStoppingError) {
$lineNumberBoxForError.attr("title", "Stopping. " + $lineNumberBoxForError.attr("title"));
}
$lineNumberBoxForError.append(lineIndex + 1);
codeMirror.setGutterMarker(lineIndex, "CodeMirror-linenumbers", $lineNumberBoxForError[0]);
});
}
}
function setErrorsToDisplay(errorsToDisplay) {
if (errorsToDisplay === null) {
linesWithErrorsAndWarnings = null;
} else {
linesWithErrorsAndWarnings = [];
errorsToDisplay.forEach(function (error, index, array) {
if (error === null) {
return;
}
if (linesWithErrorsAndWarnings[error.startPosition.line] === undefined) {
linesWithErrorsAndWarnings[error.startPosition.line] = [];
}
if (linesWithErrorsAndWarnings[error.startPosition.line][error.severityLevel] === undefined) {
linesWithErrorsAndWarnings[error.startPosition.line][error.severityLevel] = [];
}
linesWithErrorsAndWarnings[error.startPosition.line][error.severityLevel].push(error);
});
}
if (codeMirror) {
_showCurrentErrors();
}
}
exports.setErrorsToDisplay = setErrorsToDisplay;
exports.setCodeMirrorToAddHighlightsTo = function (codeMirrorToUse) {
if (codeMirror) {
// cleanup before letting go of old CodeMirror
codeMirror.off("gutterClick", _toggleErrorMessageInLine);
_clearErrorDisplay();
}
codeMirror = codeMirrorToUse;
if (codeMirror) {
codeMirror.on("gutterClick", _toggleErrorMessageInLine);
_showCurrentErrors();
}
};
});