lint.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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(cm, e, content) {
  14. var tt = document.createElement("div");
  15. tt.className = "CodeMirror-lint-tooltip cm-s-" + cm.options.theme;
  16. tt.appendChild(content.cloneNode(true));
  17. if (cm.state.lint.options.selfContain)
  18. cm.getWrapperElement().appendChild(tt);
  19. else
  20. document.body.appendChild(tt);
  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. function showTooltipFor(cm, e, content, node) {
  41. var tooltip = showTooltip(cm, e, content);
  42. function hide() {
  43. CodeMirror.off(node, "mouseout", hide);
  44. if (tooltip) { hideTooltip(tooltip); tooltip = null; }
  45. }
  46. var poll = setInterval(function() {
  47. if (tooltip) for (var n = node;; n = n.parentNode) {
  48. if (n && n.nodeType == 11) n = n.host;
  49. if (n == document.body) return;
  50. if (!n) { hide(); break; }
  51. }
  52. if (!tooltip) return clearInterval(poll);
  53. }, 400);
  54. CodeMirror.on(node, "mouseout", hide);
  55. }
  56. function LintState(cm, options, hasGutter) {
  57. this.marked = [];
  58. this.options = options;
  59. this.timeout = null;
  60. this.hasGutter = hasGutter;
  61. this.onMouseOver = function(e) { onMouseOver(cm, e); };
  62. this.waitingFor = 0
  63. }
  64. function parseOptions(_cm, options) {
  65. if (options instanceof Function) return {getAnnotations: options};
  66. if (!options || options === true) options = {};
  67. return options;
  68. }
  69. function clearMarks(cm) {
  70. var state = cm.state.lint;
  71. if (state.hasGutter) cm.clearGutter(GUTTER_ID);
  72. for (var i = 0; i < state.marked.length; ++i)
  73. state.marked[i].clear();
  74. state.marked.length = 0;
  75. }
  76. function makeMarker(cm, labels, severity, multiple, tooltips) {
  77. var marker = document.createElement("div"), inner = marker;
  78. marker.className = "CodeMirror-lint-marker CodeMirror-lint-marker-" + severity;
  79. if (multiple) {
  80. inner = marker.appendChild(document.createElement("div"));
  81. inner.className = "CodeMirror-lint-marker CodeMirror-lint-marker-multiple";
  82. }
  83. if (tooltips != false) CodeMirror.on(inner, "mouseover", function(e) {
  84. showTooltipFor(cm, e, labels, inner);
  85. });
  86. return marker;
  87. }
  88. function getMaxSeverity(a, b) {
  89. if (a == "error") return a;
  90. else return b;
  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. function annotationTooltip(ann) {
  101. var severity = ann.severity;
  102. if (!severity) severity = "error";
  103. var tip = document.createElement("div");
  104. tip.className = "CodeMirror-lint-message CodeMirror-lint-message-" + severity;
  105. if (typeof ann.messageHTML != 'undefined') {
  106. tip.innerHTML = ann.messageHTML;
  107. } else {
  108. tip.appendChild(document.createTextNode(ann.message));
  109. }
  110. return tip;
  111. }
  112. function lintAsync(cm, getAnnotations, passOptions) {
  113. var state = cm.state.lint
  114. var id = ++state.waitingFor
  115. function abort() {
  116. id = -1
  117. cm.off("change", abort)
  118. }
  119. cm.on("change", abort)
  120. getAnnotations(cm.getValue(), function(annotations, arg2) {
  121. cm.off("change", abort)
  122. if (state.waitingFor != id) return
  123. if (arg2 && annotations instanceof CodeMirror) annotations = arg2
  124. cm.operation(function() {updateLinting(cm, annotations)})
  125. }, passOptions, cm);
  126. }
  127. function startLinting(cm) {
  128. var state = cm.state.lint, options = state.options;
  129. /*
  130. * Passing rules in `options` property prevents JSHint (and other linters) from complaining
  131. * about unrecognized rules like `onUpdateLinting`, `delay`, `lintOnChange`, etc.
  132. */
  133. var passOptions = options.options || options;
  134. var getAnnotations = options.getAnnotations || cm.getHelper(CodeMirror.Pos(0, 0), "lint");
  135. if (!getAnnotations) return;
  136. if (options.async || getAnnotations.async) {
  137. lintAsync(cm, getAnnotations, passOptions)
  138. } else {
  139. var annotations = getAnnotations(cm.getValue(), passOptions, cm);
  140. if (!annotations) return;
  141. if (annotations.then) annotations.then(function(issues) {
  142. cm.operation(function() {updateLinting(cm, issues)})
  143. });
  144. else cm.operation(function() {updateLinting(cm, annotations)})
  145. }
  146. }
  147. function updateLinting(cm, annotationsNotSorted) {
  148. clearMarks(cm);
  149. var state = cm.state.lint, options = state.options;
  150. var annotations = groupByLine(annotationsNotSorted);
  151. for (var line = 0; line < annotations.length; ++line) {
  152. var anns = annotations[line];
  153. if (!anns) continue;
  154. // filter out duplicate messages
  155. var message = [];
  156. anns = anns.filter(function(item) { return message.indexOf(item.message) > -1 ? false : message.push(item.message) });
  157. var maxSeverity = null;
  158. var tipLabel = state.hasGutter && document.createDocumentFragment();
  159. for (var i = 0; i < anns.length; ++i) {
  160. var ann = anns[i];
  161. var severity = ann.severity;
  162. if (!severity) severity = "error";
  163. maxSeverity = getMaxSeverity(maxSeverity, severity);
  164. if (options.formatAnnotation) ann = options.formatAnnotation(ann);
  165. if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann));
  166. if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, {
  167. className: "CodeMirror-lint-mark CodeMirror-lint-mark-" + severity,
  168. __annotation: ann
  169. }));
  170. }
  171. // use original annotations[line] to show multiple messages
  172. if (state.hasGutter)
  173. cm.setGutterMarker(line, GUTTER_ID, makeMarker(cm, tipLabel, maxSeverity, annotations[line].length > 1,
  174. state.options.tooltips));
  175. }
  176. if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
  177. }
  178. function onChange(cm) {
  179. var state = cm.state.lint;
  180. if (!state) return;
  181. clearTimeout(state.timeout);
  182. state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500);
  183. }
  184. function popupTooltips(cm, annotations, e) {
  185. var target = e.target || e.srcElement;
  186. var tooltip = document.createDocumentFragment();
  187. for (var i = 0; i < annotations.length; i++) {
  188. var ann = annotations[i];
  189. tooltip.appendChild(annotationTooltip(ann));
  190. }
  191. showTooltipFor(cm, e, tooltip, target);
  192. }
  193. function onMouseOver(cm, e) {
  194. var target = e.target || e.srcElement;
  195. if (!/\bCodeMirror-lint-mark-/.test(target.className)) return;
  196. var box = target.getBoundingClientRect(), x = (box.left + box.right) / 2, y = (box.top + box.bottom) / 2;
  197. var spans = cm.findMarksAt(cm.coordsChar({left: x, top: y}, "client"));
  198. var annotations = [];
  199. for (var i = 0; i < spans.length; ++i) {
  200. var ann = spans[i].__annotation;
  201. if (ann) annotations.push(ann);
  202. }
  203. if (annotations.length) popupTooltips(cm, annotations, e);
  204. }
  205. CodeMirror.defineOption("lint", false, function(cm, val, old) {
  206. if (old && old != CodeMirror.Init) {
  207. clearMarks(cm);
  208. if (cm.state.lint.options.lintOnChange !== false)
  209. cm.off("change", onChange);
  210. CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver);
  211. clearTimeout(cm.state.lint.timeout);
  212. delete cm.state.lint;
  213. }
  214. if (val) {
  215. var gutters = cm.getOption("gutters"), hasLintGutter = false;
  216. for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true;
  217. var state = cm.state.lint = new LintState(cm, parseOptions(cm, val), hasLintGutter);
  218. if (state.options.lintOnChange !== false)
  219. cm.on("change", onChange);
  220. if (state.options.tooltips != false && state.options.tooltips != "gutter")
  221. CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
  222. startLinting(cm);
  223. }
  224. });
  225. CodeMirror.defineExtension("performLint", function() {
  226. if (this.state.lint) startLinting(this);
  227. });
  228. });