coffeescript-lint.js
11 years ago
coffeescript-lint.min.js
5 years ago
css-lint.js
9 years ago
css-lint.min.js
5 years ago
html-lint.js
10 years ago
html-lint.min.js
5 years ago
javascript-lint.js
11 years ago
javascript-lint.min.js
5 years ago
json-lint.js
11 years ago
json-lint.min.js
5 years ago
lint.css
9 years ago
lint.js
9 years ago
lint.min.js
5 years ago
yaml-lint.js
9 years ago
yaml-lint.min.js
5 years ago
lint.js
245 lines
| 1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others |
| 2 | // Distributed under an MIT license: http://codemirror.net/LICENSE |
| 3 | |
| 4 | (function(mod) { |
| 5 | if (typeof exports == "object" && typeof module == "object") // CommonJS |
| 6 | mod(require("../../lib/codemirror")); |
| 7 | else if (typeof define == "function" && define.amd) // AMD |
| 8 | define(["../../lib/codemirror"], mod); |
| 9 | else // Plain browser env |
| 10 | mod(CodeMirror); |
| 11 | })(function(CodeMirror) { |
| 12 | "use strict"; |
| 13 | var GUTTER_ID = "CodeMirror-lint-markers"; |
| 14 | |
| 15 | function showTooltip(e, content) { |
| 16 | var tt = document.createElement("div"); |
| 17 | tt.className = "CodeMirror-lint-tooltip"; |
| 18 | tt.appendChild(content.cloneNode(true)); |
| 19 | document.body.appendChild(tt); |
| 20 | |
| 21 | function position(e) { |
| 22 | if (!tt.parentNode) return CodeMirror.off(document, "mousemove", position); |
| 23 | tt.style.top = Math.max(0, e.clientY - tt.offsetHeight - 5) + "px"; |
| 24 | tt.style.left = (e.clientX + 5) + "px"; |
| 25 | } |
| 26 | CodeMirror.on(document, "mousemove", position); |
| 27 | position(e); |
| 28 | if (tt.style.opacity != null) tt.style.opacity = 1; |
| 29 | return tt; |
| 30 | } |
| 31 | function rm(elt) { |
| 32 | if (elt.parentNode) elt.parentNode.removeChild(elt); |
| 33 | } |
| 34 | function hideTooltip(tt) { |
| 35 | if (!tt.parentNode) return; |
| 36 | if (tt.style.opacity == null) rm(tt); |
| 37 | tt.style.opacity = 0; |
| 38 | setTimeout(function() { rm(tt); }, 600); |
| 39 | } |
| 40 | |
| 41 | function showTooltipFor(e, content, node) { |
| 42 | var tooltip = showTooltip(e, content); |
| 43 | function hide() { |
| 44 | CodeMirror.off(node, "mouseout", hide); |
| 45 | if (tooltip) { hideTooltip(tooltip); tooltip = null; } |
| 46 | } |
| 47 | var poll = setInterval(function() { |
| 48 | if (tooltip) for (var n = node;; n = n.parentNode) { |
| 49 | if (n && n.nodeType == 11) n = n.host; |
| 50 | if (n == document.body) return; |
| 51 | if (!n) { hide(); break; } |
| 52 | } |
| 53 | if (!tooltip) return clearInterval(poll); |
| 54 | }, 400); |
| 55 | CodeMirror.on(node, "mouseout", hide); |
| 56 | } |
| 57 | |
| 58 | function LintState(cm, options, hasGutter) { |
| 59 | this.marked = []; |
| 60 | this.options = options; |
| 61 | this.timeout = null; |
| 62 | this.hasGutter = hasGutter; |
| 63 | this.onMouseOver = function(e) { onMouseOver(cm, e); }; |
| 64 | this.waitingFor = 0 |
| 65 | } |
| 66 | |
| 67 | function parseOptions(_cm, options) { |
| 68 | if (options instanceof Function) return {getAnnotations: options}; |
| 69 | if (!options || options === true) options = {}; |
| 70 | return options; |
| 71 | } |
| 72 | |
| 73 | function clearMarks(cm) { |
| 74 | var state = cm.state.lint; |
| 75 | if (state.hasGutter) cm.clearGutter(GUTTER_ID); |
| 76 | for (var i = 0; i < state.marked.length; ++i) |
| 77 | state.marked[i].clear(); |
| 78 | state.marked.length = 0; |
| 79 | } |
| 80 | |
| 81 | function makeMarker(labels, severity, multiple, tooltips) { |
| 82 | var marker = document.createElement("div"), inner = marker; |
| 83 | marker.className = "CodeMirror-lint-marker-" + severity; |
| 84 | if (multiple) { |
| 85 | inner = marker.appendChild(document.createElement("div")); |
| 86 | inner.className = "CodeMirror-lint-marker-multiple"; |
| 87 | } |
| 88 | |
| 89 | if (tooltips != false) CodeMirror.on(inner, "mouseover", function(e) { |
| 90 | showTooltipFor(e, labels, inner); |
| 91 | }); |
| 92 | |
| 93 | return marker; |
| 94 | } |
| 95 | |
| 96 | function getMaxSeverity(a, b) { |
| 97 | if (a == "error") return a; |
| 98 | else return b; |
| 99 | } |
| 100 | |
| 101 | function groupByLine(annotations) { |
| 102 | var lines = []; |
| 103 | for (var i = 0; i < annotations.length; ++i) { |
| 104 | var ann = annotations[i], line = ann.from.line; |
| 105 | (lines[line] || (lines[line] = [])).push(ann); |
| 106 | } |
| 107 | return lines; |
| 108 | } |
| 109 | |
| 110 | function annotationTooltip(ann) { |
| 111 | var severity = ann.severity; |
| 112 | if (!severity) severity = "error"; |
| 113 | var tip = document.createElement("div"); |
| 114 | tip.className = "CodeMirror-lint-message-" + severity; |
| 115 | tip.appendChild(document.createTextNode(ann.message)); |
| 116 | return tip; |
| 117 | } |
| 118 | |
| 119 | function lintAsync(cm, getAnnotations, passOptions) { |
| 120 | var state = cm.state.lint |
| 121 | var id = ++state.waitingFor |
| 122 | function abort() { |
| 123 | id = -1 |
| 124 | cm.off("change", abort) |
| 125 | } |
| 126 | cm.on("change", abort) |
| 127 | getAnnotations(cm.getValue(), function(annotations, arg2) { |
| 128 | cm.off("change", abort) |
| 129 | if (state.waitingFor != id) return |
| 130 | if (arg2 && annotations instanceof CodeMirror) annotations = arg2 |
| 131 | updateLinting(cm, annotations) |
| 132 | }, passOptions, cm); |
| 133 | } |
| 134 | |
| 135 | function startLinting(cm) { |
| 136 | var state = cm.state.lint, options = state.options; |
| 137 | var passOptions = options.options || options; // Support deprecated passing of `options` property in options |
| 138 | var getAnnotations = options.getAnnotations || cm.getHelper(CodeMirror.Pos(0, 0), "lint"); |
| 139 | if (!getAnnotations) return; |
| 140 | if (options.async || getAnnotations.async) { |
| 141 | lintAsync(cm, getAnnotations, passOptions) |
| 142 | } else { |
| 143 | var annotations = getAnnotations(cm.getValue(), passOptions, cm); |
| 144 | if (!annotations) return; |
| 145 | if (annotations.then) annotations.then(function(issues) { |
| 146 | updateLinting(cm, issues); |
| 147 | }); |
| 148 | else updateLinting(cm, annotations); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | function updateLinting(cm, annotationsNotSorted) { |
| 153 | clearMarks(cm); |
| 154 | var state = cm.state.lint, options = state.options; |
| 155 | |
| 156 | var annotations = groupByLine(annotationsNotSorted); |
| 157 | |
| 158 | for (var line = 0; line < annotations.length; ++line) { |
| 159 | var anns = annotations[line]; |
| 160 | if (!anns) continue; |
| 161 | |
| 162 | var maxSeverity = null; |
| 163 | var tipLabel = state.hasGutter && document.createDocumentFragment(); |
| 164 | |
| 165 | for (var i = 0; i < anns.length; ++i) { |
| 166 | var ann = anns[i]; |
| 167 | var severity = ann.severity; |
| 168 | if (!severity) severity = "error"; |
| 169 | maxSeverity = getMaxSeverity(maxSeverity, severity); |
| 170 | |
| 171 | if (options.formatAnnotation) ann = options.formatAnnotation(ann); |
| 172 | if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann)); |
| 173 | |
| 174 | if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, { |
| 175 | className: "CodeMirror-lint-mark-" + severity, |
| 176 | __annotation: ann |
| 177 | })); |
| 178 | } |
| 179 | |
| 180 | if (state.hasGutter) |
| 181 | cm.setGutterMarker(line, GUTTER_ID, makeMarker(tipLabel, maxSeverity, anns.length > 1, |
| 182 | state.options.tooltips)); |
| 183 | } |
| 184 | if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm); |
| 185 | } |
| 186 | |
| 187 | function onChange(cm) { |
| 188 | var state = cm.state.lint; |
| 189 | if (!state) return; |
| 190 | clearTimeout(state.timeout); |
| 191 | state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500); |
| 192 | } |
| 193 | |
| 194 | function popupTooltips(annotations, e) { |
| 195 | var target = e.target || e.srcElement; |
| 196 | var tooltip = document.createDocumentFragment(); |
| 197 | for (var i = 0; i < annotations.length; i++) { |
| 198 | var ann = annotations[i]; |
| 199 | tooltip.appendChild(annotationTooltip(ann)); |
| 200 | } |
| 201 | showTooltipFor(e, tooltip, target); |
| 202 | } |
| 203 | |
| 204 | function onMouseOver(cm, e) { |
| 205 | var target = e.target || e.srcElement; |
| 206 | if (!/\bCodeMirror-lint-mark-/.test(target.className)) return; |
| 207 | var box = target.getBoundingClientRect(), x = (box.left + box.right) / 2, y = (box.top + box.bottom) / 2; |
| 208 | var spans = cm.findMarksAt(cm.coordsChar({left: x, top: y}, "client")); |
| 209 | |
| 210 | var annotations = []; |
| 211 | for (var i = 0; i < spans.length; ++i) { |
| 212 | var ann = spans[i].__annotation; |
| 213 | if (ann) annotations.push(ann); |
| 214 | } |
| 215 | if (annotations.length) popupTooltips(annotations, e); |
| 216 | } |
| 217 | |
| 218 | CodeMirror.defineOption("lint", false, function(cm, val, old) { |
| 219 | if (old && old != CodeMirror.Init) { |
| 220 | clearMarks(cm); |
| 221 | if (cm.state.lint.options.lintOnChange !== false) |
| 222 | cm.off("change", onChange); |
| 223 | CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver); |
| 224 | clearTimeout(cm.state.lint.timeout); |
| 225 | delete cm.state.lint; |
| 226 | } |
| 227 | |
| 228 | if (val) { |
| 229 | var gutters = cm.getOption("gutters"), hasLintGutter = false; |
| 230 | for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true; |
| 231 | var state = cm.state.lint = new LintState(cm, parseOptions(cm, val), hasLintGutter); |
| 232 | if (state.options.lintOnChange !== false) |
| 233 | cm.on("change", onChange); |
| 234 | if (state.options.tooltips != false && state.options.tooltips != "gutter") |
| 235 | CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver); |
| 236 | |
| 237 | startLinting(cm); |
| 238 | } |
| 239 | }); |
| 240 | |
| 241 | CodeMirror.defineExtension("performLint", function() { |
| 242 | if (this.state.lint) startLinting(this); |
| 243 | }); |
| 244 | }); |
| 245 |