123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- CodeMirror.validate = (function() {
- var GUTTER_ID = "CodeMirror-lint-markers";
- var SEVERITIES = /^(?:error|warning)$/;
- function showTooltip(e, content) {
- var tt = document.createElement("div");
- tt.className = "CodeMirror-lint-tooltip";
- tt.appendChild(content.cloneNode(true));
- document.body.appendChild(tt);
- function position(e) {
- if (!tt.parentNode) return CodeMirror.off(document, "mousemove", position);
- tt.style.top = (e.clientY - tt.offsetHeight - 5) + "px";
- tt.style.left = (e.clientX + 5) + "px";
- }
- CodeMirror.on(document, "mousemove", position);
- position(e);
- 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);
- }
- function LintState(cm, options, hasGutter) {
- this.marked = [];
- this.options = options;
- this.timeout = null;
- this.hasGutter = hasGutter;
- this.onMouseOver = function(e) { onMouseOver(cm, e); };
- }
- function parseOptions(options) {
- if (options instanceof Function) return {getAnnotations: options};
- else if (!options || !options.getAnnotations) throw new Error("Required option 'getAnnotations' missing (lint addon)");
- return options;
- }
- function clearMarks(cm) {
- var state = cm._lintState;
- if (state.hasGutter) cm.clearGutter(GUTTER_ID);
- for (var i = 0; i < state.marked.length; ++i)
- state.marked[i].clear();
- state.marked.length = 0;
- }
- function makeMarker(labels, severity, multiple) {
- var marker = document.createElement("div"), inner = marker;
- marker.className = "CodeMirror-lint-marker-" + severity;
- if (multiple) {
- inner = marker.appendChild(document.createElement("div"));
- inner.className = "CodeMirror-lint-marker-multiple";
- }
- var tooltip;
- CodeMirror.on(inner, "mouseover", function(e) { tooltip = showTooltip(e, labels); });
- CodeMirror.on(inner, "mouseout", function() { if (tooltip) hideTooltip(tooltip); });
- 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 (!SEVERITIES.test(severity)) severity = "error";
- var tip = document.createElement("div");
- tip.className = "CodeMirror-lint-message-" + severity;
- tip.appendChild(document.createTextNode(ann.message));
- return tip;
- }
- function startLinting(cm) {
- var state = cm._lintState, options = state.options;
- if (options.async)
- options.getAnnotations(cm, updateLinting, options);
- else
- updateLinting(cm, options.getAnnotations(cm.getValue()));
- }
-
- function updateLinting(cm, annotationsNotSorted) {
- clearMarks(cm);
- var state = cm._lintState, options = state.options;
- var annotations = groupByLine(annotationsNotSorted);
- for (var line = 0; line < annotations.length; ++line) {
- var anns = annotations[line];
- if (!anns) continue;
- 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 (!SEVERITIES.test(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, {
- className: "CodeMirror-lint-span-" + severity,
- __annotation: ann
- }));
- }
- if (state.hasGutter)
- cm.setGutterMarker(line, GUTTER_ID, makeMarker(tipLabel, maxSeverity, anns.length > 1));
- }
- if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
- }
- function onChange(cm) {
- var state = cm._lintState;
- clearTimeout(state.timeout);
- state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500);
- }
- function popupSpanTooltip(ann, e) {
- var tooltip = showTooltip(e, annotationTooltip(ann));
- var target = e.target || e.srcElement;
- CodeMirror.on(target, "mouseout", hide);
- function hide() {
- CodeMirror.off(target, "mouseout", hide);
- hideTooltip(tooltip);
- }
- }
- // When the mouseover fires, the cursor might not actually be over
- // the character itself yet. These pairs of x,y offsets are used to
- // probe a few nearby points when no suitable marked range is found.
- var nearby = [0, 0, 0, 5, 0, -5, 5, 0, -5, 0];
- function onMouseOver(cm, e) {
- if (!/\bCodeMirror-lint-span-/.test((e.target || e.srcElement).className)) return;
- for (var i = 0; i < nearby.length; i += 2) {
- var spans = cm.findMarksAt(cm.coordsChar({left: e.clientX + nearby[i],
- top: e.clientY + nearby[i + 1]}));
- for (var j = 0; j < spans.length; ++j) {
- var span = spans[j], ann = span.__annotation;
- if (ann) return popupSpanTooltip(ann, e);
- }
- }
- }
- CodeMirror.defineOption("lintWith", false, function(cm, val, old) {
- if (old && old != CodeMirror.Init) {
- clearMarks(cm);
- cm.off("change", onChange);
- CodeMirror.off(cm.getWrapperElement(), "mouseover", cm._lintState.onMouseOver);
- delete cm._lintState;
- }
-
- if (val) {
- var gutters = cm.getOption("gutters"), hasLintGutter = false;
- for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true;
- var state = cm._lintState = new LintState(cm, parseOptions(val), hasLintGutter);
- cm.on("change", onChange);
- CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
- startLinting(cm);
- }
- });
- })();
|