From 407019ba88010d902968aa5aa3e577e34e6ee9df Mon Sep 17 00:00:00 2001 From: Reimar Pihl Browa Date: Sat, 11 Oct 2025 16:55:13 +0200 Subject: [PATCH] add gamelib autocompletion --- index.html | 3 +++ src/gamelib_completer.js | 36 ++++++++++++++++++++++++++++++++++++ src/index.js | 7 +++++++ 3 files changed, 46 insertions(+) create mode 100644 src/gamelib_completer.js diff --git a/index.html b/index.html index 3ecc762..5015c0f 100644 --- a/index.html +++ b/index.html @@ -8,6 +8,9 @@ Karlkoder Playground + diff --git a/src/gamelib_completer.js b/src/gamelib_completer.js new file mode 100644 index 0000000..5f68a71 --- /dev/null +++ b/src/gamelib_completer.js @@ -0,0 +1,36 @@ +import { Gamelib } from "./gamelib.js"; + +export class GamelibCompleter { + getCompletions(_editor, session, pos, prefix, 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; + } + + if (prefix.length === 0) { + callback(null, []); + return; + } + + const functions = Object.getOwnPropertyNames(Gamelib.prototype); + + const wordList = functions.map((func) => { + const definition = Gamelib.prototype[func].toString(); + const signature = definition.slice(0, definition.indexOf(")") + 1); + + return { + "name": func, + "value": func, + "snippet": func + "($0)", + "caption": func, + "score": 300, + "meta": "function", + "docHTML": signature, + }; + }); + + return callback(null, wordList); + } +} diff --git a/src/index.js b/src/index.js index 89217c9..1161024 100644 --- a/src/index.js +++ b/src/index.js @@ -9,6 +9,7 @@ import { Gamelib } from "./gamelib.js"; import { CodeStopper } from "./code_stopper.js"; import { Vermiparous } from "./vermiparous.js"; import { promptUpload } from "./prompt_upload.js"; +import { GamelibCompleter } from "./gamelib_completer.js"; const playgroundConsole = new PlaygroundConsole( document.querySelector("#console-code"), @@ -38,6 +39,12 @@ const editor = ace.edit("editor"); editor.setTheme("ace/theme/gruvbox"); editor.session.setMode("ace/mode/javascript"); +const langTools = ace.require("ace/ext/language_tools"); + +editor.setOptions({ enableBasicAutocompletion: true, enableLiveAutocompletion: true }); + +langTools.addCompleter(new GamelibCompleter()); + editor.setValue(sessionStorage.getItem("code") ?? editor.getValue(), -1); const importButton = document.querySelector("#import-button");