diff --git a/src/text_completer.js b/src/text_completer.js index 9fde3b3..46e9ba7 100644 --- a/src/text_completer.js +++ b/src/text_completer.js @@ -56,7 +56,7 @@ const KEYWORDS = [ ]; export class TextCompleter { - getCompletions(_editor, session, pos, prefix, callback) { + 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*$/)) { @@ -66,6 +66,41 @@ export class TextCompleter { 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 = [] @@ -74,7 +109,6 @@ export class TextCompleter { value: word, score: 0, meta: "local", - docHTML: signature, }))) .concat(KEYWORDS.map((word) => ({ name: word,