refactor(code-editor): dynamically fetch lspConfig from LanguageServerConfig
This commit is contained in:
parent
9ef83d99b6
commit
5f79671aa5
@ -8,6 +8,7 @@ import { shikiToMonaco } from "@shikijs/monaco";
|
|||||||
import type { Monaco } from "@monaco-editor/react";
|
import type { Monaco } from "@monaco-editor/react";
|
||||||
import { useCallback, useEffect, useRef } from "react";
|
import { useCallback, useEffect, useRef } from "react";
|
||||||
import { useMonacoTheme } from "@/hooks/use-monaco-theme";
|
import { useMonacoTheme } from "@/hooks/use-monaco-theme";
|
||||||
|
import LanguageServerConfig from "@/config/language-server";
|
||||||
import { connectToLanguageServer } from "@/lib/language-server";
|
import { connectToLanguageServer } from "@/lib/language-server";
|
||||||
import { useCodeEditorStore } from "@/store/useCodeEditorStore";
|
import { useCodeEditorStore } from "@/store/useCodeEditorStore";
|
||||||
import type { MonacoLanguageClient } from "monaco-languageclient";
|
import type { MonacoLanguageClient } from "monaco-languageclient";
|
||||||
@ -40,7 +41,6 @@ export default function CodeEditor() {
|
|||||||
language,
|
language,
|
||||||
path,
|
path,
|
||||||
value,
|
value,
|
||||||
lspConfig,
|
|
||||||
editorConfig,
|
editorConfig,
|
||||||
isLspEnabled,
|
isLspEnabled,
|
||||||
setEditor,
|
setEditor,
|
||||||
@ -51,7 +51,11 @@ export default function CodeEditor() {
|
|||||||
|
|
||||||
// Connect to LSP only if enabled
|
// Connect to LSP only if enabled
|
||||||
const connectLSP = useCallback(async () => {
|
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 there's an existing language client, stop it first
|
||||||
if (monacoLanguageClientRef.current) {
|
if (monacoLanguageClientRef.current) {
|
||||||
@ -72,7 +76,7 @@ export default function CodeEditor() {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to connect to LSP:", error);
|
console.error("Failed to connect to LSP:", error);
|
||||||
}
|
}
|
||||||
}, [isLspEnabled, language, lspConfig]);
|
}, [isLspEnabled, language]);
|
||||||
|
|
||||||
// Connect to LSP once the editor has mounted
|
// Connect to LSP once the editor has mounted
|
||||||
const handleEditorDidMount = useCallback(
|
const handleEditorDidMount = useCallback(
|
||||||
@ -81,7 +85,7 @@ export default function CodeEditor() {
|
|||||||
await connectLSP();
|
await connectLSP();
|
||||||
setEditor(editor);
|
setEditor(editor);
|
||||||
},
|
},
|
||||||
[connectLSP]
|
[connectLSP, setEditor]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Reconnect to the LSP whenever language or lspConfig changes
|
// Reconnect to the LSP whenever language or lspConfig changes
|
||||||
|
@ -3,7 +3,6 @@ import { getPath } from "@/lib/utils";
|
|||||||
import type { editor } from "monaco-editor";
|
import type { editor } from "monaco-editor";
|
||||||
import { JudgeResultMetadata } from "@/types/judge";
|
import { JudgeResultMetadata } from "@/types/judge";
|
||||||
import { EditorLanguage } from "@/types/editor-language";
|
import { EditorLanguage } from "@/types/editor-language";
|
||||||
import LanguageServerConfig from "@/config/language-server";
|
|
||||||
import { createJSONStorage, persist } from "zustand/middleware";
|
import { createJSONStorage, persist } from "zustand/middleware";
|
||||||
import { LanguageServerMetadata } from "@/types/language-server";
|
import { LanguageServerMetadata } from "@/types/language-server";
|
||||||
import { DefaultEditorOptionConfig } from "@/config/editor-option";
|
import { DefaultEditorOptionConfig } from "@/config/editor-option";
|
||||||
@ -14,7 +13,7 @@ interface CodeEditorState {
|
|||||||
language: EditorLanguage;
|
language: EditorLanguage;
|
||||||
path: string;
|
path: string;
|
||||||
value: string;
|
value: string;
|
||||||
lspConfig: LanguageServerMetadata;
|
lspConfig: LanguageServerMetadata | null;
|
||||||
isLspEnabled: boolean;
|
isLspEnabled: boolean;
|
||||||
editorConfig: editor.IEditorConstructionOptions;
|
editorConfig: editor.IEditorConstructionOptions;
|
||||||
editor: editor.IStandaloneCodeEditor | null;
|
editor: editor.IStandaloneCodeEditor | null;
|
||||||
@ -37,7 +36,7 @@ export const useCodeEditorStore = create<CodeEditorState>()(
|
|||||||
language: DefaultEditorLanguageConfig.id,
|
language: DefaultEditorLanguageConfig.id,
|
||||||
path: getPath(DefaultEditorLanguageConfig.id),
|
path: getPath(DefaultEditorLanguageConfig.id),
|
||||||
value: "#include<stdio.h>",
|
value: "#include<stdio.h>",
|
||||||
lspConfig: LanguageServerConfig[DefaultEditorLanguageConfig.id],
|
lspConfig: null,
|
||||||
isLspEnabled: true,
|
isLspEnabled: true,
|
||||||
editorConfig: DefaultEditorOptionConfig,
|
editorConfig: DefaultEditorOptionConfig,
|
||||||
editor: null,
|
editor: null,
|
||||||
@ -59,7 +58,6 @@ export const useCodeEditorStore = create<CodeEditorState>()(
|
|||||||
language: state.language,
|
language: state.language,
|
||||||
path: state.path,
|
path: state.path,
|
||||||
value: state.value,
|
value: state.value,
|
||||||
lspConfig: state.lspConfig,
|
|
||||||
isLspEnabled: state.isLspEnabled,
|
isLspEnabled: state.isLspEnabled,
|
||||||
editorConfig: state.editorConfig,
|
editorConfig: state.editorConfig,
|
||||||
}),
|
}),
|
||||||
|
Loading…
Reference in New Issue
Block a user