Skip to content

Commit

Permalink
migrate to Modal LSP
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodingwizard committed Jun 6, 2024
1 parent 8d2607e commit c7969f7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/components/CodeInterface/CodeInterface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const CodeInterface = ({
defaultValue={defaultCode[lang]}
yjsDocumentId={`${fileData.id}.${lang}`}
useEditorWithVim={true}
lspEnabled={lang === 'cpp'}
lspEnabled={true} // at some point, maybe make this a user setting?
dataTestId="code-editor"
/>
</div>
Expand Down
10 changes: 5 additions & 5 deletions src/components/editor/MonacoEditor/MonacoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default function MonacoEditor({
useState<monaco.editor.IStandaloneCodeEditor | null>(null);

useEffect(() => {
const modelPath = `file:///home/thecodingwizard/${path ?? 'default'}`;
const modelPath = `file:///root/${path ?? 'default'}`;

editorRef.current = monaco.editor.create(
ref.current!,
Expand Down Expand Up @@ -111,11 +111,11 @@ export default function MonacoEditor({
}, [editor, yjsInfo]);

useEffect(() => {
// TODO fix LSP connection
if (lspEnabled) {
return createLSPConnection();
if (lspEnabled && (language === 'cpp' || language === 'python')) {
// yikes, ugly how there's both python and py
return createLSPConnection(language);
}
}, [lspEnabled]);
}, [lspEnabled, language]);

useEffect(() => {
if (vim) {
Expand Down
48 changes: 22 additions & 26 deletions src/components/editor/MonacoEditor/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import normalizeUrl from 'normalize-url';

import toast from 'react-hot-toast';

// note: all the toast notifications shoudl probably be moved up to MonacoEditor.tsx
// note: all the toast notifications should probably be moved up to MonacoEditor.tsx
const notify = (message: string) => {
toast(message, {
style: {
Expand All @@ -24,21 +24,30 @@ const notify = (message: string) => {
});
};

export default function createLSPConnection() {
export default function createLSPConnection(language: 'cpp' | 'python') {
if (language !== 'cpp' && language !== 'python') {
throw new Error('Unsupported LSP language: ' + language);
}

notify('Connecting to server...');
const url = createUrl('lsp.usaco.guide', 3000, '/sampleServer');
const url = createUrl(
'thecodingwizard--lsp-server-main.modal.run',
443,
language === 'cpp' ? '/clangd' : '/pyright'
);

let webSocket: WebSocket | null = new WebSocket(url);
let ping: ReturnType<typeof setInterval> | null = null;
let languageClient: MonacoLanguageClient | null;

webSocket.addEventListener('open', () => {
ping = setInterval(() => {
if (!webSocket || webSocket.readyState !== WebSocket.OPEN) {
clearInterval(ping!);
return;
}
webSocket.send(JSON.stringify({ jsonrpc: '2.0', method: 'ping' }));
}, 5000);
const socket = toSocket(webSocket!);
const reader = new WebSocketMessageReader(socket);
const writer = new WebSocketMessageWriter(socket);
languageClient = createLanguageClient({
reader,
writer,
});
languageClient.start();
});

webSocket.addEventListener('message', event => {
Expand All @@ -50,19 +59,7 @@ export default function createLSPConnection() {
return;
}
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) {
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 @@ -91,7 +88,7 @@ export default function createLSPConnection() {
name: 'Sample Language Client',
clientOptions: {
// use a language id as a document selector
documentSelector: ['cpp'],
documentSelector: [language],
// disable the default error handler
errorHandler: {
error: (error, message, count) => {
Expand Down Expand Up @@ -122,7 +119,6 @@ export default function createLSPConnection() {
return normalizeUrl(`${protocol}://${hostname}:${port}${path}`);
}
function dispose() {
if (ping) clearInterval(ping);
if (!languageClient) {
// possibly didn't connect to websocket before exiting
if (webSocket && webSocket.readyState === webSocket.CONNECTING) {
Expand Down

0 comments on commit c7969f7

Please sign in to comment.