No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

205 líneas
6.9 KiB

hace 3 años
  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 && n.nodeType == 11) n = n.host;
  46. if (n == document.body) return;
  47. if (!n) { hide(); break; }
  48. }
  49. if (!tooltip) return clearInterval(poll);
  50. }, 400);
  51. CodeMirror.on(node, "mouseout", hide);
  52. }
  53. function LintState(cm, options, hasGutter) {
  54. this.marked = [];
  55. this.options = options;
  56. this.timeout = null;
  57. this.hasGutter = hasGutter;
  58. this.onMouseOver = function(e) { onMouseOver(cm, e); };
  59. }
  60. function parseOptions(cm, options) {
  61. if (options instanceof Function) return {getAnnotations: options};
  62. if (!options || options === true) options = {};
  63. if (!options.getAnnotations) options.getAnnotations = cm.getHelper(CodeMirror.Pos(0, 0), "lint");
  64. if (!options.getAnnotations) throw new Error("Required option 'getAnnotations' missing (lint addon)");
  65. return options;
  66. }
  67. function clearMarks(cm) {
  68. var state = cm.state.lint;
  69. if (state.hasGutter) cm.clearGutter(GUTTER_ID);
  70. for (var i = 0; i < state.marked.length; ++i)
  71. state.marked[i].clear();
  72. state.marked.length = 0;
  73. }
  74. function makeMarker(labels, severity, multiple, tooltips) {
  75. var marker = document.createElement("div"), inner = marker;
  76. marker.className = "CodeMirror-lint-marker-" + severity;
  77. if (multiple) {
  78. inner = marker.appendChild(document.createElement("div"));
  79. inner.className = "CodeMirror-lint-marker-multiple";
  80. }
  81. if (tooltips != false) CodeMirror.on(inner, "mouseover", function(e) {
  82. showTooltipFor(e, labels, inner);
  83. });
  84. return marker;
  85. }
  86. function getMaxSeverity(a, b) {
  87. if (a == "error") return a;
  88. else return b;
  89. }
  90. function groupByLine(annotations) {
  91. var lines = [];
  92. for (var i = 0; i < annotations.length; ++i) {
  93. var ann = annotations[i], line = ann.from.line;
  94. (lines[line] || (lines[line] = [])).push(ann);
  95. }
  96. return lines;
  97. }
  98. function annotationTooltip(ann) {
  99. var severity = ann.severity;
  100. if (!severity) severity = "error";
  101. var tip = document.createElement("div");
  102. tip.className = "CodeMirror-lint-message-" + severity;
  103. tip.appendChild(document.createTextNode(ann.message));
  104. return tip;
  105. }
  106. function startLinting(cm) {
  107. var state = cm.state.lint, options = state.options;
  108. var passOptions = options.options || options; // Support deprecated passing of `options` property in options
  109. if (options.async || options.getAnnotations.async)
  110. options.getAnnotations(cm.getValue(), updateLinting, passOptions, cm);
  111. else
  112. updateLinting(cm, options.getAnnotations(cm.getValue(), passOptions, cm));
  113. }
  114. function updateLinting(cm, annotationsNotSorted) {
  115. clearMarks(cm);
  116. var state = cm.state.lint, options = state.options;
  117. var annotations = groupByLine(annotationsNotSorted);
  118. for (var line = 0; line < annotations.length; ++line) {
  119. var anns = annotations[line];
  120. if (!anns) continue;
  121. var maxSeverity = null;
  122. var tipLabel = state.hasGutter && document.createDocumentFragment();
  123. for (var i = 0; i < anns.length; ++i) {
  124. var ann = anns[i];
  125. var severity = ann.severity;
  126. if (!severity) severity = "error";
  127. maxSeverity = getMaxSeverity(maxSeverity, severity);
  128. if (options.formatAnnotation) ann = options.formatAnnotation(ann);
  129. if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann));
  130. if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, {
  131. className: "CodeMirror-lint-mark-" + severity,
  132. __annotation: ann
  133. }));
  134. }
  135. if (state.hasGutter)
  136. cm.setGutterMarker(line, GUTTER_ID, makeMarker(tipLabel, maxSeverity, anns.length > 1,
  137. state.options.tooltips));
  138. }
  139. if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
  140. }
  141. function onChange(cm) {
  142. var state = cm.state.lint;
  143. clearTimeout(state.timeout);
  144. state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500);
  145. }
  146. function popupSpanTooltip(ann, e) {
  147. var target = e.target || e.srcElement;
  148. showTooltipFor(e, annotationTooltip(ann), target);
  149. }
  150. function onMouseOver(cm, e) {
  151. var target = e.target || e.srcElement;
  152. if (!/\bCodeMirror-lint-mark-/.test(target.className)) return;
  153. var box = target.getBoundingClientRect(), x = (box.left + box.right) / 2, y = (box.top + box.bottom) / 2;
  154. var spans = cm.findMarksAt(cm.coordsChar({left: x, top: y}, "client"));
  155. for (var i = 0; i < spans.length; ++i) {
  156. var ann = spans[i].__annotation;
  157. if (ann) return popupSpanTooltip(ann, e);
  158. }
  159. }
  160. CodeMirror.defineOption("lint", false, function(cm, val, old) {
  161. if (old && old != CodeMirror.Init) {
  162. clearMarks(cm);
  163. cm.off("change", onChange);
  164. CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver);
  165. delete cm.state.lint;
  166. }
  167. if (val) {
  168. var gutters = cm.getOption("gutters"), hasLintGutter = false;
  169. for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true;
  170. var state = cm.state.lint = new LintState(cm, parseOptions(cm, val), hasLintGutter);
  171. cm.on("change", onChange);
  172. if (state.options.tooltips != false)
  173. CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
  174. startLinting(cm);
  175. }
  176. });
  177. });