Skip to content

Commit

Permalink
Merge pull request #400 from moonbitlang/zhiyuan/tour-auto-trace
Browse files Browse the repository at this point in the history
auto trace in tour
  • Loading branch information
bzy-debug authored Jan 3, 2025
2 parents a374df6 + 5b0125e commit 9007d64
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
2 changes: 1 addition & 1 deletion moonbit-tour/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"vite": "^6.0.1"
},
"dependencies": {
"@moonbit/moonpad-monaco": "^0.1.202501030",
"@moonbit/moonpad-monaco": "^0.1.202501031",
"monaco-editor-core": "^0.52.0"
}
}
57 changes: 32 additions & 25 deletions moonbit-tour/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const moon = moonbitMode.init({
lspWorker: new lspWorker(),
mooncWorkerFactory: () => new mooncWorker(),
codeLensFilter(l) {
return !(l.command?.command === "moonbit-lsp/run-main");
return l.command?.command === "moonbit-lsp/debug-main";
},
});

Expand Down Expand Up @@ -84,31 +84,38 @@ const model = monaco.editor.createModel(
);

const output = document.querySelector<HTMLPreElement>("#output")!;
const trace = moonbitMode.traceCommandFactory();

async function run(debug: boolean) {
const result = await moon.compile({
libUris: [monaco.Uri.file("/main.mbt").toString()],
debugMain: debug,
});
switch (result.kind) {
case "success": {
const js = result.js;
const stream = await moon.run(js);
let buffer = "";
await stream.pipeTo(
new WritableStream({
write(chunk) {
buffer += `${chunk}\n`;
},
}),
);
output.textContent = buffer;
return;
}
case "error": {
console.error(result.diagnostics);
if (debug) {
const result = await moon.compile({
libInputs: [["main.mbt", model.getValue()]],
debugMain: true,
});
switch (result.kind) {
case "success": {
const js = result.js;
const stream = await moon.run(js);
let buffer = "";
await stream.pipeTo(
new WritableStream({
write(chunk) {
buffer += `${chunk}\n`;
},
}),
);
output.textContent = buffer;
return;
}
case "error": {
console.error(result.diagnostics);
}
}
return;
}
const stdout = await trace(monaco.Uri.file("/main.mbt").toString());
if (stdout === undefined) return;
output.textContent = stdout;
}

model.onDidChangeContent(debounce(() => run(false), 100));
Expand All @@ -117,9 +124,9 @@ monaco.editor.onDidCreateEditor(() => {
codePre.remove();
});

const editor = document.getElementById("editor")!;
editor.classList.remove("pl-[10px]", "text-[14px]");
monaco.editor.create(editor, {
const editorContainer = document.getElementById("editor")!;
editorContainer.classList.remove("pl-[10px]", "text-[14px]");
monaco.editor.create(editorContainer, {
model,
lineNumbers: "off",
glyphMargin: false,
Expand Down
23 changes: 17 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9007d64

Please sign in to comment.