Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lsp updates #128

Merged
merged 4 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/editor/MonacoEditor/MonacoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default function MonacoEditor({

useEffect(() => {
// TODO fix LSP connection
if (lspEnabled && false) {
if (lspEnabled) {
return createLSPConnection();
}
}, [lspEnabled]);
Expand Down
50 changes: 23 additions & 27 deletions src/components/editor/MonacoEditor/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
MonacoLanguageClient,
CloseAction,
ErrorAction,
MonacoServices,
MessageTransports,
} from 'monaco-languageclient';
import {
Expand All @@ -27,29 +26,13 @@ const notify = (message: string) => {

export default function createLSPConnection() {
notify('Connecting to server...');

const url = createUrl('lsp.usaco.guide', 3000, '/sampleServer');
let webSocket: WebSocket | null = new WebSocket(url);
let languageClient: MonacoLanguageClient | null;

webSocket.onopen = () => {
// if webSocket is null, that means we closed the connection before we got a response from the server
// though I don't think this is ever true?? whatever
const ping = setInterval(() => {
if (!webSocket) return;

const socket = toSocket(webSocket);
const reader = new WebSocketMessageReader(socket);
const writer = new WebSocketMessageWriter(socket);
languageClient = createLanguageClient({
reader,
writer,
});
languageClient.start();
reader.onClose(() => {
languageClient?.stop();
languageClient = null;
});
};
webSocket.send(JSON.stringify({ jsonrpc: '2.0', method: 'ping' }));
}, 5000);
danielzsh marked this conversation as resolved.
Show resolved Hide resolved
let languageClient: MonacoLanguageClient | null;

webSocket.addEventListener('message', event => {
let message;
Expand All @@ -59,8 +42,20 @@ export default function createLSPConnection() {
console.error('Malformed message from LSP server:', event.data);
return;
}

if (message.id === 0 && message.result.capabilities) {
if (!webSocket) return;
if (message.method === 'start') {
const socket = toSocket(webSocket);
const reader = new WebSocketMessageReader(socket);
const writer = new WebSocketMessageWriter(socket);
languageClient = createLanguageClient({
reader,
writer,
});
languageClient.start();
} else if (message.method === 'reject') {
notify('Servers full, try again later!');
dispose();
} else if (message.id === 0 && message.result.capabilities) {
// assume this is the first message from the server
// and that connection is successfully established
notify('Connected');
Expand Down Expand Up @@ -119,10 +114,10 @@ export default function createLSPConnection() {
const protocol = location.protocol === 'https:' ? 'wss' : 'wss';
return normalizeUrl(`${protocol}://${hostname}:${port}${path}`);
}

return () => {
function dispose() {
clearInterval(ping);
if (!languageClient) {
// possibly didn't conenct to websocket before exiting
// possibly didn't connect to websocket before exiting
if (webSocket && webSocket.readyState === webSocket.CONNECTING) {
webSocket.close();
webSocket = null;
Expand All @@ -131,5 +126,6 @@ export default function createLSPConnection() {
languageClient.stop();
languageClient = null;
}
};
}
return dispose;
}
Loading