Clean up LSPServer
MODIFY
packages/@romejs/core/master/Master.ts
packages/@romejs/core/master/Master.ts
@@ -56,7 +56,7 @@ import {
createUnknownFilePath,
} from '@romejs/path';
import {Dict} from '@romejs/typescript-helpers';
-import {LSPServer} from './lsp/index';
+import LSPServer from './lsp/LSPServer';
import MasterReporter from './MasterReporter';
const STDOUT_MAX_CHUNK_LENGTH = 100_000;

MODIFY
packages/@romejs/core/master/commands/index.ts
packages/@romejs/core/master/commands/index.ts
@@ -22,6 +22,7 @@ import analyzeDependencies from './analyzeDependencies';
import parse from './parse';
import bundle from './bundle';
import format from './format';
+import lsp from './lsp';
// Code Quality
import lint from './lint';
@@ -54,4 +55,5 @@ masterCommands.set('publish', publish);
masterCommands.set('ci', ci);
masterCommands.set('develop', develop);
masterCommands.set('format', format);
+masterCommands.set('lsp', lsp);
masterCommands.set('_noop', noop);

MODIFY
packages/@romejs/core/master/commands/lsp.ts
packages/@romejs/core/master/commands/lsp.ts
import {MasterRequest} from '@romejs/core';
import {commandCategories, createMasterCommand} from '../../commands';
-import {LSPServer} from '../lsp/index';
+import LSPServer from '../lsp/LSPServer';
export default createMasterCommand({
category: commandCategories.PROJECT_MANAGEMENT,

RENAME
packages/@romejs/core/master/lsp/index.ts
packages/@romejs/core/master/lsp/LSPServer.ts
@@ -285,7 +285,7 @@ class LSPProgress extends ReporterProgressBase {
}
}
-export class LSPServer {
+export default class LSPServer {
constructor(request: MasterRequest) {
this.status = 'IDLE';
this.buffer = '';
@@ -315,7 +315,6 @@ export class LSPServer {
write(res: JSONObject) {
const json = JSON.stringify(res);
const out = `Content-Length: ${String(json.length)}${HEADERS_END}${json}`;
- console.error('WRITE', out);
this.client.bridge.lspFromServerBuffer.send(out);
}
@@ -514,12 +513,29 @@ export class LSPServer {
}
}
- async onMessage(headers: Headers, content: string) {
+ normalizeMessage(content: string): undefined | Consumer {
+ try {
const data = JSON.parse(content);
const consumer = consumeUnknown(data, 'lsp/parse');
+ return consumer;
+ } catch (err) {
+ if (err instanceof SyntaxError) {
+ console.error('JSON parse error', content);
+ return undefined;
+ } else {
+ throw err;
+ }
+ }
+ }
+
+ async onMessage(headers: Headers, content: string) {
+ const consumer = this.normalizeMessage(content);
+ if (consumer === undefined) {
+ return;
+ }
if (!consumer.has('method')) {
- console.error(data);
+ console.error('NO METHOD', content);
return;
}

Clean up LSPServer