You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
9.6 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/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. var LINT_LINE_ID = "CodeMirror-lint-line-";
  14. function showTooltip(cm, e, content) {
  15. var tt = document.createElement("div");
  16. tt.className = "CodeMirror-lint-tooltip cm-s-" + cm.options.theme;
  17. tt.appendChild(content.cloneNode(true));
  18. if (cm.state.lint.options.selfContain)
  19. cm.getWrapperElement().appendChild(tt);
  20. else
  21. document.body.appendChild(tt);
  22. function position(e) {
  23. if (!tt.parentNode) return CodeMirror.off(document, "mousemove", position);
  24. var top = Math.max(0, e.clientY - tt.offsetHeight - 5);
  25. var left = Math.max(0, Math.min(e.clientX + 5, tt.ownerDocument.defaultView.innerWidth - tt.offsetWidth));
  26. tt.style.top = top + "px"
  27. tt.style.left = left + "px";
  28. }
  29. CodeMirror.on(document, "mousemove", position);
  30. position(e);
  31. if (tt.style.opacity != null) tt.style.opacity = 1;
  32. return tt;
  33. }
  34. function rm(elt) {
  35. if (elt.parentNode) elt.parentNode.removeChild(elt);
  36. }
  37. function hideTooltip(tt) {
  38. if (!tt.parentNode) return;
  39. if (tt.style.opacity == null) rm(tt);
  40. tt.style.opacity = 0;
  41. setTimeout(function() { rm(tt); }, 600);
  42. }
  43. function showTooltipFor(cm, e, content, node) {
  44. var tooltip = showTooltip(cm, e, content);
  45. function hide() {
  46. CodeMirror.off(node, "mouseout", hide);
  47. if (tooltip) { hideTooltip(tooltip); tooltip = null; }
  48. }
  49. var poll = setInterval(function() {
  50. if (tooltip) for (var n = node;; n = n.parentNode) {
  51. if (n && n.nodeType == 11) n = n.host;
  52. if (n == document.body) return;
  53. if (!n) { hide(); break; }
  54. }
  55. if (!tooltip) return clearInterval(poll);
  56. }, 400);
  57. CodeMirror.on(node, "mouseout", hide);
  58. }
  59. function LintState(cm, conf, hasGutter) {
  60. this.marked = [];
  61. if (conf instanceof Function) conf = {getAnnotations: conf};
  62. if (!conf || conf === true) conf = {};
  63. this.options = {};
  64. this.linterOptions = conf.options || {};
  65. for (var prop in defaults) this.options[prop] = defaults[prop];
  66. for (var prop in conf) {
  67. if (defaults.hasOwnProperty(prop)) {
  68. if (conf[prop] != null) this.options[prop] = conf[prop];
  69. } else if (!conf.options) {
  70. this.linterOptions[prop] = conf[prop];
  71. }
  72. }
  73. this.timeout = null;
  74. this.hasGutter = hasGutter;
  75. this.onMouseOver = function(e) { onMouseOver(cm, e); };
  76. this.waitingFor = 0
  77. }
  78. var defaults = {
  79. highlightLines: false,
  80. tooltips: true,
  81. delay: 500,
  82. lintOnChange: true,
  83. getAnnotations: null,
  84. async: false,
  85. selfContain: null,
  86. formatAnnotation: null,
  87. onUpdateLinting: null
  88. }
  89. function clearMarks(cm) {
  90. var state = cm.state.lint;
  91. if (state.hasGutter) cm.clearGutter(GUTTER_ID);
  92. if (state.options.highlightLines) clearErrorLines(cm);
  93. for (var i = 0; i < state.marked.length; ++i)
  94. state.marked[i].clear();
  95. state.marked.length = 0;
  96. }
  97. function clearErrorLines(cm) {
  98. cm.eachLine(function(line) {
  99. var has = line.wrapClass && /\bCodeMirror-lint-line-\w+\b/.exec(line.wrapClass);
  100. if (has) cm.removeLineClass(line, "wrap", has[0]);
  101. })
  102. }
  103. function makeMarker(cm, labels, severity, multiple, tooltips) {
  104. var marker = document.createElement("div"), inner = marker;
  105. marker.className = "CodeMirror-lint-marker CodeMirror-lint-marker-" + severity;
  106. if (multiple) {
  107. inner = marker.appendChild(document.createElement("div"));
  108. inner.className = "CodeMirror-lint-marker CodeMirror-lint-marker-multiple";
  109. }
  110. if (tooltips != false) CodeMirror.on(inner, "mouseover", function(e) {
  111. showTooltipFor(cm, e, labels, inner);
  112. });
  113. return marker;
  114. }
  115. function getMaxSeverity(a, b) {
  116. if (a == "error") return a;
  117. else return b;
  118. }
  119. function groupByLine(annotations) {
  120. var lines = [];
  121. for (var i = 0; i < annotations.length; ++i) {
  122. var ann = annotations[i], line = ann.from.line;
  123. (lines[line] || (lines[line] = [])).push(ann);
  124. }
  125. return lines;
  126. }
  127. function annotationTooltip(ann) {
  128. var severity = ann.severity;
  129. if (!severity) severity = "error";
  130. var tip = document.createElement("div");
  131. tip.className = "CodeMirror-lint-message CodeMirror-lint-message-" + severity;
  132. if (typeof ann.messageHTML != 'undefined') {
  133. tip.innerHTML = ann.messageHTML;
  134. } else {
  135. tip.appendChild(document.createTextNode(ann.message));
  136. }
  137. return tip;
  138. }
  139. function lintAsync(cm, getAnnotations) {
  140. var state = cm.state.lint
  141. var id = ++state.waitingFor
  142. function abort() {
  143. id = -1
  144. cm.off("change", abort)
  145. }
  146. cm.on("change", abort)
  147. getAnnotations(cm.getValue(), function(annotations, arg2) {
  148. cm.off("change", abort)
  149. if (state.waitingFor != id) return
  150. if (arg2 && annotations instanceof CodeMirror) annotations = arg2
  151. cm.operation(function() {updateLinting(cm, annotations)})
  152. }, state.linterOptions, cm);
  153. }
  154. function startLinting(cm) {
  155. var state = cm.state.lint;
  156. if (!state) return;
  157. var options = state.options;
  158. /*
  159. * Passing rules in `options` property prevents JSHint (and other linters) from complaining
  160. * about unrecognized rules like `onUpdateLinting`, `delay`, `lintOnChange`, etc.
  161. */
  162. var getAnnotations = options.getAnnotations || cm.getHelper(CodeMirror.Pos(0, 0), "lint");
  163. if (!getAnnotations) return;
  164. if (options.async || getAnnotations.async) {
  165. lintAsync(cm, getAnnotations)
  166. } else {
  167. var annotations = getAnnotations(cm.getValue(), state.linterOptions, cm);
  168. if (!annotations) return;
  169. if (annotations.then) annotations.then(function(issues) {
  170. cm.operation(function() {updateLinting(cm, issues)})
  171. });
  172. else cm.operation(function() {updateLinting(cm, annotations)})
  173. }
  174. }
  175. function updateLinting(cm, annotationsNotSorted) {
  176. var state = cm.state.lint;
  177. if (!state) return;
  178. var options = state.options;
  179. clearMarks(cm);
  180. var annotations = groupByLine(annotationsNotSorted);
  181. for (var line = 0; line < annotations.length; ++line) {
  182. var anns = annotations[line];
  183. if (!anns) continue;
  184. var maxSeverity = null;
  185. var tipLabel = state.hasGutter && document.createDocumentFragment();
  186. for (var i = 0; i < anns.length; ++i) {
  187. var ann = anns[i];
  188. var severity = ann.severity;
  189. if (!severity) severity = "error";
  190. maxSeverity = getMaxSeverity(maxSeverity, severity);
  191. if (options.formatAnnotation) ann = options.formatAnnotation(ann);
  192. if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann));
  193. if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, {
  194. className: "CodeMirror-lint-mark CodeMirror-lint-mark-" + severity,
  195. __annotation: ann
  196. }));
  197. }
  198. if (state.hasGutter)
  199. cm.setGutterMarker(line, GUTTER_ID, makeMarker(cm, tipLabel, maxSeverity, anns.length > 1,
  200. options.tooltips));
  201. if (options.highlightLines)
  202. cm.addLineClass(line, "wrap", LINT_LINE_ID + maxSeverity);
  203. }
  204. if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
  205. }
  206. function onChange(cm) {
  207. var state = cm.state.lint;
  208. if (!state) return;
  209. clearTimeout(state.timeout);
  210. state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay);
  211. }
  212. function popupTooltips(cm, annotations, e) {
  213. var target = e.target || e.srcElement;
  214. var tooltip = document.createDocumentFragment();
  215. for (var i = 0; i < annotations.length; i++) {
  216. var ann = annotations[i];
  217. tooltip.appendChild(annotationTooltip(ann));
  218. }
  219. showTooltipFor(cm, e, tooltip, target);
  220. }
  221. function onMouseOver(cm, e) {
  222. var target = e.target || e.srcElement;
  223. if (!/\bCodeMirror-lint-mark-/.test(target.className)) return;
  224. var box = target.getBoundingClientRect(), x = (box.left + box.right) / 2, y = (box.top + box.bottom) / 2;
  225. var spans = cm.findMarksAt(cm.coordsChar({left: x, top: y}, "client"));
  226. var annotations = [];
  227. for (var i = 0; i < spans.length; ++i) {
  228. var ann = spans[i].__annotation;
  229. if (ann) annotations.push(ann);
  230. }
  231. if (annotations.length) popupTooltips(cm, annotations, e);
  232. }
  233. CodeMirror.defineOption("lint", false, function(cm, val, old) {
  234. if (old && old != CodeMirror.Init) {
  235. clearMarks(cm);
  236. if (cm.state.lint.options.lintOnChange !== false)
  237. cm.off("change", onChange);
  238. CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver);
  239. clearTimeout(cm.state.lint.timeout);
  240. delete cm.state.lint;
  241. }
  242. if (val) {
  243. var gutters = cm.getOption("gutters"), hasLintGutter = false;
  244. for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true;
  245. var state = cm.state.lint = new LintState(cm, val, hasLintGutter);
  246. if (state.options.lintOnChange)
  247. cm.on("change", onChange);
  248. if (state.options.tooltips != false && state.options.tooltips != "gutter")
  249. CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
  250. startLinting(cm);
  251. }
  252. });
  253. CodeMirror.defineExtension("performLint", function() {
  254. startLinting(this);
  255. });
  256. });