lint.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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. tip.appendChild(document.createTextNode(ann.message));
  103. return tip;
  104. }
  105. function lintAsync(cm, getAnnotations, passOptions) {
  106. var state = cm.state.lint
  107. var id = ++state.waitingFor
  108. function abort() {
  109. id = -1
  110. cm.off("change", abort)
  111. }
  112. cm.on("change", abort)
  113. getAnnotations(cm.getValue(), function(annotations, arg2) {
  114. cm.off("change", abort)
  115. if (state.waitingFor != id) return
  116. if (arg2 && annotations instanceof CodeMirror) annotations = arg2
  117. updateLinting(cm, annotations)
  118. }, passOptions, cm);
  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. var getAnnotations = options.getAnnotations || cm.getHelper(CodeMirror.Pos(0, 0), "lint");
  124. if (!getAnnotations) return;
  125. if (options.async || getAnnotations.async) {
  126. lintAsync(cm, getAnnotations, passOptions)
  127. } else {
  128. updateLinting(cm, getAnnotations(cm.getValue(), passOptions, cm));
  129. }
  130. }
  131. function updateLinting(cm, annotationsNotSorted) {
  132. clearMarks(cm);
  133. var state = cm.state.lint, options = state.options;
  134. var annotations = groupByLine(annotationsNotSorted);
  135. for (var line = 0; line < annotations.length; ++line) {
  136. var anns = annotations[line];
  137. if (!anns) continue;
  138. var maxSeverity = null;
  139. var tipLabel = state.hasGutter && document.createDocumentFragment();
  140. for (var i = 0; i < anns.length; ++i) {
  141. var ann = anns[i];
  142. var severity = ann.severity;
  143. if (!severity) severity = "error";
  144. maxSeverity = getMaxSeverity(maxSeverity, severity);
  145. if (options.formatAnnotation) ann = options.formatAnnotation(ann);
  146. if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann));
  147. if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, {
  148. className: "CodeMirror-lint-mark-" + severity,
  149. __annotation: ann
  150. }));
  151. }
  152. if (state.hasGutter)
  153. cm.setGutterMarker(line, GUTTER_ID, makeMarker(tipLabel, maxSeverity, anns.length > 1,
  154. state.options.tooltips));
  155. }
  156. if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
  157. }
  158. function onChange(cm) {
  159. var state = cm.state.lint;
  160. if (!state) return;
  161. clearTimeout(state.timeout);
  162. state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500);
  163. }
  164. function popupTooltips(annotations, e) {
  165. var target = e.target || e.srcElement;
  166. var tooltip = document.createDocumentFragment();
  167. for (var i = 0; i < annotations.length; i++) {
  168. var ann = annotations[i];
  169. tooltip.appendChild(annotationTooltip(ann));
  170. }
  171. showTooltipFor(e, tooltip, target);
  172. }
  173. function onMouseOver(cm, e) {
  174. var target = e.target || e.srcElement;
  175. if (!/\bCodeMirror-lint-mark-/.test(target.className)) return;
  176. var box = target.getBoundingClientRect(), x = (box.left + box.right) / 2, y = (box.top + box.bottom) / 2;
  177. var spans = cm.findMarksAt(cm.coordsChar({left: x, top: y}, "client"));
  178. var annotations = [];
  179. for (var i = 0; i < spans.length; ++i) {
  180. var ann = spans[i].__annotation;
  181. if (ann) annotations.push(ann);
  182. }
  183. if (annotations.length) popupTooltips(annotations, e);
  184. }
  185. CodeMirror.defineOption("lint", false, function(cm, val, old) {
  186. if (old && old != CodeMirror.Init) {
  187. clearMarks(cm);
  188. if (cm.state.lint.options.lintOnChange !== false)
  189. cm.off("change", onChange);
  190. CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver);
  191. clearTimeout(cm.state.lint.timeout);
  192. delete cm.state.lint;
  193. }
  194. if (val) {
  195. var gutters = cm.getOption("gutters"), hasLintGutter = false;
  196. for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true;
  197. var state = cm.state.lint = new LintState(cm, parseOptions(cm, val), hasLintGutter);
  198. if (state.options.lintOnChange !== false)
  199. cm.on("change", onChange);
  200. if (state.options.tooltips != false && state.options.tooltips != "gutter")
  201. CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
  202. startLinting(cm);
  203. }
  204. });
  205. CodeMirror.defineExtension("performLint", function() {
  206. if (this.state.lint) startLinting(this);
  207. });
  208. });