@basthon/kernel-javascript 0.88.0 → 0.89.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/kernel.js CHANGED
@@ -30,7 +30,7 @@ export class JavaScriptKernel extends KernelMainBase {
30
30
  * The language's name, as displayed (see `KernelBase.languageName`).
31
31
  */
32
32
  languageName() {
33
- return "Javascript";
33
+ return "JavaScript";
34
34
  }
35
35
  /**
36
36
  * The extensions of the files installed as modules (see
package/lib/worker.d.ts CHANGED
@@ -65,7 +65,11 @@ export declare class JavaScriptKernelWorker extends KernelWorkerBase {
65
65
  */
66
66
  putModule(filename: string, content: ArrayBuffer): Promise<void>;
67
67
  /**
68
- * Complete the last word with the names of the global scope.
68
+ * Complete the last word: the properties of an object after `a.b.` (its
69
+ * own and inherited ones, the path being made of names only, nothing is
70
+ * called), otherwise the keywords and the names of the global scope.
71
+ * Functions get a `(`, names starting with `__` are only completed after
72
+ * a `_`.
69
73
  *
70
74
  * @param code - The code before the cursor.
71
75
  * @returns `[completions, start]`.
package/lib/worker.js CHANGED
@@ -2,6 +2,11 @@ import { parse } from "acorn";
2
2
  import objectInspect from "object-inspect";
3
3
  import { KernelWorkerBase } from "@basthon/kernel-base/worker";
4
4
  const PARSE_OPTIONS = { ecmaVersion: "latest", sourceType: "script" };
5
+ // the keywords completed at the top level (see complete)
6
+ const KEYWORDS = `async await break case catch class const continue debugger
7
+ default delete do else export extends false finally for function if import
8
+ in instanceof let new null of return static super switch this throw true try
9
+ typeof undefined var void while with yield`.split(/\s+/);
5
10
  /**
6
11
  * The code where the top-level declarations (`let`, `const` and `class`)
7
12
  * are global variables (`var`), as in a browser's console: they persist
@@ -148,18 +153,53 @@ export class JavaScriptKernelWorker extends KernelWorkerBase {
148
153
  }
149
154
  }
150
155
  /**
151
- * Complete the last word with the names of the global scope.
156
+ * Complete the last word: the properties of an object after `a.b.` (its
157
+ * own and inherited ones, the path being made of names only, nothing is
158
+ * called), otherwise the keywords and the names of the global scope.
159
+ * Functions get a `(`, names starting with `__` are only completed after
160
+ * a `_`.
152
161
  *
153
162
  * @param code - The code before the cursor.
154
163
  * @returns `[completions, start]`.
155
164
  */
156
165
  async complete(code) {
157
- // basic completion based on global object
158
- const vars = Object.getOwnPropertyNames(self);
159
- const words = code.match(/(\w+)$/) ?? [];
160
- const word = words[0] ?? "";
161
- const matches = vars.filter((v) => v.startsWith(word));
162
- return [matches, code.length - word.length];
166
+ const [, path, word] = /((?:[\w$]+\??\.)*)([\w$]*)$/.exec(code);
167
+ // a member of what is not a path of names (f()., a[0]., "s".) is not
168
+ // completed
169
+ const before = code[code.length - path.length - word.length - 1] ?? "";
170
+ if (/[.)\]"'`]/.test(before))
171
+ return [];
172
+ // a.b?.c. -> ["a", "b", "c"]
173
+ const names = path.split(/\??\./).slice(0, -1);
174
+ const candidates = new Set(names.length === 0 ? KEYWORDS : []);
175
+ let owner = self;
176
+ try {
177
+ for (const name of names)
178
+ owner = owner?.[name];
179
+ if (owner == null)
180
+ return [];
181
+ for (let o = Object(owner); o != null; o = Object.getPrototypeOf(o))
182
+ for (const name of Object.getOwnPropertyNames(o))
183
+ if (/^[\w$]+$/.test(name))
184
+ candidates.add(name);
185
+ }
186
+ catch {
187
+ return [];
188
+ }
189
+ const matches = [];
190
+ for (const name of candidates) {
191
+ if (!name.startsWith(word))
192
+ continue;
193
+ if (name.startsWith("__") && !word.startsWith("_"))
194
+ continue;
195
+ let value;
196
+ try {
197
+ value = KEYWORDS.includes(name) ? undefined : owner[name];
198
+ }
199
+ catch { }
200
+ matches.push(typeof value === "function" ? `${name}(` : name);
201
+ }
202
+ return [matches.sort(), code.length - word.length];
163
203
  }
164
204
  /**
165
205
  * Does the code need more lines? Yes when it doesn't parse because it
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basthon/kernel-javascript",
3
- "version": "0.88.0",
3
+ "version": "0.89.0",
4
4
  "description": "Basthon - JavaScript Kernel",
5
5
  "homepage": "https://basthon.fr",
6
6
  "bugs": {
@@ -25,7 +25,7 @@
25
25
  "clean": "rimraf lib/"
26
26
  },
27
27
  "dependencies": {
28
- "@basthon/kernel-base": "0.88.0",
28
+ "@basthon/kernel-base": "0.89.0",
29
29
  "acorn": "^8.19.0",
30
30
  "comlink": "^4.4.2",
31
31
  "object-inspect": "^1.13.4"