lint.js 8.4 KB

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