123 lines
3.0 KiB
JavaScript
123 lines
3.0 KiB
JavaScript
/*REMEMBER 2 add KARLKODER + LIB :-)*/
|
|
|
|
const KEYWORDS = [
|
|
"Array",
|
|
"ArrayBuffer",
|
|
"BigInt",
|
|
"Blob",
|
|
"CaretPosition",
|
|
"Date",
|
|
"Error",
|
|
"FormData",
|
|
"Headers",
|
|
"Infinity",
|
|
"Int8Array",
|
|
"JSON",
|
|
"Map",
|
|
"Math",
|
|
"NaN",
|
|
"Number",
|
|
"Object",
|
|
"Promise",
|
|
"ReadableStream",
|
|
"RegExp",
|
|
"Request",
|
|
"Response",
|
|
"Set",
|
|
"String",
|
|
"Symbol",
|
|
"TextDecoder",
|
|
"TextEncoder",
|
|
"URL",
|
|
"Uint8Array",
|
|
"WebSocket",
|
|
"Worker",
|
|
"WritableStream",
|
|
"alert",
|
|
"atob",
|
|
"blur",
|
|
"btoa",
|
|
"crypto",
|
|
"decodeURI",
|
|
"decodeURIComponent",
|
|
"encodeURI",
|
|
"encodeURIComponent",
|
|
"fetch",
|
|
"isFinite",
|
|
"isNaN",
|
|
"length",
|
|
"localStorage",
|
|
"navigator",
|
|
"parseFloat",
|
|
"parseInt",
|
|
"sessionStorage",
|
|
"undefined",
|
|
"window",
|
|
];
|
|
|
|
export class TextCompleter {
|
|
getCompletions(_editor, session, pos, _, callback) {
|
|
// Check if user has written "lib."
|
|
const line = session.doc["$lines"][pos.row].slice(0, pos.column);
|
|
if (line.match(/lib\.\w*$/)) {
|
|
callback(null, []);
|
|
return;
|
|
}
|
|
|
|
const locals = [];
|
|
for (let i = 0; i <= pos.row; ++i) {
|
|
const line = session.doc["$lines"][i].trim();
|
|
if (line.startsWith("const ") || line.startsWith("let ")) {
|
|
const [_, name] = line.match(/^\w+ (\w+)/);
|
|
locals.push(name);
|
|
continue;
|
|
}
|
|
|
|
if (line.startsWith("function ")) {
|
|
const lineNoFunction = line.slice("function ".length);
|
|
const [_, name] = lineNoFunction.match(/^\s*(\w+)/);
|
|
locals.push(name);
|
|
|
|
let local = "";
|
|
const start = lineNoFunction.indexOf(name) + name.length;
|
|
for (let i = start; i < lineNoFunction.length; ++i) {
|
|
if (lineNoFunction[i].trim() === "") {
|
|
continue;
|
|
}
|
|
const ch = lineNoFunction[i];
|
|
if (["{", "=", "}", "(", ")", "[", "]"].includes(ch)) {
|
|
continue;
|
|
}
|
|
if (ch === ",") {
|
|
locals.push(local);
|
|
local = "";
|
|
continue;
|
|
}
|
|
local += ch;
|
|
}
|
|
if (local.trim() !== "") {
|
|
locals.push(local);
|
|
}
|
|
|
|
continue;
|
|
}
|
|
}
|
|
|
|
const wordList = []
|
|
.concat(locals.map((word) => ({
|
|
name: word,
|
|
value: word,
|
|
score: 0,
|
|
meta: "local",
|
|
})))
|
|
.concat(KEYWORDS.map((word) => ({
|
|
name: word,
|
|
value: word,
|
|
score: 0,
|
|
meta: "keyword",
|
|
})));
|
|
|
|
return callback(null, wordList);
|
|
}
|
|
}
|