comparison .cms/lib/codemirror/addon/hint/javascript-hint.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: https://codemirror.net/5/LICENSE
3
4 (function(mod) {
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"));
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror"], mod);
9 else // Plain browser env
10 mod(CodeMirror);
11 })(function(CodeMirror) {
12 var Pos = CodeMirror.Pos;
13
14 function forEach(arr, f) {
15 for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
16 }
17
18 function arrayContains(arr, item) {
19 if (!Array.prototype.indexOf) {
20 var i = arr.length;
21 while (i--) {
22 if (arr[i] === item) {
23 return true;
24 }
25 }
26 return false;
27 }
28 return arr.indexOf(item) != -1;
29 }
30
31 function scriptHint(editor, keywords, getToken, options) {
32 // Find the token at the cursor
33 var cur = editor.getCursor(), token = getToken(editor, cur);
34 if (/\b(?:string|comment)\b/.test(token.type)) return;
35 var innerMode = CodeMirror.innerMode(editor.getMode(), token.state);
36 if (innerMode.mode.helperType === "json") return;
37 token.state = innerMode.state;
38
39 // If it's not a 'word-style' token, ignore the token.
40 if (!/^[\w$_]*$/.test(token.string)) {
41 token = {start: cur.ch, end: cur.ch, string: "", state: token.state,
42 type: token.string == "." ? "property" : null};
43 } else if (token.end > cur.ch) {
44 token.end = cur.ch;
45 token.string = token.string.slice(0, cur.ch - token.start);
46 }
47
48 var tprop = token;
49 // If it is a property, find out what it is a property of.
50 while (tprop.type == "property") {
51 tprop = getToken(editor, Pos(cur.line, tprop.start));
52 if (tprop.string != ".") return;
53 tprop = getToken(editor, Pos(cur.line, tprop.start));
54 if (!context) var context = [];
55 context.push(tprop);
56 }
57 return {list: getCompletions(token, context, keywords, options),
58 from: Pos(cur.line, token.start),
59 to: Pos(cur.line, token.end)};
60 }
61
62 function javascriptHint(editor, options) {
63 return scriptHint(editor, javascriptKeywords,
64 function (e, cur) {return e.getTokenAt(cur);},
65 options);
66 };
67 CodeMirror.registerHelper("hint", "javascript", javascriptHint);
68
69 function getCoffeeScriptToken(editor, cur) {
70 // This getToken, it is for coffeescript, imitates the behavior of
71 // getTokenAt method in javascript.js, that is, returning "property"
72 // type and treat "." as independent token.
73 var token = editor.getTokenAt(cur);
74 if (cur.ch == token.start + 1 && token.string.charAt(0) == '.') {
75 token.end = token.start;
76 token.string = '.';
77 token.type = "property";
78 }
79 else if (/^\.[\w$_]*$/.test(token.string)) {
80 token.type = "property";
81 token.start++;
82 token.string = token.string.replace(/\./, '');
83 }
84 return token;
85 }
86
87 function coffeescriptHint(editor, options) {
88 return scriptHint(editor, coffeescriptKeywords, getCoffeeScriptToken, options);
89 }
90 CodeMirror.registerHelper("hint", "coffeescript", coffeescriptHint);
91
92 var stringProps = ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " +
93 "toUpperCase toLowerCase split concat match replace search").split(" ");
94 var arrayProps = ("length concat join splice push pop shift unshift slice reverse sort indexOf " +
95 "lastIndexOf every some filter forEach map reduce reduceRight ").split(" ");
96 var funcProps = "prototype apply call bind".split(" ");
97 var 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(" ");
99 var 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
102 function forAllProps(obj, callback) {
103 if (!Object.getOwnPropertyNames || !Object.getPrototypeOf) {
104 for (var name in obj) callback(name)
105 } else {
106 for (var o = obj; o; o = Object.getPrototypeOf(o))
107 Object.getOwnPropertyNames(o).forEach(callback)
108 }
109 }
110
111 function getCompletions(token, context, keywords, options) {
112 var found = [], start = token.string, global = options && options.globalScope || window;
113 function maybeAdd(str) {
114 if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str);
115 }
116 function gatherCompletions(obj) {
117 if (typeof obj == "string") forEach(stringProps, maybeAdd);
118 else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
119 else if (obj instanceof Function) forEach(funcProps, maybeAdd);
120 forAllProps(obj, maybeAdd)
121 }
122
123 if (context && context.length) {
124 // If this is a property, see if it belongs to some object we can
125 // find in the current environment.
126 var obj = context.pop(), base;
127 if (obj.type && obj.type.indexOf("variable") === 0) {
128 if (options && options.additionalContext)
129 base = options.additionalContext[obj.string];
130 if (!options || options.useGlobalScope !== false)
131 base = base || global[obj.string];
132 } else if (obj.type == "string") {
133 base = "";
134 } else if (obj.type == "atom") {
135 base = 1;
136 } else if (obj.type == "function") {
137 if (global.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') &&
138 (typeof global.jQuery == 'function'))
139 base = global.jQuery();
140 else if (global._ != null && (obj.string == '_') && (typeof global._ == 'function'))
141 base = global._();
142 }
143 while (base != null && context.length)
144 base = base[context.pop().string];
145 if (base != null) gatherCompletions(base);
146 } else {
147 // If not, just look in the global object, any local scope, and optional additional-context
148 // (reading into JS mode internals to get at the local and global variables)
149 for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name);
150 for (var c = token.state.context; c; c = c.prev)
151 for (var v = c.vars; v; v = v.next) maybeAdd(v.name)
152 for (var v = token.state.globalVars; v; v = v.next) maybeAdd(v.name);
153 if (options && options.additionalContext != null)
154 for (var key in options.additionalContext)
155 maybeAdd(key);
156 if (!options || options.useGlobalScope !== false)
157 gatherCompletions(global);
158 forEach(keywords, maybeAdd);
159 }
160 return found;
161 }
162 });