lint.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. (function() {
  2. "use strict";
  3. var GUTTER_ID = "CodeMirror-lint-markers";
  4. var SEVERITIES = /^(?:error|warning)$/;
  5. function showTooltip(e, content) {
  6. var tt = document.createElement("div");
  7. tt.className = "CodeMirror-lint-tooltip";
  8. tt.appendChild(content.cloneNode(true));
  9. document.body.appendChild(tt);
  10. function position(e) {
  11. if (!tt.parentNode) return CodeMirror.off(document, "mousemove", position);
  12. tt.style.top = Math.max(0, e.clientY - tt.offsetHeight - 5) + "px";
  13. tt.style.left = (e.clientX + 5) + "px";
  14. }
  15. CodeMirror.on(document, "mousemove", position);
  16. position(e);
  17. if (tt.style.opacity != null) tt.style.opacity = 1;
  18. return tt;
  19. }
  20. function rm(elt) {
  21. if (elt.parentNode) elt.parentNode.removeChild(elt);
  22. }
  23. function hideTooltip(tt) {
  24. if (!tt.parentNode) return;
  25. if (tt.style.opacity == null) rm(tt);
  26. tt.style.opacity = 0;
  27. setTimeout(function() { rm(tt); }, 600);
  28. }
  29. function showTooltipFor(e, content, node) {
  30. var tooltip = showTooltip(e, content);
  31. function hide() {
  32. CodeMirror.off(node, "mouseout", hide);
  33. if (tooltip) { hideTooltip(tooltip); tooltip = null; }
  34. }
  35. var poll = setInterval(function() {
  36. if (tooltip) for (var n = node;; n = n.parentNode) {
  37. if (n == document.body) return;
  38. if (!n) { hide(); break; }
  39. }
  40. if (!tooltip) return clearInterval(poll);
  41. }, 400);
  42. CodeMirror.on(node, "mouseout", hide);
  43. }
  44. function LintState(cm, options, hasGutter) {
  45. this.marked = [];
  46. this.options = options;
  47. this.timeout = null;
  48. this.hasGutter = hasGutter;
  49. this.onMouseOver = function(e) { onMouseOver(cm, e); };
  50. }
  51. function parseOptions(cm, options) {
  52. if (options instanceof Function) return {getAnnotations: options};
  53. if (!options || options === true) options = {};
  54. if (!options.getAnnotations) options.getAnnotations = cm.getHelper(CodeMirror.Pos(0, 0), "lint");
  55. if (!options.getAnnotations) throw new Error("Required option 'getAnnotations' missing (lint addon)");
  56. return options;
  57. }
  58. function clearMarks(cm) {
  59. var state = cm.state.lint;
  60. if (state.hasGutter) cm.clearGutter(GUTTER_ID);
  61. for (var i = 0; i < state.marked.length; ++i)
  62. state.marked[i].clear();
  63. state.marked.length = 0;
  64. }
  65. function makeMarker(labels, severity, multiple, tooltips) {
  66. var marker = document.createElement("div"), inner = marker;
  67. marker.className = "CodeMirror-lint-marker-" + severity;
  68. if (multiple) {
  69. inner = marker.appendChild(document.createElement("div"));
  70. inner.className = "CodeMirror-lint-marker-multiple";
  71. }
  72. if (tooltips != false) CodeMirror.on(inner, "mouseover", function(e) {
  73. showTooltipFor(e, labels, inner);
  74. });
  75. return marker;
  76. }
  77. function getMaxSeverity(a, b) {
  78. if (a == "error") return a;
  79. else return b;
  80. }
  81. function groupByLine(annotations) {
  82. var lines = [];
  83. for (var i = 0; i < annotations.length; ++i) {
  84. var ann = annotations[i], line = ann.from.line;
  85. (lines[line] || (lines[line] = [])).push(ann);
  86. }
  87. return lines;
  88. }
  89. function annotationTooltip(ann) {
  90. var severity = ann.severity;
  91. if (!SEVERITIES.test(severity)) severity = "error";
  92. var tip = document.createElement("div");
  93. tip.className = "CodeMirror-lint-message-" + severity;
  94. tip.appendChild(document.createTextNode(ann.message));
  95. return tip;
  96. }
  97. function startLinting(cm) {
  98. var state = cm.state.lint, options = state.options;
  99. if (options.async)
  100. options.getAnnotations(cm, updateLinting, options);
  101. else
  102. updateLinting(cm, options.getAnnotations(cm.getValue(), options));
  103. }
  104. function updateLinting(cm, annotationsNotSorted) {
  105. clearMarks(cm);
  106. var state = cm.state.lint, options = state.options;
  107. var annotations = groupByLine(annotationsNotSorted);
  108. for (var line = 0; line < annotations.length; ++line) {
  109. var anns = annotations[line];
  110. if (!anns) continue;
  111. var maxSeverity = null;
  112. var tipLabel = state.hasGutter && document.createDocumentFragment();
  113. for (var i = 0; i < anns.length; ++i) {
  114. var ann = anns[i];
  115. var severity = ann.severity;
  116. if (!SEVERITIES.test(severity)) severity = "error";
  117. maxSeverity = getMaxSeverity(maxSeverity, severity);
  118. if (options.formatAnnotation) ann = options.formatAnnotation(ann);
  119. if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann));
  120. if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, {
  121. className: "CodeMirror-lint-mark-" + severity,
  122. __annotation: ann
  123. }));
  124. }
  125. if (state.hasGutter)
  126. cm.setGutterMarker(line, GUTTER_ID, makeMarker(tipLabel, maxSeverity, anns.length > 1,
  127. state.options.tooltips));
  128. }
  129. if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
  130. }
  131. function onChange(cm) {
  132. var state = cm.state.lint;
  133. clearTimeout(state.timeout);
  134. state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500);
  135. }
  136. function popupSpanTooltip(ann, e) {
  137. var target = e.target || e.srcElement;
  138. showTooltipFor(e, annotationTooltip(ann), target);
  139. }
  140. // When the mouseover fires, the cursor might not actually be over
  141. // the character itself yet. These pairs of x,y offsets are used to
  142. // probe a few nearby points when no suitable marked range is found.
  143. var nearby = [0, 0, 0, 5, 0, -5, 5, 0, -5, 0];
  144. function onMouseOver(cm, e) {
  145. if (!/\bCodeMirror-lint-mark-/.test((e.target || e.srcElement).className)) return;
  146. for (var i = 0; i < nearby.length; i += 2) {
  147. var spans = cm.findMarksAt(cm.coordsChar({left: e.clientX + nearby[i],
  148. top: e.clientY + nearby[i + 1]}));
  149. for (var j = 0; j < spans.length; ++j) {
  150. var span = spans[j], ann = span.__annotation;
  151. if (ann) return popupSpanTooltip(ann, e);
  152. }
  153. }
  154. }
  155. function optionHandler(cm, val, old) {
  156. if (old && old != CodeMirror.Init) {
  157. clearMarks(cm);
  158. cm.off("change", onChange);
  159. CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver);
  160. delete cm.state.lint;
  161. }
  162. if (val) {
  163. var gutters = cm.getOption("gutters"), hasLintGutter = false;
  164. for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true;
  165. var state = cm.state.lint = new LintState(cm, parseOptions(cm, val), hasLintGutter);
  166. cm.on("change", onChange);
  167. if (state.options.tooltips != false)
  168. CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
  169. startLinting(cm);
  170. }
  171. }
  172. CodeMirror.defineOption("lintWith", false, optionHandler); // deprecated
  173. CodeMirror.defineOption("lint", false, optionHandler); // deprecated
  174. })();