lint.js 6.5 KB

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