LaravelTest
132 строки · 5.6 Кб
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";13
14var Pos = CodeMirror.Pos;15
16function matches(hint, typed, matchInMiddle) {17if (matchInMiddle) return hint.indexOf(typed) >= 0;18else return hint.lastIndexOf(typed, 0) == 0;19}20
21function getHints(cm, options) {22var tags = options && options.schemaInfo;23var quote = (options && options.quoteChar) || '"';24var matchInMiddle = options && options.matchInMiddle;25if (!tags) return;26var cur = cm.getCursor(), token = cm.getTokenAt(cur);27if (token.end > cur.ch) {28token.end = cur.ch;29token.string = token.string.slice(0, cur.ch - token.start);30}31var inner = CodeMirror.innerMode(cm.getMode(), token.state);32if (!inner.mode.xmlCurrentTag) return33var result = [], replaceToken = false, prefix;34var tag = /\btag\b/.test(token.type) && !/>$/.test(token.string);35var tagName = tag && /^\w/.test(token.string), tagStart;36
37if (tagName) {38var before = cm.getLine(cur.line).slice(Math.max(0, token.start - 2), token.start);39var tagType = /<\/$/.test(before) ? "close" : /<$/.test(before) ? "open" : null;40if (tagType) tagStart = token.start - (tagType == "close" ? 2 : 1);41} else if (tag && token.string == "<") {42tagType = "open";43} else if (tag && token.string == "</") {44tagType = "close";45}46
47var tagInfo = inner.mode.xmlCurrentTag(inner.state)48if (!tag && !tagInfo || tagType) {49if (tagName)50prefix = token.string;51replaceToken = tagType;52var context = inner.mode.xmlCurrentContext ? inner.mode.xmlCurrentContext(inner.state) : []53var inner = context.length && context[context.length - 1]54var curTag = inner && tags[inner]55var childList = inner ? curTag && curTag.children : tags["!top"];56if (childList && tagType != "close") {57for (var i = 0; i < childList.length; ++i) if (!prefix || matches(childList[i], prefix, matchInMiddle))58result.push("<" + childList[i]);59} else if (tagType != "close") {60for (var name in tags)61if (tags.hasOwnProperty(name) && name != "!top" && name != "!attrs" && (!prefix || matches(name, prefix, matchInMiddle)))62result.push("<" + name);63}64if (inner && (!prefix || tagType == "close" && matches(inner, prefix, matchInMiddle)))65result.push("</" + inner + ">");66} else {67// Attribute completion68var curTag = tagInfo && tags[tagInfo.name], attrs = curTag && curTag.attrs;69var globalAttrs = tags["!attrs"];70if (!attrs && !globalAttrs) return;71if (!attrs) {72attrs = globalAttrs;73} else if (globalAttrs) { // Combine tag-local and global attributes74var set = {};75for (var nm in globalAttrs) if (globalAttrs.hasOwnProperty(nm)) set[nm] = globalAttrs[nm];76for (var nm in attrs) if (attrs.hasOwnProperty(nm)) set[nm] = attrs[nm];77attrs = set;78}79if (token.type == "string" || token.string == "=") { // A value80var before = cm.getRange(Pos(cur.line, Math.max(0, cur.ch - 60)),81Pos(cur.line, token.type == "string" ? token.start : token.end));82var atName = before.match(/([^\s\u00a0=<>\"\']+)=$/), atValues;83if (!atName || !attrs.hasOwnProperty(atName[1]) || !(atValues = attrs[atName[1]])) return;84if (typeof atValues == 'function') atValues = atValues.call(this, cm); // Functions can be used to supply values for autocomplete widget85if (token.type == "string") {86prefix = token.string;87var n = 0;88if (/['"]/.test(token.string.charAt(0))) {89quote = token.string.charAt(0);90prefix = token.string.slice(1);91n++;92}93var len = token.string.length;94if (/['"]/.test(token.string.charAt(len - 1))) {95quote = token.string.charAt(len - 1);96prefix = token.string.substr(n, len - 2);97}98if (n) { // an opening quote99var line = cm.getLine(cur.line);100if (line.length > token.end && line.charAt(token.end) == quote) token.end++; // include a closing quote101}102replaceToken = true;103}104var returnHintsFromAtValues = function(atValues) {105if (atValues)106for (var i = 0; i < atValues.length; ++i) if (!prefix || matches(atValues[i], prefix, matchInMiddle))107result.push(quote + atValues[i] + quote);108return returnHints();109};110if (atValues && atValues.then) return atValues.then(returnHintsFromAtValues);111return returnHintsFromAtValues(atValues);112} else { // An attribute name113if (token.type == "attribute") {114prefix = token.string;115replaceToken = true;116}117for (var attr in attrs) if (attrs.hasOwnProperty(attr) && (!prefix || matches(attr, prefix, matchInMiddle)))118result.push(attr);119}120}121function returnHints() {122return {123list: result,124from: replaceToken ? Pos(cur.line, tagStart == null ? token.start : tagStart) : cur,125to: replaceToken ? Pos(cur.line, token.end) : cur126};127}128return returnHints();129}130
131CodeMirror.registerHelper("hint", "xml", getHints);132});133