add gamelib autocompletion
This commit is contained in:
parent
59b7577068
commit
407019ba88
@ -8,6 +8,9 @@
|
|||||||
<title>Karlkoder Playground</title>
|
<title>Karlkoder Playground</title>
|
||||||
<link rel="stylesheet" href="./style.css">
|
<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/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>
|
<script src="./src/index.js" type="module"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
36
src/gamelib_completer.js
Normal file
36
src/gamelib_completer.js
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,7 @@ import { Gamelib } from "./gamelib.js";
|
|||||||
import { CodeStopper } from "./code_stopper.js";
|
import { CodeStopper } from "./code_stopper.js";
|
||||||
import { Vermiparous } from "./vermiparous.js";
|
import { Vermiparous } from "./vermiparous.js";
|
||||||
import { promptUpload } from "./prompt_upload.js";
|
import { promptUpload } from "./prompt_upload.js";
|
||||||
|
import { GamelibCompleter } from "./gamelib_completer.js";
|
||||||
|
|
||||||
const playgroundConsole = new PlaygroundConsole(
|
const playgroundConsole = new PlaygroundConsole(
|
||||||
document.querySelector("#console-code"),
|
document.querySelector("#console-code"),
|
||||||
@ -38,6 +39,12 @@ const editor = ace.edit("editor");
|
|||||||
editor.setTheme("ace/theme/gruvbox");
|
editor.setTheme("ace/theme/gruvbox");
|
||||||
editor.session.setMode("ace/mode/javascript");
|
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);
|
editor.setValue(sessionStorage.getItem("code") ?? editor.getValue(), -1);
|
||||||
|
|
||||||
const importButton = document.querySelector("#import-button");
|
const importButton = document.querySelector("#import-button");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user