Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

lint.js 9.5KB

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