feat(playground): enhance code compilation feedback and add syntax highlighting for output messages

This commit is contained in:
ngc2207 2025-01-05 15:28:00 +08:00
parent c629dc5612
commit 88b68d147e
2 changed files with 51 additions and 18 deletions

View File

@ -23,7 +23,10 @@ export async function runCode(code: string, language: string) {
Cmd: [ Cmd: [
"sh", "sh",
"-c", "-c",
`echo '${code}' > main.c && gcc main.c -o main && ./main`, `export LANG=C.UTF-8 && printf '%s' '${code.replace(
/'/g,
"'\\''"
)}' > main.c && gcc main.c -o main && ./main`,
], ],
Tty: false, Tty: false,
}); });
@ -39,7 +42,7 @@ export async function runCode(code: string, language: string) {
const output = await new Promise<string>((resolve, reject) => { const output = await new Promise<string>((resolve, reject) => {
let output = ""; let output = "";
stream.on("data", (chunk) => { stream.on("data", (chunk) => {
output += chunk.toString(); output += chunk.toString("utf8");
}); });
stream.on("end", () => { stream.on("end", () => {
resolve(output); resolve(output);
@ -50,13 +53,27 @@ export async function runCode(code: string, language: string) {
}); });
console.log("Output:", output); console.log("Output:", output);
return output;
// 检查输出中是否有错误信息
const isCompilationSuccess = !output.includes("error:");
return {
success: isCompilationSuccess,
output: output,
message: isCompilationSuccess
? "Compilation successful"
: "Compilation failed",
};
} else { } else {
throw new Error(`Unsupported language: ${language}`); throw new Error(`Unsupported language: ${language}`);
} }
} catch (error) { } catch (error) {
console.error("Error running code:", error); console.error("Error running code:", error);
throw error; return {
success: false,
output: error instanceof Error ? error.message : "Unknown error",
message: "Compilation failed",
};
} finally { } finally {
if (container) { if (container) {
try { try {

View File

@ -11,6 +11,7 @@ import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area"; import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
import { COriginal, CplusplusOriginal, JavaOriginal } from "devicons-react"; import { COriginal, CplusplusOriginal, JavaOriginal } from "devicons-react";
import { connectToLanguageServer, LSP_SUPPORTED_LANGUAGES } from "@/lib/lsp"; import { connectToLanguageServer, LSP_SUPPORTED_LANGUAGES } from "@/lib/lsp";
import { codeToHtml } from "shiki";
const files: { const files: {
[key: string]: { name: string; language: string; value: string }; [key: string]: { name: string; language: string; value: string };
@ -48,6 +49,7 @@ loader.config({ monaco });
export default function PlaygroundPage() { export default function PlaygroundPage() {
const [language, setLanguage] = useState<string>("c"); const [language, setLanguage] = useState<string>("c");
const [highlightedMessage, setHighlightedMessage] = useState<string>("");
const file = files[language]; const file = files[language];
const editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null); const editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null);
const webSocketRef = useRef<WebSocket | null>(null); const webSocketRef = useRef<WebSocket | null>(null);
@ -99,6 +101,29 @@ export default function PlaygroundPage() {
handleLanguageChange(); handleLanguageChange();
}, [language]); }, [language]);
const handleSubmit = async () => {
const code = editorRef.current?.getValue();
if (!code) {
console.error("No code to compile");
return;
}
const result = await runCode(code, language);
// 根据编译结果显示不同的信息
const statusMessage = result.success
? "Compilation successful"
: "Compilation failed";
const fullMessage = `${statusMessage}\n\n${result.output}`;
const highlighted = await codeToHtml(fullMessage, {
lang: "log", // 或者根据你的需求选择合适的语言
theme: "one-dark-pro",
});
setHighlightedMessage(highlighted);
};
return ( return (
<div className="h-full flex flex-col"> <div className="h-full flex flex-col">
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
@ -151,17 +176,7 @@ export default function PlaygroundPage() {
</ScrollArea> </ScrollArea>
</Tabs> </Tabs>
<div className="m-3"> <div className="m-3">
<Button <Button variant="outline" onClick={handleSubmit}>
variant="outline"
onClick={async () => {
const code = editorRef.current?.getValue();
if (!code) {
console.error("No code to compile");
return;
}
await runCode(code, language);
}}
>
Submit Submit
<Send <Send
className="-me-1 ms-2 opacity-60" className="-me-1 ms-2 opacity-60"
@ -173,8 +188,7 @@ export default function PlaygroundPage() {
</div> </div>
</div> </div>
<div className="flex flex-1 mt-0 m-3 gap-x-1"> <div className="flex flex-1 mt-0 m-3 gap-x-1">
<div className="w-1/3"></div> <div className="w-2/3">
<div className="w-1/3">
<Editor <Editor
theme="vs-dark" theme="vs-dark"
path={file.name} path={file.name}
@ -208,7 +222,9 @@ export default function PlaygroundPage() {
}} }}
/> />
</div> </div>
<div className="w-1/3"></div> <div className="h-full w-1/3">
<div dangerouslySetInnerHTML={{ __html: highlightedMessage }} />
</div>
</div> </div>
</div> </div>
); );