add gamelib autocompletion

This commit is contained in:
Reimar Pihl Browa 2025-10-11 16:55:13 +02:00
parent 59b7577068
commit 407019ba88
3 changed files with 46 additions and 0 deletions

View File

@ -8,6 +8,9 @@
<title>Karlkoder Playground</title>
<link rel="stylesheet" href="./style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.43.2/ace.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.43.2/ext-language_tools.js"
></script>
<script src="./src/index.js" type="module"></script>
</head>
<body>

36
src/gamelib_completer.js Normal file
View File

@ -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);
}
}

View File

@ -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");