LaravelTest
529 строк · 19.5 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4// declare global: DOMRect
5
6(function(mod) {7if (typeof exports == "object" && typeof module == "object") // CommonJS8mod(require("../../lib/codemirror"));9else if (typeof define == "function" && define.amd) // AMD10define(["../../lib/codemirror"], mod);11else // Plain browser env12mod(CodeMirror);13})(function(CodeMirror) {14"use strict";15
16var HINT_ELEMENT_CLASS = "CodeMirror-hint";17var ACTIVE_HINT_ELEMENT_CLASS = "CodeMirror-hint-active";18
19// This is the old interface, kept around for now to stay20// backwards-compatible.21CodeMirror.showHint = function(cm, getHints, options) {22if (!getHints) return cm.showHint(options);23if (options && options.async) getHints.async = true;24var newOpts = {hint: getHints};25if (options) for (var prop in options) newOpts[prop] = options[prop];26return cm.showHint(newOpts);27};28
29CodeMirror.defineExtension("showHint", function(options) {30options = parseOptions(this, this.getCursor("start"), options);31var selections = this.listSelections()32if (selections.length > 1) return;33// By default, don't allow completion when something is selected.34// A hint function can have a `supportsSelection` property to35// indicate that it can handle selections.36if (this.somethingSelected()) {37if (!options.hint.supportsSelection) return;38// Don't try with cross-line selections39for (var i = 0; i < selections.length; i++)40if (selections[i].head.line != selections[i].anchor.line) return;41}42
43if (this.state.completionActive) this.state.completionActive.close();44var completion = this.state.completionActive = new Completion(this, options);45if (!completion.options.hint) return;46
47CodeMirror.signal(this, "startCompletion", this);48completion.update(true);49});50
51CodeMirror.defineExtension("closeHint", function() {52if (this.state.completionActive) this.state.completionActive.close()53})54
55function Completion(cm, options) {56this.cm = cm;57this.options = options;58this.widget = null;59this.debounce = 0;60this.tick = 0;61this.startPos = this.cm.getCursor("start");62this.startLen = this.cm.getLine(this.startPos.line).length - this.cm.getSelection().length;63
64if (this.options.updateOnCursorActivity) {65var self = this;66cm.on("cursorActivity", this.activityFunc = function() { self.cursorActivity(); });67}68}69
70var requestAnimationFrame = window.requestAnimationFrame || function(fn) {71return setTimeout(fn, 1000/60);72};73var cancelAnimationFrame = window.cancelAnimationFrame || clearTimeout;74
75Completion.prototype = {76close: function() {77if (!this.active()) return;78this.cm.state.completionActive = null;79this.tick = null;80if (this.options.updateOnCursorActivity) {81this.cm.off("cursorActivity", this.activityFunc);82}83
84if (this.widget && this.data) CodeMirror.signal(this.data, "close");85if (this.widget) this.widget.close();86CodeMirror.signal(this.cm, "endCompletion", this.cm);87},88
89active: function() {90return this.cm.state.completionActive == this;91},92
93pick: function(data, i) {94var completion = data.list[i], self = this;95this.cm.operation(function() {96if (completion.hint)97completion.hint(self.cm, data, completion);98else99self.cm.replaceRange(getText(completion), completion.from || data.from,100completion.to || data.to, "complete");101CodeMirror.signal(data, "pick", completion);102self.cm.scrollIntoView();103});104if (this.options.closeOnPick) {105this.close();106}107},108
109cursorActivity: function() {110if (this.debounce) {111cancelAnimationFrame(this.debounce);112this.debounce = 0;113}114
115var identStart = this.startPos;116if(this.data) {117identStart = this.data.from;118}119
120var pos = this.cm.getCursor(), line = this.cm.getLine(pos.line);121if (pos.line != this.startPos.line || line.length - pos.ch != this.startLen - this.startPos.ch ||122pos.ch < identStart.ch || this.cm.somethingSelected() ||123(!pos.ch || this.options.closeCharacters.test(line.charAt(pos.ch - 1)))) {124this.close();125} else {126var self = this;127this.debounce = requestAnimationFrame(function() {self.update();});128if (this.widget) this.widget.disable();129}130},131
132update: function(first) {133if (this.tick == null) return134var self = this, myTick = ++this.tick135fetchHints(this.options.hint, this.cm, this.options, function(data) {136if (self.tick == myTick) self.finishUpdate(data, first)137})138},139
140finishUpdate: function(data, first) {141if (this.data) CodeMirror.signal(this.data, "update");142
143var picked = (this.widget && this.widget.picked) || (first && this.options.completeSingle);144if (this.widget) this.widget.close();145
146this.data = data;147
148if (data && data.list.length) {149if (picked && data.list.length == 1) {150this.pick(data, 0);151} else {152this.widget = new Widget(this, data);153CodeMirror.signal(data, "shown");154}155}156}157};158
159function parseOptions(cm, pos, options) {160var editor = cm.options.hintOptions;161var out = {};162for (var prop in defaultOptions) out[prop] = defaultOptions[prop];163if (editor) for (var prop in editor)164if (editor[prop] !== undefined) out[prop] = editor[prop];165if (options) for (var prop in options)166if (options[prop] !== undefined) out[prop] = options[prop];167if (out.hint.resolve) out.hint = out.hint.resolve(cm, pos)168return out;169}170
171function getText(completion) {172if (typeof completion == "string") return completion;173else return completion.text;174}175
176function buildKeyMap(completion, handle) {177var baseMap = {178Up: function() {handle.moveFocus(-1);},179Down: function() {handle.moveFocus(1);},180PageUp: function() {handle.moveFocus(-handle.menuSize() + 1, true);},181PageDown: function() {handle.moveFocus(handle.menuSize() - 1, true);},182Home: function() {handle.setFocus(0);},183End: function() {handle.setFocus(handle.length - 1);},184Enter: handle.pick,185Tab: handle.pick,186Esc: handle.close187};188
189var mac = /Mac/.test(navigator.platform);190
191if (mac) {192baseMap["Ctrl-P"] = function() {handle.moveFocus(-1);};193baseMap["Ctrl-N"] = function() {handle.moveFocus(1);};194}195
196var custom = completion.options.customKeys;197var ourMap = custom ? {} : baseMap;198function addBinding(key, val) {199var bound;200if (typeof val != "string")201bound = function(cm) { return val(cm, handle); };202// This mechanism is deprecated203else if (baseMap.hasOwnProperty(val))204bound = baseMap[val];205else206bound = val;207ourMap[key] = bound;208}209if (custom)210for (var key in custom) if (custom.hasOwnProperty(key))211addBinding(key, custom[key]);212var extra = completion.options.extraKeys;213if (extra)214for (var key in extra) if (extra.hasOwnProperty(key))215addBinding(key, extra[key]);216return ourMap;217}218
219function getHintElement(hintsElement, el) {220while (el && el != hintsElement) {221if (el.nodeName.toUpperCase() === "LI" && el.parentNode == hintsElement) return el;222el = el.parentNode;223}224}225
226function Widget(completion, data) {227this.id = "cm-complete-" + Math.floor(Math.random(1e6))228this.completion = completion;229this.data = data;230this.picked = false;231var widget = this, cm = completion.cm;232var ownerDocument = cm.getInputField().ownerDocument;233var parentWindow = ownerDocument.defaultView || ownerDocument.parentWindow;234
235var hints = this.hints = ownerDocument.createElement("ul");236hints.setAttribute("role", "listbox")237hints.setAttribute("aria-expanded", "true")238hints.id = this.id239var theme = completion.cm.options.theme;240hints.className = "CodeMirror-hints " + theme;241this.selectedHint = data.selectedHint || 0;242
243var completions = data.list;244for (var i = 0; i < completions.length; ++i) {245var elt = hints.appendChild(ownerDocument.createElement("li")), cur = completions[i];246var className = HINT_ELEMENT_CLASS + (i != this.selectedHint ? "" : " " + ACTIVE_HINT_ELEMENT_CLASS);247if (cur.className != null) className = cur.className + " " + className;248elt.className = className;249if (i == this.selectedHint) elt.setAttribute("aria-selected", "true")250elt.id = this.id + "-" + i251elt.setAttribute("role", "option")252if (cur.render) cur.render(elt, data, cur);253else elt.appendChild(ownerDocument.createTextNode(cur.displayText || getText(cur)));254elt.hintId = i;255}256
257var container = completion.options.container || ownerDocument.body;258var pos = cm.cursorCoords(completion.options.alignWithWord ? data.from : null);259var left = pos.left, top = pos.bottom, below = true;260var offsetLeft = 0, offsetTop = 0;261if (container !== ownerDocument.body) {262// We offset the cursor position because left and top are relative to the offsetParent's top left corner.263var isContainerPositioned = ['absolute', 'relative', 'fixed'].indexOf(parentWindow.getComputedStyle(container).position) !== -1;264var offsetParent = isContainerPositioned ? container : container.offsetParent;265var offsetParentPosition = offsetParent.getBoundingClientRect();266var bodyPosition = ownerDocument.body.getBoundingClientRect();267offsetLeft = (offsetParentPosition.left - bodyPosition.left - offsetParent.scrollLeft);268offsetTop = (offsetParentPosition.top - bodyPosition.top - offsetParent.scrollTop);269}270hints.style.left = (left - offsetLeft) + "px";271hints.style.top = (top - offsetTop) + "px";272
273// If we're at the edge of the screen, then we want the menu to appear on the left of the cursor.274var winW = parentWindow.innerWidth || Math.max(ownerDocument.body.offsetWidth, ownerDocument.documentElement.offsetWidth);275var winH = parentWindow.innerHeight || Math.max(ownerDocument.body.offsetHeight, ownerDocument.documentElement.offsetHeight);276container.appendChild(hints);277cm.getInputField().setAttribute("aria-autocomplete", "list")278cm.getInputField().setAttribute("aria-owns", this.id)279cm.getInputField().setAttribute("aria-activedescendant", this.id + "-" + this.selectedHint)280
281var box = completion.options.moveOnOverlap ? hints.getBoundingClientRect() : new DOMRect();282var scrolls = completion.options.paddingForScrollbar ? hints.scrollHeight > hints.clientHeight + 1 : false;283
284// Compute in the timeout to avoid reflow on init285var startScroll;286setTimeout(function() { startScroll = cm.getScrollInfo(); });287
288var overlapY = box.bottom - winH;289if (overlapY > 0) {290var height = box.bottom - box.top, curTop = pos.top - (pos.bottom - box.top);291if (curTop - height > 0) { // Fits above cursor292hints.style.top = (top = pos.top - height - offsetTop) + "px";293below = false;294} else if (height > winH) {295hints.style.height = (winH - 5) + "px";296hints.style.top = (top = pos.bottom - box.top - offsetTop) + "px";297var cursor = cm.getCursor();298if (data.from.ch != cursor.ch) {299pos = cm.cursorCoords(cursor);300hints.style.left = (left = pos.left - offsetLeft) + "px";301box = hints.getBoundingClientRect();302}303}304}305var overlapX = box.right - winW;306if (scrolls) overlapX += cm.display.nativeBarWidth;307if (overlapX > 0) {308if (box.right - box.left > winW) {309hints.style.width = (winW - 5) + "px";310overlapX -= (box.right - box.left) - winW;311}312hints.style.left = (left = pos.left - overlapX - offsetLeft) + "px";313}314if (scrolls) for (var node = hints.firstChild; node; node = node.nextSibling)315node.style.paddingRight = cm.display.nativeBarWidth + "px"316
317cm.addKeyMap(this.keyMap = buildKeyMap(completion, {318moveFocus: function(n, avoidWrap) { widget.changeActive(widget.selectedHint + n, avoidWrap); },319setFocus: function(n) { widget.changeActive(n); },320menuSize: function() { return widget.screenAmount(); },321length: completions.length,322close: function() { completion.close(); },323pick: function() { widget.pick(); },324data: data325}));326
327if (completion.options.closeOnUnfocus) {328var closingOnBlur;329cm.on("blur", this.onBlur = function() { closingOnBlur = setTimeout(function() { completion.close(); }, 100); });330cm.on("focus", this.onFocus = function() { clearTimeout(closingOnBlur); });331}332
333cm.on("scroll", this.onScroll = function() {334var curScroll = cm.getScrollInfo(), editor = cm.getWrapperElement().getBoundingClientRect();335if (!startScroll) startScroll = cm.getScrollInfo();336var newTop = top + startScroll.top - curScroll.top;337var point = newTop - (parentWindow.pageYOffset || (ownerDocument.documentElement || ownerDocument.body).scrollTop);338if (!below) point += hints.offsetHeight;339if (point <= editor.top || point >= editor.bottom) return completion.close();340hints.style.top = newTop + "px";341hints.style.left = (left + startScroll.left - curScroll.left) + "px";342});343
344CodeMirror.on(hints, "dblclick", function(e) {345var t = getHintElement(hints, e.target || e.srcElement);346if (t && t.hintId != null) {widget.changeActive(t.hintId); widget.pick();}347});348
349CodeMirror.on(hints, "click", function(e) {350var t = getHintElement(hints, e.target || e.srcElement);351if (t && t.hintId != null) {352widget.changeActive(t.hintId);353if (completion.options.completeOnSingleClick) widget.pick();354}355});356
357CodeMirror.on(hints, "mousedown", function() {358setTimeout(function(){cm.focus();}, 20);359});360
361// The first hint doesn't need to be scrolled to on init362var selectedHintRange = this.getSelectedHintRange();363if (selectedHintRange.from !== 0 || selectedHintRange.to !== 0) {364this.scrollToActive();365}366
367CodeMirror.signal(data, "select", completions[this.selectedHint], hints.childNodes[this.selectedHint]);368return true;369}370
371Widget.prototype = {372close: function() {373if (this.completion.widget != this) return;374this.completion.widget = null;375if (this.hints.parentNode) this.hints.parentNode.removeChild(this.hints);376this.completion.cm.removeKeyMap(this.keyMap);377var input = this.completion.cm.getInputField()378input.removeAttribute("aria-activedescendant")379input.removeAttribute("aria-owns")380
381var cm = this.completion.cm;382if (this.completion.options.closeOnUnfocus) {383cm.off("blur", this.onBlur);384cm.off("focus", this.onFocus);385}386cm.off("scroll", this.onScroll);387},388
389disable: function() {390this.completion.cm.removeKeyMap(this.keyMap);391var widget = this;392this.keyMap = {Enter: function() { widget.picked = true; }};393this.completion.cm.addKeyMap(this.keyMap);394},395
396pick: function() {397this.completion.pick(this.data, this.selectedHint);398},399
400changeActive: function(i, avoidWrap) {401if (i >= this.data.list.length)402i = avoidWrap ? this.data.list.length - 1 : 0;403else if (i < 0)404i = avoidWrap ? 0 : this.data.list.length - 1;405if (this.selectedHint == i) return;406var node = this.hints.childNodes[this.selectedHint];407if (node) {408node.className = node.className.replace(" " + ACTIVE_HINT_ELEMENT_CLASS, "");409node.removeAttribute("aria-selected")410}411node = this.hints.childNodes[this.selectedHint = i];412node.className += " " + ACTIVE_HINT_ELEMENT_CLASS;413node.setAttribute("aria-selected", "true")414this.completion.cm.getInputField().setAttribute("aria-activedescendant", node.id)415this.scrollToActive()416CodeMirror.signal(this.data, "select", this.data.list[this.selectedHint], node);417},418
419scrollToActive: function() {420var selectedHintRange = this.getSelectedHintRange();421var node1 = this.hints.childNodes[selectedHintRange.from];422var node2 = this.hints.childNodes[selectedHintRange.to];423var firstNode = this.hints.firstChild;424if (node1.offsetTop < this.hints.scrollTop)425this.hints.scrollTop = node1.offsetTop - firstNode.offsetTop;426else if (node2.offsetTop + node2.offsetHeight > this.hints.scrollTop + this.hints.clientHeight)427this.hints.scrollTop = node2.offsetTop + node2.offsetHeight - this.hints.clientHeight + firstNode.offsetTop;428},429
430screenAmount: function() {431return Math.floor(this.hints.clientHeight / this.hints.firstChild.offsetHeight) || 1;432},433
434getSelectedHintRange: function() {435var margin = this.completion.options.scrollMargin || 0;436return {437from: Math.max(0, this.selectedHint - margin),438to: Math.min(this.data.list.length - 1, this.selectedHint + margin),439};440}441};442
443function applicableHelpers(cm, helpers) {444if (!cm.somethingSelected()) return helpers445var result = []446for (var i = 0; i < helpers.length; i++)447if (helpers[i].supportsSelection) result.push(helpers[i])448return result449}450
451function fetchHints(hint, cm, options, callback) {452if (hint.async) {453hint(cm, callback, options)454} else {455var result = hint(cm, options)456if (result && result.then) result.then(callback)457else callback(result)458}459}460
461function resolveAutoHints(cm, pos) {462var helpers = cm.getHelpers(pos, "hint"), words463if (helpers.length) {464var resolved = function(cm, callback, options) {465var app = applicableHelpers(cm, helpers);466function run(i) {467if (i == app.length) return callback(null)468fetchHints(app[i], cm, options, function(result) {469if (result && result.list.length > 0) callback(result)470else run(i + 1)471})472}473run(0)474}475resolved.async = true476resolved.supportsSelection = true477return resolved478} else if (words = cm.getHelper(cm.getCursor(), "hintWords")) {479return function(cm) { return CodeMirror.hint.fromList(cm, {words: words}) }480} else if (CodeMirror.hint.anyword) {481return function(cm, options) { return CodeMirror.hint.anyword(cm, options) }482} else {483return function() {}484}485}486
487CodeMirror.registerHelper("hint", "auto", {488resolve: resolveAutoHints489});490
491CodeMirror.registerHelper("hint", "fromList", function(cm, options) {492var cur = cm.getCursor(), token = cm.getTokenAt(cur)493var term, from = CodeMirror.Pos(cur.line, token.start), to = cur494if (token.start < cur.ch && /\w/.test(token.string.charAt(cur.ch - token.start - 1))) {495term = token.string.substr(0, cur.ch - token.start)496} else {497term = ""498from = cur499}500var found = [];501for (var i = 0; i < options.words.length; i++) {502var word = options.words[i];503if (word.slice(0, term.length) == term)504found.push(word);505}506
507if (found.length) return {list: found, from: from, to: to};508});509
510CodeMirror.commands.autocomplete = CodeMirror.showHint;511
512var defaultOptions = {513hint: CodeMirror.hint.auto,514completeSingle: true,515alignWithWord: true,516closeCharacters: /[\s()\[\]{};:>,]/,517closeOnPick: true,518closeOnUnfocus: true,519updateOnCursorActivity: true,520completeOnSingleClick: true,521container: null,522customKeys: null,523extraKeys: null,524paddingForScrollbar: true,525moveOnOverlap: true,526};527
528CodeMirror.defineOption("hintOptions", null);529});530