lint.js 6.1 KB

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