3 var GUTTER_ID = "CodeMirror-lint-markers";
4 var SEVERITIES = /^(?:error|warning)$/;
6 function showTooltip(e, content) {
7 var tt = document.createElement("div");
8 tt.className = "CodeMirror-lint-tooltip";
9 tt.appendChild(content.cloneNode(true));
10 document.body.appendChild(tt);
12 function position(e) {
13 if (!tt.parentNode) return CodeMirror.off(document, "mousemove", position);
14 tt.style.top = Math.max(0, e.clientY - tt.offsetHeight - 5) + "px";
15 tt.style.left = (e.clientX + 5) + "px";
17 CodeMirror.on(document, "mousemove", position);
19 if (tt.style.opacity != null) tt.style.opacity = 1;
23 if (elt.parentNode) elt.parentNode.removeChild(elt);
25 function hideTooltip(tt) {
26 if (!tt.parentNode) return;
27 if (tt.style.opacity == null) rm(tt);
29 setTimeout(function() { rm(tt); }, 600);
32 function showTooltipFor(e, content, node) {
33 var tooltip = showTooltip(e, content);
35 CodeMirror.off(node, "mouseout", hide);
36 if (tooltip) { hideTooltip(tooltip); tooltip = null; }
38 var poll = setInterval(function() {
39 if (tooltip) for (var n = node;; n = n.parentNode) {
40 if (n == document.body) return;
41 if (!n) { hide(); break; }
43 if (!tooltip) return clearInterval(poll);
45 CodeMirror.on(node, "mouseout", hide);
48 function LintState(cm, options, hasGutter) {
50 this.options = options;
52 this.hasGutter = hasGutter;
53 this.onMouseOver = function(e) { onMouseOver(cm, e); };
56 function parseOptions(cm, options) {
57 if (options instanceof Function) return {getAnnotations: options};
58 if (!options || options === true) options = {};
59 if (!options.getAnnotations) options.getAnnotations = cm.getHelper(CodeMirror.Pos(0, 0), "lint");
60 if (!options.getAnnotations) throw new Error("Required option 'getAnnotations' missing (lint addon)");
64 function clearMarks(cm) {
65 var state = cm.state.lint;
66 if (state.hasGutter) cm.clearGutter(GUTTER_ID);
67 for (var i = 0; i < state.marked.length; ++i)
68 state.marked[i].clear();
69 state.marked.length = 0;
72 function makeMarker(labels, severity, multiple, tooltips) {
73 var marker = document.createElement("div"), inner = marker;
74 marker.className = "CodeMirror-lint-marker-" + severity;
76 inner = marker.appendChild(document.createElement("div"));
77 inner.className = "CodeMirror-lint-marker-multiple";
80 if (tooltips != false) CodeMirror.on(inner, "mouseover", function(e) {
81 showTooltipFor(e, labels, inner);
87 function getMaxSeverity(a, b) {
88 if (a == "error") return a;
92 function groupByLine(annotations) {
94 for (var i = 0; i < annotations.length; ++i) {
95 var ann = annotations[i], line = ann.from.line;
96 (lines[line] || (lines[line] = [])).push(ann);
101 function annotationTooltip(ann) {
102 var severity = ann.severity;
103 if (!SEVERITIES.test(severity)) severity = "error";
104 var tip = document.createElement("div");
105 tip.className = "CodeMirror-lint-message-" + severity;
106 tip.appendChild(document.createTextNode(ann.message));
110 function startLinting(cm) {
111 var state = cm.state.lint, options = state.options;
113 options.getAnnotations(cm, updateLinting, options);
115 updateLinting(cm, options.getAnnotations(cm.getValue()));
118 function updateLinting(cm, annotationsNotSorted) {
120 var state = cm.state.lint, options = state.options;
122 var annotations = groupByLine(annotationsNotSorted);
124 for (var line = 0; line < annotations.length; ++line) {
125 var anns = annotations[line];
128 var maxSeverity = null;
129 var tipLabel = state.hasGutter && document.createDocumentFragment();
131 for (var i = 0; i < anns.length; ++i) {
133 var severity = ann.severity;
134 if (!SEVERITIES.test(severity)) severity = "error";
135 maxSeverity = getMaxSeverity(maxSeverity, severity);
137 if (options.formatAnnotation) ann = options.formatAnnotation(ann);
138 if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann));
140 if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, {
141 className: "CodeMirror-lint-mark-" + severity,
147 cm.setGutterMarker(line, GUTTER_ID, makeMarker(tipLabel, maxSeverity, anns.length > 1,
148 state.options.tooltips));
150 if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
153 function onChange(cm) {
154 var state = cm.state.lint;
155 clearTimeout(state.timeout);
156 state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500);
159 function popupSpanTooltip(ann, e) {
160 var target = e.target || e.srcElement;
161 showTooltipFor(e, annotationTooltip(ann), target);
164 // When the mouseover fires, the cursor might not actually be over
165 // the character itself yet. These pairs of x,y offsets are used to
166 // probe a few nearby points when no suitable marked range is found.
167 var nearby = [0, 0, 0, 5, 0, -5, 5, 0, -5, 0];
169 function onMouseOver(cm, e) {
170 if (!/\bCodeMirror-lint-mark-/.test((e.target || e.srcElement).className)) return;
171 for (var i = 0; i < nearby.length; i += 2) {
172 var spans = cm.findMarksAt(cm.coordsChar({left: e.clientX + nearby[i],
173 top: e.clientY + nearby[i + 1]}));
174 for (var j = 0; j < spans.length; ++j) {
175 var span = spans[j], ann = span.__annotation;
176 if (ann) return popupSpanTooltip(ann, e);
181 function optionHandler(cm, val, old) {
182 if (old && old != CodeMirror.Init) {
184 cm.off("change", onChange);
185 CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver);
186 delete cm.state.lint;
190 var gutters = cm.getOption("gutters"), hasLintGutter = false;
191 for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true;
192 var state = cm.state.lint = new LintState(cm, parseOptions(cm, val), hasLintGutter);
193 cm.on("change", onChange);
194 if (state.options.tooltips != false)
195 CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
201 CodeMirror.defineOption("lintWith", false, optionHandler); // deprecated
202 CodeMirror.defineOption("lint", false, optionHandler); // deprecated