refactor(code-editor): dynamically fetch lspConfig from LanguageServerConfig

This commit is contained in:
cfngc4594 2025-03-05 13:29:17 +08:00
parent 9ef83d99b6
commit 5f79671aa5
2 changed files with 10 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import { shikiToMonaco } from "@shikijs/monaco";
import type { Monaco } from "@monaco-editor/react";
import { useCallback, useEffect, useRef } from "react";
import { useMonacoTheme } from "@/hooks/use-monaco-theme";
import LanguageServerConfig from "@/config/language-server";
import { connectToLanguageServer } from "@/lib/language-server";
import { useCodeEditorStore } from "@/store/useCodeEditorStore";
import type { MonacoLanguageClient } from "monaco-languageclient";
@ -40,7 +41,6 @@ export default function CodeEditor() {
language,
path,
value,
lspConfig,
editorConfig,
isLspEnabled,
setEditor,
@ -51,7 +51,11 @@ export default function CodeEditor() {
// Connect to LSP only if enabled
const connectLSP = useCallback(async () => {
if (!(isLspEnabled && language && lspConfig && editorRef.current)) return;
if (!(isLspEnabled && language && editorRef.current)) return;
const lspConfig = LanguageServerConfig[language];
if (!lspConfig) return;
// If there's an existing language client, stop it first
if (monacoLanguageClientRef.current) {
@ -72,7 +76,7 @@ export default function CodeEditor() {
} catch (error) {
console.error("Failed to connect to LSP:", error);
}
}, [isLspEnabled, language, lspConfig]);
}, [isLspEnabled, language]);
// Connect to LSP once the editor has mounted
const handleEditorDidMount = useCallback(
@ -81,7 +85,7 @@ export default function CodeEditor() {
await connectLSP();
setEditor(editor);
},
[connectLSP]
[connectLSP, setEditor]
);
// Reconnect to the LSP whenever language or lspConfig changes

View File

@ -3,7 +3,6 @@ import { getPath } from "@/lib/utils";
import type { editor } from "monaco-editor";
import { JudgeResultMetadata } from "@/types/judge";
import { EditorLanguage } from "@/types/editor-language";
import LanguageServerConfig from "@/config/language-server";
import { createJSONStorage, persist } from "zustand/middleware";
import { LanguageServerMetadata } from "@/types/language-server";
import { DefaultEditorOptionConfig } from "@/config/editor-option";
@ -14,7 +13,7 @@ interface CodeEditorState {
language: EditorLanguage;
path: string;
value: string;
lspConfig: LanguageServerMetadata;
lspConfig: LanguageServerMetadata | null;
isLspEnabled: boolean;
editorConfig: editor.IEditorConstructionOptions;
editor: editor.IStandaloneCodeEditor | null;
@ -37,7 +36,7 @@ export const useCodeEditorStore = create<CodeEditorState>()(
language: DefaultEditorLanguageConfig.id,
path: getPath(DefaultEditorLanguageConfig.id),
value: "#include<stdio.h>",
lspConfig: LanguageServerConfig[DefaultEditorLanguageConfig.id],
lspConfig: null,
isLspEnabled: true,
editorConfig: DefaultEditorOptionConfig,
editor: null,
@ -59,7 +58,6 @@ export const useCodeEditorStore = create<CodeEditorState>()(
language: state.language,
path: state.path,
value: state.value,
lspConfig: state.lspConfig,
isLspEnabled: state.isLspEnabled,
editorConfig: state.editorConfig,
}),