LaravelTest
162 строки · 6.7 Кб
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) {12var Pos = CodeMirror.Pos;13
14function forEach(arr, f) {15for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);16}17
18function arrayContains(arr, item) {19if (!Array.prototype.indexOf) {20var i = arr.length;21while (i--) {22if (arr[i] === item) {23return true;24}25}26return false;27}28return arr.indexOf(item) != -1;29}30
31function scriptHint(editor, keywords, getToken, options) {32// Find the token at the cursor33var cur = editor.getCursor(), token = getToken(editor, cur);34if (/\b(?:string|comment)\b/.test(token.type)) return;35var innerMode = CodeMirror.innerMode(editor.getMode(), token.state);36if (innerMode.mode.helperType === "json") return;37token.state = innerMode.state;38
39// If it's not a 'word-style' token, ignore the token.40if (!/^[\w$_]*$/.test(token.string)) {41token = {start: cur.ch, end: cur.ch, string: "", state: token.state,42type: token.string == "." ? "property" : null};43} else if (token.end > cur.ch) {44token.end = cur.ch;45token.string = token.string.slice(0, cur.ch - token.start);46}47
48var tprop = token;49// If it is a property, find out what it is a property of.50while (tprop.type == "property") {51tprop = getToken(editor, Pos(cur.line, tprop.start));52if (tprop.string != ".") return;53tprop = getToken(editor, Pos(cur.line, tprop.start));54if (!context) var context = [];55context.push(tprop);56}57return {list: getCompletions(token, context, keywords, options),58from: Pos(cur.line, token.start),59to: Pos(cur.line, token.end)};60}61
62function javascriptHint(editor, options) {63return scriptHint(editor, javascriptKeywords,64function (e, cur) {return e.getTokenAt(cur);},65options);66};67CodeMirror.registerHelper("hint", "javascript", javascriptHint);68
69function getCoffeeScriptToken(editor, cur) {70// This getToken, it is for coffeescript, imitates the behavior of71// getTokenAt method in javascript.js, that is, returning "property"72// type and treat "." as independent token.73var token = editor.getTokenAt(cur);74if (cur.ch == token.start + 1 && token.string.charAt(0) == '.') {75token.end = token.start;76token.string = '.';77token.type = "property";78}79else if (/^\.[\w$_]*$/.test(token.string)) {80token.type = "property";81token.start++;82token.string = token.string.replace(/\./, '');83}84return token;85}86
87function coffeescriptHint(editor, options) {88return scriptHint(editor, coffeescriptKeywords, getCoffeeScriptToken, options);89}90CodeMirror.registerHelper("hint", "coffeescript", coffeescriptHint);91
92var stringProps = ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " +93"toUpperCase toLowerCase split concat match replace search").split(" ");94var arrayProps = ("length concat join splice push pop shift unshift slice reverse sort indexOf " +95"lastIndexOf every some filter forEach map reduce reduceRight ").split(" ");96var funcProps = "prototype apply call bind".split(" ");97var javascriptKeywords = ("break case catch class const continue debugger default delete do else export extends false finally for function " +98"if in import instanceof new null return super switch this throw true try typeof var void while with yield").split(" ");99var coffeescriptKeywords = ("and break catch class continue delete do else extends false finally for " +100"if in instanceof isnt new no not null of off on or return switch then throw true try typeof until void while with yes").split(" ");101
102function forAllProps(obj, callback) {103if (!Object.getOwnPropertyNames || !Object.getPrototypeOf) {104for (var name in obj) callback(name)105} else {106for (var o = obj; o; o = Object.getPrototypeOf(o))107Object.getOwnPropertyNames(o).forEach(callback)108}109}110
111function getCompletions(token, context, keywords, options) {112var found = [], start = token.string, global = options && options.globalScope || window;113function maybeAdd(str) {114if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str);115}116function gatherCompletions(obj) {117if (typeof obj == "string") forEach(stringProps, maybeAdd);118else if (obj instanceof Array) forEach(arrayProps, maybeAdd);119else if (obj instanceof Function) forEach(funcProps, maybeAdd);120forAllProps(obj, maybeAdd)121}122
123if (context && context.length) {124// If this is a property, see if it belongs to some object we can125// find in the current environment.126var obj = context.pop(), base;127if (obj.type && obj.type.indexOf("variable") === 0) {128if (options && options.additionalContext)129base = options.additionalContext[obj.string];130if (!options || options.useGlobalScope !== false)131base = base || global[obj.string];132} else if (obj.type == "string") {133base = "";134} else if (obj.type == "atom") {135base = 1;136} else if (obj.type == "function") {137if (global.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') &&138(typeof global.jQuery == 'function'))139base = global.jQuery();140else if (global._ != null && (obj.string == '_') && (typeof global._ == 'function'))141base = global._();142}143while (base != null && context.length)144base = base[context.pop().string];145if (base != null) gatherCompletions(base);146} else {147// If not, just look in the global object, any local scope, and optional additional-context148// (reading into JS mode internals to get at the local and global variables)149for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name);150for (var c = token.state.context; c; c = c.prev)151for (var v = c.vars; v; v = v.next) maybeAdd(v.name)152for (var v = token.state.globalVars; v; v = v.next) maybeAdd(v.name);153if (options && options.additionalContext != null)154for (var key in options.additionalContext)155maybeAdd(key);156if (!options || options.useGlobalScope !== false)157gatherCompletions(global);158forEach(keywords, maybeAdd);159}160return found;161}162});163