lint.js 7.0 KB

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