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

chore(lsp-cli): use rollup to build everything #2

Merged
merged 1 commit into from Mar 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/lsp-cli/package.json
Expand Up @@ -7,6 +7,7 @@
"lsp": "./dist/cli.js"
},
"scripts": {
"build": "rollup -c",
"test": "biome check ./"
},
"dependencies": {
Expand All @@ -21,8 +22,13 @@
},
"devDependencies": {
"@biomejs/biome": "1.6.1",
"@rollup/plugin-commonjs": "25.0.7",
"@rollup/plugin-node-resolve": "15.2.3",
"@types/node": "20.11.29",
"@types/picomatch": "2.3.3",
"esbuild": "0.20.2",
"rollup": "4.13.0",
"rollup-plugin-esbuild": "6.1.1",
"typescript": "5.4.2"
}
}
23 changes: 23 additions & 0 deletions packages/lsp-cli/rollup.config.js
@@ -0,0 +1,23 @@
import commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import esbuild from 'rollup-plugin-esbuild';

export default [
{
input: './src/cli.ts',
plugins: [
nodeResolve({
exportConditions: ['node'],
}),
commonjs(),
esbuild(),
],
output: [
{
file: './dist/cli.js',
format: 'esm',
sourcemap: true,
},
],
},
];
19 changes: 14 additions & 5 deletions packages/lsp-cli/src/FixCommand.ts
@@ -1,10 +1,10 @@
import * as os from 'node:os';
import { resolve } from 'node:path';
import { basename, resolve } from 'node:path';
import type { WriteStream } from 'node:tty';
import { pathToFileURL } from 'node:url';
import { Chalk, type ChalkInstance } from 'chalk';
import { Option } from 'clipanion';
import { Bar, Progress, TerminalTty } from 'ku-progress-bar';
import { Bar, BarItem, Progress, TerminalTty } from 'ku-progress-bar';
import createThroat from 'throat';
import * as t from 'typanion';

Expand Down Expand Up @@ -126,8 +126,16 @@ export class FixCommand extends BaseLspCommand {
return await fixFile(lsp, path, fixOptions);
});

const progress = new Progress({ total: 1 });
const progress = new Progress({ total: 1 }, { file: '<none>' });
const bar = new Bar(new TerminalTty(stdout as WriteStream));
bar.add(
// TODO(@izaakschroeder): Figure this one out
// @ts-expect-error
new BarItem(progress, {
template:
'[{bar}] {percentage} eta: {eta} elapsed: {duration} {value}/{total} – {file}',
}),
);
const interval = setInterval(() => bar.render(), 50);

let total = 0;
Expand All @@ -138,16 +146,17 @@ export class FixCommand extends BaseLspCommand {
async (path) => {
progress.setTotal(++total);
await exec(path);
progress.increment();
progress.increment(1, { file: basename(path) });
},
);
}

clearInterval(interval);
bar.clean();

stdout.write('🧹 Shutting down language server...\n');
await lsp.request('shutdown');
lsp.notify('exit');
stdout.write('✨ Done.\n');
stdout.write('✨ Done.\n');
}
}
7 changes: 4 additions & 3 deletions packages/lsp-cli/src/createActionFilter.ts
Expand Up @@ -12,14 +12,15 @@ const patternToRegExp = (str: string) => {
return new RegExp(
parts
.map((part, i, arr) => {
const sof = i === 0;
const eof = i === arr.length - 1;
if (part === '*') {
return `[^.]+${eof ? '$' : '\\.'}`;
return `${sof ? '^' : ''}[^.]+${eof ? '$' : '\\.'}`;
}
if (part === '**') {
return `([^.]+\.)*${eof ? '[^.]*$' : ''}`;
return `${sof ? '^' : ''}([^.]+\.)*${eof ? '[^.]*$' : ''}`;
}
return `${escapeRegExp(part)}${eof ? '$' : '\\.'}`;
return `${sof ? '^' : ''}${escapeRegExp(part)}${eof ? '$' : '\\.'}`;
})
.join(''),
);
Expand Down
3 changes: 3 additions & 0 deletions packages/lsp-cli/src/fixFile.ts
Expand Up @@ -90,6 +90,9 @@ export const fixFile = async (
if (options.actionMap) {
desiredActions = desiredActions.map(options.actionMap);
}
if (desiredActions.length === 0) {
return false;
}

const finalChanges: TextEdit[] = [];
const finalActions: CodeActionItem[] = [];
Expand Down
8 changes: 4 additions & 4 deletions packages/lsp-client/src/LspClient.ts
Expand Up @@ -51,8 +51,8 @@ export class LspClient {
});
}

connect(transport: LspTransport) {
const connection = transport.connect({
async connect(transport: LspTransport) {
const connection = await transport.connect({
tap: (name, fn) => {
return this.tap(name, fn);
},
Expand All @@ -64,7 +64,7 @@ export class LspClient {
if (index === -1) {
return;
}
this.#connections.splice(index);
this.#connections.splice(index, 1);
},
});
this.#connections.push(connection);
Expand Down Expand Up @@ -142,7 +142,7 @@ export class LspClient {
const method = notifications[name];
const arg = method.params.parse(params);
this.#rpc.notify(name, arg);
this.#events.emit(name, arg);
this.#events.emit(`$tap-${name}`, arg);
}

tap<N extends All>(name: N, cb: (params: Params<N>) => void) {
Expand Down
4 changes: 3 additions & 1 deletion packages/lsp-client/src/LspStdioTransport.ts
Expand Up @@ -33,9 +33,11 @@ export class LspStdioTransport implements LspTransport {
// TODO(@izaakschroeder): Propagate error information
params.onClose();
});

return {
send: (json: JSONRPCPayload) => {
if (child.killed) {
throw new Error();
}
const payload = JSON.stringify(json);
const len = Buffer.byteLength(payload);
child.stdin.write(`Content-Length: ${len}\r\n\r\n${payload}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/lsp-client/src/createTransport.ts
Expand Up @@ -3,7 +3,7 @@ import { LspStdioTransport } from './LspStdioTransport';
export const createTransport = (urlString: string) => {
const url = new URL(urlString);
switch (url.protocol) {
case 'stdio': {
case 'stdio:': {
const bin = url.hostname;
const args = url.searchParams.getAll('arg');
return new LspStdioTransport(bin, args);
Expand Down
9 changes: 0 additions & 9 deletions packages/lsp-client/src/schema.ts
Expand Up @@ -89,15 +89,6 @@ export const all = {
...events,
};

type VoidEntries<T> = {
[P in keyof T as T[P] extends { params: z.ZodVoid } ? P : never]: T[P];
};
type NonVoidEntries<T> = {
[P in keyof T as T[P] extends { params: z.ZodVoid } ? never : P]: T[P];
};
type VoidNames<T> = keyof VoidEntries<T>;
type NonVoidNames<T> = keyof NonVoidEntries<T>;

export type All = keyof typeof all;

export type Params<T extends All> = (typeof all)[T]['params'];
Expand Down