lint.js 7.1 KB

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