Skip to content

Commit

Permalink
Merge pull request #63 from georgestagg/null-lsp
Browse files Browse the repository at this point in the history
  • Loading branch information
wch authored Sep 13, 2023
2 parents 1bd9505 + ce42528 commit 4f5dc4e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import * as fileio from "../fileio";
import { createUri } from "../language-server/client";
import { LSPClient } from "../language-server/lsp-client";
import { ensureNullClient } from "../language-server/null-client";
import { ensurePyrightClient } from "../language-server/pyright-client";
import { inferFiletype, modKeySymbol, stringToUint8Array } from "../utils";
import type { AppEngine, UtilityMethods } from "./App";
Expand Down Expand Up @@ -97,7 +98,9 @@ export default function Editor({
// lsp-extensions.ts, and useTabbedCodeMirror.tsx, there are explicit checks
// that files are python files in order to enable LS features, and they should
// not be necessary at this level.
const lspClient: LSPClient = ensurePyrightClient();
const lspClient: LSPClient = appEngine === 'python'
? ensurePyrightClient()
: ensureNullClient();

// A unique ID for this instance of the Editor. At some point it might make
// sense to hoist this up into the App component, if we need unique IDs for
Expand Down
46 changes: 46 additions & 0 deletions src/language-server/null-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { createUri, LanguageServerClient } from "./client";
import { LSPClient } from "./lsp-client";
import {
AbstractMessageReader,
AbstractMessageWriter,
createMessageConnection,
} from "vscode-jsonrpc";

let nullClient: NullClient | null = null;

/**
* This returns a NullClient object. If this is called multiple times, it
* will return the same object each time.
*/
export function ensureNullClient(): NullClient {
if (!nullClient) {
nullClient = new NullClient();
}
return nullClient;
}

export class NullMessageReader extends AbstractMessageReader {
public listen() {
return { dispose: () => {} };
}
}

export class NullMessageWriter extends AbstractMessageWriter {
public async write() {}
public end(): void {}
}

/**
* A "null" LSP client that listens for messages but does nothing.
*/
export class NullClient extends LSPClient {
constructor() {
const conn = createMessageConnection(
new NullMessageReader(),
new NullMessageWriter()
);
conn.listen();
const client = new LanguageServerClient(conn, "en", createUri(""));
super(client);
}
}

0 comments on commit 4f5dc4e

Please sign in to comment.