lint.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. var passOptions = options.options || options; // Support deprecated passing of `options` property in options
  108. if (options.async)
  109. options.getAnnotations(cm.getValue(), updateLinting, passOptions, cm);
  110. else
  111. updateLinting(cm, options.getAnnotations(cm.getValue(), passOptions, cm));
  112. }
  113. function updateLinting(cm, annotationsNotSorted) {
  114. clearMarks(cm);
  115. var state = cm.state.lint, options = state.options;
  116. var annotations = groupByLine(annotationsNotSorted);
  117. for (var line = 0; line < annotations.length; ++line) {
  118. var anns = annotations[line];
  119. if (!anns) continue;
  120. var maxSeverity = null;
  121. var tipLabel = state.hasGutter && document.createDocumentFragment();
  122. for (var i = 0; i < anns.length; ++i) {
  123. var ann = anns[i];
  124. var severity = ann.severity;
  125. if (!severity) severity = "error";
  126. maxSeverity = getMaxSeverity(maxSeverity, severity);
  127. if (options.formatAnnotation) ann = options.formatAnnotation(ann);
  128. if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann));
  129. if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, {
  130. className: "CodeMirror-lint-mark-" + severity,
  131. __annotation: ann
  132. }));
  133. }
  134. if (state.hasGutter)
  135. cm.setGutterMarker(line, GUTTER_ID, makeMarker(tipLabel, maxSeverity, anns.length > 1,
  136. state.options.tooltips));
  137. }
  138. if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
  139. }
  140. function onChange(cm) {
  141. var state = cm.state.lint;
  142. clearTimeout(state.timeout);
  143. state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500);
  144. }
  145. function popupSpanTooltip(ann, e) {
  146. var target = e.target || e.srcElement;
  147. showTooltipFor(e, annotationTooltip(ann), target);
  148. }
  149. function onMouseOver(cm, e) {
  150. var target = e.target || e.srcElement;
  151. if (!/\bCodeMirror-lint-mark-/.test(target.className)) return;
  152. var box = target.getBoundingClientRect(), x = (box.left + box.right) / 2, y = (box.top + box.bottom) / 2;
  153. var spans = cm.findMarksAt(cm.coordsChar({left: x, top: y}, "client"));
  154. for (var i = 0; i < spans.length; ++i) {
  155. var ann = spans[i].__annotation;
  156. if (ann) return popupSpanTooltip(ann, e);
  157. }
  158. }
  159. CodeMirror.defineOption("lint", false, function(cm, val, old) {
  160. if (old && old != CodeMirror.Init) {
  161. clearMarks(cm);
  162. cm.off("change", onChange);
  163. CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver);
  164. delete cm.state.lint;
  165. }
  166. if (val) {
  167. var gutters = cm.getOption("gutters"), hasLintGutter = false;
  168. for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true;
  169. var state = cm.state.lint = new LintState(cm, parseOptions(cm, val), hasLintGutter);
  170. cm.on("change", onChange);
  171. if (state.options.tooltips != false)
  172. CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
  173. startLinting(cm);
  174. }
  175. });
  176. });