fix(playground): enhance language server connection handling with support for LSP_SUPPORTED_LANGUAGES

This commit is contained in:
ngc2207 2025-01-05 08:26:13 +08:00
parent 9ecb387418
commit 44ef62c03a
2 changed files with 18 additions and 12 deletions

View File

@ -1,13 +1,13 @@
"use client";
import * as monaco from "monaco-editor";
import { connectToLanguageServer } from "@/lib/lsp";
import { useEffect, useRef, useState } from "react";
import { Editor, loader } from "@monaco-editor/react";
import { MonacoLanguageClient } from "monaco-languageclient";
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
import { COriginal, CplusplusOriginal, JavaOriginal } from "devicons-react";
import { connectToLanguageServer, LSP_SUPPORTED_LANGUAGES } from "@/lib/lsp";
const files: {
[key: string]: { name: string; language: string; value: string };
@ -62,18 +62,23 @@ export default function PlaygroundPage() {
column: lastLineLength + 1,
});
editorRef.current.focus();
if (languageClientRef.current) {
if (
languageClientRef.current &&
language in LSP_SUPPORTED_LANGUAGES
) {
await languageClientRef.current.stop();
languageClientRef.current = null;
}
if (
webSocketRef.current &&
webSocketRef.current.readyState !== WebSocket.CLOSED &&
webSocketRef.current.readyState !== WebSocket.CLOSING
webSocketRef.current.readyState !== WebSocket.CLOSING &&
language in LSP_SUPPORTED_LANGUAGES
) {
webSocketRef.current.close();
webSocketRef.current = null;
}
if(language in LSP_SUPPORTED_LANGUAGES){
try {
const languageClient = await connectToLanguageServer(
language,
@ -85,6 +90,7 @@ export default function PlaygroundPage() {
}
}
}
}
};
handleLanguageChange();

View File

@ -12,7 +12,7 @@ import { RefObject } from "react";
import normalizeUrl from "normalize-url";
import { MonacoLanguageClient } from "monaco-languageclient";
const LSP_SUPPORTED_LANGUAGES: {
export const LSP_SUPPORTED_LANGUAGES: {
[key: string]: { hostname: string; port: number; path: string };
} = {
c: {