Fix: merge conflict
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / addon / lint / lint.js
1 (function() {
2   "use strict";
3   var GUTTER_ID = "CodeMirror-lint-markers";
4   var SEVERITIES = /^(?:error|warning)$/;
5
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);
11
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";
16     }
17     CodeMirror.on(document, "mousemove", position);
18     position(e);
19     if (tt.style.opacity != null) tt.style.opacity = 1;
20     return tt;
21   }
22   function rm(elt) {
23     if (elt.parentNode) elt.parentNode.removeChild(elt);
24   }
25   function hideTooltip(tt) {
26     if (!tt.parentNode) return;
27     if (tt.style.opacity == null) rm(tt);
28     tt.style.opacity = 0;
29     setTimeout(function() { rm(tt); }, 600);
30   }
31
32   function showTooltipFor(e, content, node) {
33     var tooltip = showTooltip(e, content);
34     function hide() {
35       CodeMirror.off(node, "mouseout", hide);
36       if (tooltip) { hideTooltip(tooltip); tooltip = null; }
37     }
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; }
42       }
43       if (!tooltip) return clearInterval(poll);
44     }, 400);
45     CodeMirror.on(node, "mouseout", hide);
46   }
47
48   function LintState(cm, options, hasGutter) {
49     this.marked = [];
50     this.options = options;
51     this.timeout = null;
52     this.hasGutter = hasGutter;
53     this.onMouseOver = function(e) { onMouseOver(cm, e); };
54   }
55
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)");
61     return options;
62   }
63
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;
70   }
71
72   function makeMarker(labels, severity, multiple, tooltips) {
73     var marker = document.createElement("div"), inner = marker;
74     marker.className = "CodeMirror-lint-marker-" + severity;
75     if (multiple) {
76       inner = marker.appendChild(document.createElement("div"));
77       inner.className = "CodeMirror-lint-marker-multiple";
78     }
79
80     if (tooltips != false) CodeMirror.on(inner, "mouseover", function(e) {
81       showTooltipFor(e, labels, inner);
82     });
83
84     return marker;
85   }
86
87   function getMaxSeverity(a, b) {
88     if (a == "error") return a;
89     else return b;
90   }
91
92   function groupByLine(annotations) {
93     var lines = [];
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);
97     }
98     return lines;
99   }
100
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));
107     return tip;
108   }
109
110   function startLinting(cm) {
111     var state = cm.state.lint, options = state.options;
112     if (options.async)
113       options.getAnnotations(cm, updateLinting, options);
114     else
115       updateLinting(cm, options.getAnnotations(cm.getValue()));
116   }
117
118   function updateLinting(cm, annotationsNotSorted) {
119     clearMarks(cm);
120     var state = cm.state.lint, options = state.options;
121
122     var annotations = groupByLine(annotationsNotSorted);
123
124     for (var line = 0; line < annotations.length; ++line) {
125       var anns = annotations[line];
126       if (!anns) continue;
127
128       var maxSeverity = null;
129       var tipLabel = state.hasGutter && document.createDocumentFragment();
130
131       for (var i = 0; i < anns.length; ++i) {
132         var ann = anns[i];
133         var severity = ann.severity;
134         if (!SEVERITIES.test(severity)) severity = "error";
135         maxSeverity = getMaxSeverity(maxSeverity, severity);
136
137         if (options.formatAnnotation) ann = options.formatAnnotation(ann);
138         if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann));
139
140         if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, {
141           className: "CodeMirror-lint-mark-" + severity,
142           __annotation: ann
143         }));
144       }
145
146       if (state.hasGutter)
147         cm.setGutterMarker(line, GUTTER_ID, makeMarker(tipLabel, maxSeverity, anns.length > 1,
148                                                        state.options.tooltips));
149     }
150     if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
151   }
152
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);
157   }
158
159   function popupSpanTooltip(ann, e) {
160     var target = e.target || e.srcElement;
161     showTooltipFor(e, annotationTooltip(ann), target);
162   }
163
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];
168
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);
177       }
178     }
179   }
180
181   function optionHandler(cm, val, old) {
182     if (old && old != CodeMirror.Init) {
183       clearMarks(cm);
184       cm.off("change", onChange);
185       CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver);
186       delete cm.state.lint;
187     }
188
189     if (val) {
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);
196
197       startLinting(cm);
198     }
199   }
200
201   CodeMirror.defineOption("lintWith", false, optionHandler); // deprecated
202   CodeMirror.defineOption("lint", false, optionHandler); // deprecated
203 })();