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