coffeescript-lint.js
11 years ago
coffeescript-lint.min.js
11 years ago
css-lint.js
11 years ago
css-lint.min.js
11 years ago
javascript-lint.js
11 years ago
javascript-lint.min.js
11 years ago
json-lint.js
11 years ago
json-lint.min.js
11 years ago
lint.css
11 years ago
lint.js
11 years ago
lint.min.js
11 years ago
yaml-lint.js
11 years ago
yaml-lint.min.js
11 years ago
lint.js
208 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 | } |
| 65 | |
| 66 | function parseOptions(cm, options) { |
| 67 | if (options instanceof Function) return {getAnnotations: options}; |
| 68 | if (!options || options === true) options = {}; |
| 69 | if (!options.getAnnotations) options.getAnnotations = cm.getHelper(CodeMirror.Pos(0, 0), "lint"); |
| 70 | if (!options.getAnnotations) throw new Error("Required option 'getAnnotations' missing (lint addon)"); |
| 71 | return options; |
| 72 | } |
| 73 | |
| 74 | function clearMarks(cm) { |
| 75 | var state = cm.state.lint; |
| 76 | if (state.hasGutter) cm.clearGutter(GUTTER_ID); |
| 77 | for (var i = 0; i < state.marked.length; ++i) |
| 78 | state.marked[i].clear(); |
| 79 | state.marked.length = 0; |
| 80 | } |
| 81 | |
| 82 | function makeMarker(labels, severity, multiple, tooltips) { |
| 83 | var marker = document.createElement("div"), inner = marker; |
| 84 | marker.className = "CodeMirror-lint-marker-" + severity; |
| 85 | if (multiple) { |
| 86 | inner = marker.appendChild(document.createElement("div")); |
| 87 | inner.className = "CodeMirror-lint-marker-multiple"; |
| 88 | } |
| 89 | |
| 90 | if (tooltips != false) CodeMirror.on(inner, "mouseover", function(e) { |
| 91 | showTooltipFor(e, labels, inner); |
| 92 | }); |
| 93 | |
| 94 | return marker; |
| 95 | } |
| 96 | |
| 97 | function getMaxSeverity(a, b) { |
| 98 | if (a == "error") return a; |
| 99 | else return b; |
| 100 | } |
| 101 | |
| 102 | function groupByLine(annotations) { |
| 103 | var lines = []; |
| 104 | for (var i = 0; i < annotations.length; ++i) { |
| 105 | var ann = annotations[i], line = ann.from.line; |
| 106 | (lines[line] || (lines[line] = [])).push(ann); |
| 107 | } |
| 108 | return lines; |
| 109 | } |
| 110 | |
| 111 | function annotationTooltip(ann) { |
| 112 | var severity = ann.severity; |
| 113 | if (!severity) severity = "error"; |
| 114 | var tip = document.createElement("div"); |
| 115 | tip.className = "CodeMirror-lint-message-" + severity; |
| 116 | tip.appendChild(document.createTextNode(ann.message)); |
| 117 | return tip; |
| 118 | } |
| 119 | |
| 120 | function startLinting(cm) { |
| 121 | var state = cm.state.lint, options = state.options; |
| 122 | var passOptions = options.options || options; // Support deprecated passing of `options` property in options |
| 123 | if (options.async || options.getAnnotations.async) |
| 124 | options.getAnnotations(cm.getValue(), updateLinting, passOptions, cm); |
| 125 | else |
| 126 | updateLinting(cm, options.getAnnotations(cm.getValue(), passOptions, cm)); |
| 127 | } |
| 128 | |
| 129 | function updateLinting(cm, annotationsNotSorted) { |
| 130 | clearMarks(cm); |
| 131 | var state = cm.state.lint, options = state.options; |
| 132 | |
| 133 | var annotations = groupByLine(annotationsNotSorted); |
| 134 | |
| 135 | for (var line = 0; line < annotations.length; ++line) { |
| 136 | var anns = annotations[line]; |
| 137 | if (!anns) continue; |
| 138 | |
| 139 | var maxSeverity = null; |
| 140 | var tipLabel = state.hasGutter && document.createDocumentFragment(); |
| 141 | |
| 142 | for (var i = 0; i < anns.length; ++i) { |
| 143 | var ann = anns[i]; |
| 144 | var severity = ann.severity; |
| 145 | if (!severity) severity = "error"; |
| 146 | maxSeverity = getMaxSeverity(maxSeverity, severity); |
| 147 | |
| 148 | if (options.formatAnnotation) ann = options.formatAnnotation(ann); |
| 149 | if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann)); |
| 150 | |
| 151 | if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, { |
| 152 | className: "CodeMirror-lint-mark-" + severity, |
| 153 | __annotation: ann |
| 154 | })); |
| 155 | } |
| 156 | |
| 157 | if (state.hasGutter) |
| 158 | cm.setGutterMarker(line, GUTTER_ID, makeMarker(tipLabel, maxSeverity, anns.length > 1, |
| 159 | state.options.tooltips)); |
| 160 | } |
| 161 | if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm); |
| 162 | } |
| 163 | |
| 164 | function onChange(cm) { |
| 165 | var state = cm.state.lint; |
| 166 | if (!state) return; |
| 167 | clearTimeout(state.timeout); |
| 168 | state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500); |
| 169 | } |
| 170 | |
| 171 | function popupSpanTooltip(ann, e) { |
| 172 | var target = e.target || e.srcElement; |
| 173 | showTooltipFor(e, annotationTooltip(ann), target); |
| 174 | } |
| 175 | |
| 176 | function onMouseOver(cm, e) { |
| 177 | var target = e.target || e.srcElement; |
| 178 | if (!/\bCodeMirror-lint-mark-/.test(target.className)) return; |
| 179 | var box = target.getBoundingClientRect(), x = (box.left + box.right) / 2, y = (box.top + box.bottom) / 2; |
| 180 | var spans = cm.findMarksAt(cm.coordsChar({left: x, top: y}, "client")); |
| 181 | for (var i = 0; i < spans.length; ++i) { |
| 182 | var ann = spans[i].__annotation; |
| 183 | if (ann) return popupSpanTooltip(ann, e); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | CodeMirror.defineOption("lint", false, function(cm, val, old) { |
| 188 | if (old && old != CodeMirror.Init) { |
| 189 | clearMarks(cm); |
| 190 | cm.off("change", onChange); |
| 191 | CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver); |
| 192 | clearTimeout(cm.state.lint.timeout); |
| 193 | delete cm.state.lint; |
| 194 | } |
| 195 | |
| 196 | if (val) { |
| 197 | var gutters = cm.getOption("gutters"), hasLintGutter = false; |
| 198 | for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true; |
| 199 | var state = cm.state.lint = new LintState(cm, parseOptions(cm, val), hasLintGutter); |
| 200 | cm.on("change", onChange); |
| 201 | if (state.options.tooltips != false) |
| 202 | CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver); |
| 203 | |
| 204 | startLinting(cm); |
| 205 | } |
| 206 | }); |
| 207 | }); |
| 208 |