lint.js 7.0 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 == document.body) return;
  46. if (!n) { hide(); break; }
  47. }
  48. if (!tooltip) return clearInterval(poll);
  49. }, 400);
  50. CodeMirror.on(node, "mouseout", hide);
  51. }
  52. function LintState(cm, options, hasGutter) {
  53. this.marked = [];
  54. this.options = options;
  55. this.timeout = null;
  56. this.hasGutter = hasGutter;
  57. this.onMouseOver = function(e) { onMouseOver(cm, e); };
  58. }
  59. function parseOptions(cm, options) {
  60. if (options instanceof Function) return {getAnnotations: options};
  61. if (!options || options === true) options = {};
  62. if (!options.getAnnotations) options.getAnnotations = cm.getHelper(CodeMirror.Pos(0, 0), "lint");
  63. if (!options.getAnnotations) throw new Error("Required option 'getAnnotations' missing (lint addon)");
  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 startLinting(cm) {
  106. var state = cm.state.lint, options = state.options;
  107. if (options.async)
  108. options.getAnnotations(cm, updateLinting, options);
  109. else
  110. updateLinting(cm, options.getAnnotations(cm.getValue(), options.options));
  111. }
  112. function updateLinting(cm, annotationsNotSorted) {
  113. clearMarks(cm);
  114. var state = cm.state.lint, options = state.options;
  115. var annotations = groupByLine(annotationsNotSorted);
  116. for (var line = 0; line < annotations.length; ++line) {
  117. var anns = annotations[line];
  118. if (!anns) continue;
  119. var maxSeverity = null;
  120. var tipLabel = state.hasGutter && document.createDocumentFragment();
  121. for (var i = 0; i < anns.length; ++i) {
  122. var ann = anns[i];
  123. var severity = ann.severity;
  124. if (!severity) severity = "error";
  125. maxSeverity = getMaxSeverity(maxSeverity, severity);
  126. if (options.formatAnnotation) ann = options.formatAnnotation(ann);
  127. if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann));
  128. if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, {
  129. className: "CodeMirror-lint-mark-" + severity,
  130. __annotation: ann
  131. }));
  132. }
  133. if (state.hasGutter)
  134. cm.setGutterMarker(line, GUTTER_ID, makeMarker(tipLabel, maxSeverity, anns.length > 1,
  135. state.options.tooltips));
  136. }
  137. if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
  138. }
  139. function onChange(cm) {
  140. var state = cm.state.lint;
  141. clearTimeout(state.timeout);
  142. state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500);
  143. }
  144. function popupSpanTooltip(ann, e) {
  145. var target = e.target || e.srcElement;
  146. showTooltipFor(e, annotationTooltip(ann), target);
  147. }
  148. // When the mouseover fires, the cursor might not actually be over
  149. // the character itself yet. These pairs of x,y offsets are used to
  150. // probe a few nearby points when no suitable marked range is found.
  151. var nearby = [0, 0, 0, 5, 0, -5, 5, 0, -5, 0];
  152. function onMouseOver(cm, e) {
  153. if (!/\bCodeMirror-lint-mark-/.test((e.target || e.srcElement).className)) return;
  154. for (var i = 0; i < nearby.length; i += 2) {
  155. var spans = cm.findMarksAt(cm.coordsChar({left: e.clientX + nearby[i],
  156. top: e.clientY + nearby[i + 1]}, "client"));
  157. for (var j = 0; j < spans.length; ++j) {
  158. var span = spans[j], ann = span.__annotation;
  159. if (ann) return popupSpanTooltip(ann, e);
  160. }
  161. }
  162. }
  163. CodeMirror.defineOption("lint", false, function(cm, val, old) {
  164. if (old && old != CodeMirror.Init) {
  165. clearMarks(cm);
  166. cm.off("change", onChange);
  167. CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver);
  168. delete cm.state.lint;
  169. }
  170. if (val) {
  171. var gutters = cm.getOption("gutters"), hasLintGutter = false;
  172. for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true;
  173. var state = cm.state.lint = new LintState(cm, parseOptions(cm, val), hasLintGutter);
  174. cm.on("change", onChange);
  175. if (state.options.tooltips != false)
  176. CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
  177. startLinting(cm);
  178. }
  179. });
  180. });