2015-02-25 04:05:00 +00:00
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
2022-06-23 18:18:15 +00:00
|
|
|
// Distributed under an MIT license: https://codemirror.net/5/LICENSE
|
2015-02-25 04:05:00 +00:00
|
|
|
|
|
|
|
(function(mod) {
|
|
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
|
|
mod(require("../../lib/codemirror"));
|
|
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
|
|
define(["../../lib/codemirror"], mod);
|
|
|
|
else // Plain browser env
|
|
|
|
mod(CodeMirror);
|
|
|
|
})(function(CodeMirror) {
|
|
|
|
"use strict";
|
|
|
|
var GUTTER_ID = "CodeMirror-lint-markers";
|
2021-07-21 09:13:11 +00:00
|
|
|
var LINT_LINE_ID = "CodeMirror-lint-line-";
|
2015-02-25 04:05:00 +00:00
|
|
|
|
2020-06-02 08:08:02 +00:00
|
|
|
function showTooltip(cm, e, content) {
|
2015-02-25 04:05:00 +00:00
|
|
|
var tt = document.createElement("div");
|
2020-06-02 08:08:02 +00:00
|
|
|
tt.className = "CodeMirror-lint-tooltip cm-s-" + cm.options.theme;
|
2015-02-25 04:05:00 +00:00
|
|
|
tt.appendChild(content.cloneNode(true));
|
2020-06-02 08:08:02 +00:00
|
|
|
if (cm.state.lint.options.selfContain)
|
|
|
|
cm.getWrapperElement().appendChild(tt);
|
|
|
|
else
|
|
|
|
document.body.appendChild(tt);
|
2015-02-25 04:05:00 +00:00
|
|
|
|
|
|
|
function position(e) {
|
|
|
|
if (!tt.parentNode) return CodeMirror.off(document, "mousemove", position);
|
|
|
|
tt.style.top = Math.max(0, e.clientY - tt.offsetHeight - 5) + "px";
|
|
|
|
tt.style.left = (e.clientX + 5) + "px";
|
|
|
|
}
|
|
|
|
CodeMirror.on(document, "mousemove", position);
|
|
|
|
position(e);
|
|
|
|
if (tt.style.opacity != null) tt.style.opacity = 1;
|
|
|
|
return tt;
|
|
|
|
}
|
|
|
|
function rm(elt) {
|
|
|
|
if (elt.parentNode) elt.parentNode.removeChild(elt);
|
|
|
|
}
|
|
|
|
function hideTooltip(tt) {
|
|
|
|
if (!tt.parentNode) return;
|
|
|
|
if (tt.style.opacity == null) rm(tt);
|
|
|
|
tt.style.opacity = 0;
|
|
|
|
setTimeout(function() { rm(tt); }, 600);
|
|
|
|
}
|
|
|
|
|
2020-06-02 08:08:02 +00:00
|
|
|
function showTooltipFor(cm, e, content, node) {
|
|
|
|
var tooltip = showTooltip(cm, e, content);
|
2015-02-25 04:05:00 +00:00
|
|
|
function hide() {
|
|
|
|
CodeMirror.off(node, "mouseout", hide);
|
|
|
|
if (tooltip) { hideTooltip(tooltip); tooltip = null; }
|
|
|
|
}
|
|
|
|
var poll = setInterval(function() {
|
|
|
|
if (tooltip) for (var n = node;; n = n.parentNode) {
|
|
|
|
if (n && n.nodeType == 11) n = n.host;
|
|
|
|
if (n == document.body) return;
|
|
|
|
if (!n) { hide(); break; }
|
|
|
|
}
|
|
|
|
if (!tooltip) return clearInterval(poll);
|
|
|
|
}, 400);
|
|
|
|
CodeMirror.on(node, "mouseout", hide);
|
|
|
|
}
|
|
|
|
|
2021-07-21 12:32:01 +00:00
|
|
|
function LintState(cm, conf, hasGutter) {
|
2015-02-25 04:05:00 +00:00
|
|
|
this.marked = [];
|
2021-07-21 12:32:01 +00:00
|
|
|
if (conf instanceof Function) conf = {getAnnotations: conf};
|
|
|
|
if (!conf || conf === true) conf = {};
|
|
|
|
this.options = {};
|
|
|
|
this.linterOptions = conf.options || {};
|
|
|
|
for (var prop in defaults) this.options[prop] = defaults[prop];
|
|
|
|
for (var prop in conf) {
|
|
|
|
if (defaults.hasOwnProperty(prop)) {
|
|
|
|
if (conf[prop] != null) this.options[prop] = conf[prop];
|
|
|
|
} else if (!conf.options) {
|
|
|
|
this.linterOptions[prop] = conf[prop];
|
|
|
|
}
|
|
|
|
}
|
2015-02-25 04:05:00 +00:00
|
|
|
this.timeout = null;
|
|
|
|
this.hasGutter = hasGutter;
|
|
|
|
this.onMouseOver = function(e) { onMouseOver(cm, e); };
|
2017-02-08 12:35:49 +00:00
|
|
|
this.waitingFor = 0
|
2015-02-25 04:05:00 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 12:32:01 +00:00
|
|
|
var defaults = {
|
|
|
|
highlightLines: false,
|
|
|
|
tooltips: true,
|
|
|
|
delay: 500,
|
|
|
|
lintOnChange: true,
|
|
|
|
getAnnotations: null,
|
|
|
|
async: false,
|
|
|
|
selfContain: null,
|
|
|
|
formatAnnotation: null,
|
|
|
|
onUpdateLinting: null
|
2015-02-25 04:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearMarks(cm) {
|
|
|
|
var state = cm.state.lint;
|
|
|
|
if (state.hasGutter) cm.clearGutter(GUTTER_ID);
|
2021-07-21 12:32:01 +00:00
|
|
|
if (state.options.highlightLines) clearErrorLines(cm);
|
2015-02-25 04:05:00 +00:00
|
|
|
for (var i = 0; i < state.marked.length; ++i)
|
|
|
|
state.marked[i].clear();
|
|
|
|
state.marked.length = 0;
|
|
|
|
}
|
|
|
|
|
2021-07-21 09:13:11 +00:00
|
|
|
function clearErrorLines(cm) {
|
|
|
|
cm.eachLine(function(line) {
|
|
|
|
var has = line.wrapClass && /\bCodeMirror-lint-line-\w+\b/.exec(line.wrapClass);
|
|
|
|
if (has) cm.removeLineClass(line, "wrap", has[0]);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-02 08:08:02 +00:00
|
|
|
function makeMarker(cm, labels, severity, multiple, tooltips) {
|
2015-02-25 04:05:00 +00:00
|
|
|
var marker = document.createElement("div"), inner = marker;
|
2020-09-22 11:03:31 +00:00
|
|
|
marker.className = "CodeMirror-lint-marker CodeMirror-lint-marker-" + severity;
|
2015-02-25 04:05:00 +00:00
|
|
|
if (multiple) {
|
|
|
|
inner = marker.appendChild(document.createElement("div"));
|
2020-09-22 11:03:31 +00:00
|
|
|
inner.className = "CodeMirror-lint-marker CodeMirror-lint-marker-multiple";
|
2015-02-25 04:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tooltips != false) CodeMirror.on(inner, "mouseover", function(e) {
|
2020-06-02 08:08:02 +00:00
|
|
|
showTooltipFor(cm, e, labels, inner);
|
2015-02-25 04:05:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return marker;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMaxSeverity(a, b) {
|
|
|
|
if (a == "error") return a;
|
|
|
|
else return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
function groupByLine(annotations) {
|
|
|
|
var lines = [];
|
|
|
|
for (var i = 0; i < annotations.length; ++i) {
|
|
|
|
var ann = annotations[i], line = ann.from.line;
|
|
|
|
(lines[line] || (lines[line] = [])).push(ann);
|
|
|
|
}
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
function annotationTooltip(ann) {
|
|
|
|
var severity = ann.severity;
|
|
|
|
if (!severity) severity = "error";
|
|
|
|
var tip = document.createElement("div");
|
2020-09-22 11:03:31 +00:00
|
|
|
tip.className = "CodeMirror-lint-message CodeMirror-lint-message-" + severity;
|
2017-11-05 06:12:57 +00:00
|
|
|
if (typeof ann.messageHTML != 'undefined') {
|
2020-06-02 08:08:02 +00:00
|
|
|
tip.innerHTML = ann.messageHTML;
|
2017-11-05 06:12:57 +00:00
|
|
|
} else {
|
2020-06-02 08:08:02 +00:00
|
|
|
tip.appendChild(document.createTextNode(ann.message));
|
2017-11-05 06:12:57 +00:00
|
|
|
}
|
2015-02-25 04:05:00 +00:00
|
|
|
return tip;
|
|
|
|
}
|
|
|
|
|
2021-07-21 12:32:01 +00:00
|
|
|
function lintAsync(cm, getAnnotations) {
|
2017-02-08 12:35:49 +00:00
|
|
|
var state = cm.state.lint
|
|
|
|
var id = ++state.waitingFor
|
|
|
|
function abort() {
|
|
|
|
id = -1
|
|
|
|
cm.off("change", abort)
|
|
|
|
}
|
|
|
|
cm.on("change", abort)
|
|
|
|
getAnnotations(cm.getValue(), function(annotations, arg2) {
|
|
|
|
cm.off("change", abort)
|
|
|
|
if (state.waitingFor != id) return
|
|
|
|
if (arg2 && annotations instanceof CodeMirror) annotations = arg2
|
2018-03-03 20:03:05 +00:00
|
|
|
cm.operation(function() {updateLinting(cm, annotations)})
|
2021-07-21 12:32:01 +00:00
|
|
|
}, state.linterOptions, cm);
|
2017-02-08 12:35:49 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 04:05:00 +00:00
|
|
|
function startLinting(cm) {
|
2021-07-21 09:13:11 +00:00
|
|
|
var state = cm.state.lint;
|
|
|
|
if (!state) return;
|
|
|
|
var options = state.options;
|
2017-11-05 06:12:57 +00:00
|
|
|
/*
|
|
|
|
* Passing rules in `options` property prevents JSHint (and other linters) from complaining
|
|
|
|
* about unrecognized rules like `onUpdateLinting`, `delay`, `lintOnChange`, etc.
|
|
|
|
*/
|
2015-07-29 23:36:59 +00:00
|
|
|
var getAnnotations = options.getAnnotations || cm.getHelper(CodeMirror.Pos(0, 0), "lint");
|
|
|
|
if (!getAnnotations) return;
|
2017-02-08 12:35:49 +00:00
|
|
|
if (options.async || getAnnotations.async) {
|
2021-07-21 12:32:01 +00:00
|
|
|
lintAsync(cm, getAnnotations)
|
2017-02-08 12:35:49 +00:00
|
|
|
} else {
|
2021-07-21 12:32:01 +00:00
|
|
|
var annotations = getAnnotations(cm.getValue(), state.linterOptions, cm);
|
2017-06-29 13:39:01 +00:00
|
|
|
if (!annotations) return;
|
2017-03-18 00:24:59 +00:00
|
|
|
if (annotations.then) annotations.then(function(issues) {
|
2018-03-03 20:03:05 +00:00
|
|
|
cm.operation(function() {updateLinting(cm, issues)})
|
2017-03-18 00:24:59 +00:00
|
|
|
});
|
2018-03-03 20:03:05 +00:00
|
|
|
else cm.operation(function() {updateLinting(cm, annotations)})
|
2017-02-08 12:35:49 +00:00
|
|
|
}
|
2015-02-25 04:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateLinting(cm, annotationsNotSorted) {
|
2021-07-21 09:13:11 +00:00
|
|
|
var state = cm.state.lint;
|
|
|
|
if (!state) return;
|
|
|
|
var options = state.options;
|
2015-02-25 04:05:00 +00:00
|
|
|
clearMarks(cm);
|
|
|
|
|
|
|
|
var annotations = groupByLine(annotationsNotSorted);
|
|
|
|
|
|
|
|
for (var line = 0; line < annotations.length; ++line) {
|
|
|
|
var anns = annotations[line];
|
|
|
|
if (!anns) continue;
|
|
|
|
|
2020-12-21 22:17:19 +00:00
|
|
|
// filter out duplicate messages
|
|
|
|
var message = [];
|
|
|
|
anns = anns.filter(function(item) { return message.indexOf(item.message) > -1 ? false : message.push(item.message) });
|
|
|
|
|
2015-02-25 04:05:00 +00:00
|
|
|
var maxSeverity = null;
|
|
|
|
var tipLabel = state.hasGutter && document.createDocumentFragment();
|
|
|
|
|
|
|
|
for (var i = 0; i < anns.length; ++i) {
|
|
|
|
var ann = anns[i];
|
|
|
|
var severity = ann.severity;
|
|
|
|
if (!severity) severity = "error";
|
|
|
|
maxSeverity = getMaxSeverity(maxSeverity, severity);
|
|
|
|
|
|
|
|
if (options.formatAnnotation) ann = options.formatAnnotation(ann);
|
|
|
|
if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann));
|
|
|
|
|
|
|
|
if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, {
|
2020-09-22 11:03:31 +00:00
|
|
|
className: "CodeMirror-lint-mark CodeMirror-lint-mark-" + severity,
|
2015-02-25 04:05:00 +00:00
|
|
|
__annotation: ann
|
|
|
|
}));
|
|
|
|
}
|
2020-12-21 22:17:19 +00:00
|
|
|
// use original annotations[line] to show multiple messages
|
2015-02-25 04:05:00 +00:00
|
|
|
if (state.hasGutter)
|
2020-12-21 22:17:19 +00:00
|
|
|
cm.setGutterMarker(line, GUTTER_ID, makeMarker(cm, tipLabel, maxSeverity, annotations[line].length > 1,
|
2021-07-21 12:32:01 +00:00
|
|
|
options.tooltips));
|
2021-07-21 09:13:11 +00:00
|
|
|
|
2021-07-21 12:32:01 +00:00
|
|
|
if (options.highlightLines)
|
2021-07-21 09:13:11 +00:00
|
|
|
cm.addLineClass(line, "wrap", LINT_LINE_ID + maxSeverity);
|
2015-02-25 04:05:00 +00:00
|
|
|
}
|
|
|
|
if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onChange(cm) {
|
|
|
|
var state = cm.state.lint;
|
2015-04-23 02:44:04 +00:00
|
|
|
if (!state) return;
|
2015-02-25 04:05:00 +00:00
|
|
|
clearTimeout(state.timeout);
|
2021-07-21 12:32:01 +00:00
|
|
|
state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay);
|
2015-02-25 04:05:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 08:08:02 +00:00
|
|
|
function popupTooltips(cm, annotations, e) {
|
2015-02-25 04:05:00 +00:00
|
|
|
var target = e.target || e.srcElement;
|
2017-02-08 12:35:49 +00:00
|
|
|
var tooltip = document.createDocumentFragment();
|
|
|
|
for (var i = 0; i < annotations.length; i++) {
|
|
|
|
var ann = annotations[i];
|
|
|
|
tooltip.appendChild(annotationTooltip(ann));
|
|
|
|
}
|
2020-06-02 08:08:02 +00:00
|
|
|
showTooltipFor(cm, e, tooltip, target);
|
2015-02-25 04:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onMouseOver(cm, e) {
|
|
|
|
var target = e.target || e.srcElement;
|
|
|
|
if (!/\bCodeMirror-lint-mark-/.test(target.className)) return;
|
|
|
|
var box = target.getBoundingClientRect(), x = (box.left + box.right) / 2, y = (box.top + box.bottom) / 2;
|
|
|
|
var spans = cm.findMarksAt(cm.coordsChar({left: x, top: y}, "client"));
|
2017-02-08 12:35:49 +00:00
|
|
|
|
|
|
|
var annotations = [];
|
2015-02-25 04:05:00 +00:00
|
|
|
for (var i = 0; i < spans.length; ++i) {
|
|
|
|
var ann = spans[i].__annotation;
|
2017-02-08 12:35:49 +00:00
|
|
|
if (ann) annotations.push(ann);
|
2015-02-25 04:05:00 +00:00
|
|
|
}
|
2020-06-02 08:08:02 +00:00
|
|
|
if (annotations.length) popupTooltips(cm, annotations, e);
|
2015-02-25 04:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeMirror.defineOption("lint", false, function(cm, val, old) {
|
|
|
|
if (old && old != CodeMirror.Init) {
|
|
|
|
clearMarks(cm);
|
2017-02-08 12:35:49 +00:00
|
|
|
if (cm.state.lint.options.lintOnChange !== false)
|
2015-07-29 23:36:59 +00:00
|
|
|
cm.off("change", onChange);
|
2015-02-25 04:05:00 +00:00
|
|
|
CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver);
|
2015-04-23 02:44:04 +00:00
|
|
|
clearTimeout(cm.state.lint.timeout);
|
2015-02-25 04:05:00 +00:00
|
|
|
delete cm.state.lint;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val) {
|
|
|
|
var gutters = cm.getOption("gutters"), hasLintGutter = false;
|
|
|
|
for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true;
|
2021-07-21 12:32:01 +00:00
|
|
|
var state = cm.state.lint = new LintState(cm, val, hasLintGutter);
|
|
|
|
if (state.options.lintOnChange)
|
2015-07-29 23:36:59 +00:00
|
|
|
cm.on("change", onChange);
|
2017-02-08 12:35:49 +00:00
|
|
|
if (state.options.tooltips != false && state.options.tooltips != "gutter")
|
2015-02-25 04:05:00 +00:00
|
|
|
CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
|
|
|
|
|
2017-02-08 12:35:49 +00:00
|
|
|
startLinting(cm);
|
2015-02-25 04:05:00 +00:00
|
|
|
}
|
|
|
|
});
|
2015-07-29 23:36:59 +00:00
|
|
|
|
|
|
|
CodeMirror.defineExtension("performLint", function() {
|
2021-07-21 09:13:11 +00:00
|
|
|
startLinting(this);
|
2015-07-29 23:36:59 +00:00
|
|
|
});
|
2015-02-25 04:05:00 +00:00
|
|
|
});
|