Skip to content

Commit 0ea02a2

Browse files
Use Talon rpc library (#34)
Use the Talon rpc library as the single source of truth for the rpc server protocol. Is also used by: https://github.com/AndreasArvidsson/clippy https://github.com/AndreasArvidsson/talon-browser
1 parent c94f885 commit 0ea02a2

File tree

10 files changed

+37
-317
lines changed

10 files changed

+37
-317
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"type": "git",
99
"url": "https://github.com/cursorless-dev/command-server"
1010
},
11-
"version": "0.10.1",
11+
"version": "0.11.0",
1212
"engines": {
1313
"vscode": "^1.53.0"
1414
},
@@ -138,6 +138,7 @@
138138
},
139139
"dependencies": {
140140
"minimatch": "^3.0.4",
141-
"rimraf": "^3.0.2"
141+
"rimraf": "^3.0.2",
142+
"talon-rpc": "2.1.0"
142143
}
143144
}

src/commandRunner.ts

Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
import { Minimatch } from "minimatch";
2+
import type { RequestCallbackOptions } from "talon-rpc";
23
import * as vscode from "vscode";
3-
44
import { any } from "./regex";
5-
import { Request } from "./types";
6-
import { Io } from "./io";
75

86
export default class CommandRunner {
97
private allowRegex!: RegExp;
108
private denyRegex!: RegExp | null;
119
private backgroundWindowProtection!: boolean;
1210

13-
constructor(private io: Io) {
11+
constructor() {
1412
this.reloadConfiguration = this.reloadConfiguration.bind(this);
1513
this.runCommand = this.runCommand.bind(this);
1614

1715
this.reloadConfiguration();
1816
vscode.workspace.onDidChangeConfiguration(this.reloadConfiguration);
1917
}
2018

21-
reloadConfiguration() {
19+
private reloadConfiguration() {
2220
const allowList = vscode.workspace
2321
.getConfiguration("command-server")
2422
.get<string[]>("allowList")!;
@@ -41,77 +39,23 @@ export default class CommandRunner {
4139
.get<boolean>("backgroundWindowProtection")!;
4240
}
4341

44-
/**
45-
* Reads a command from the request file and executes it. Writes information
46-
* about command execution to the result of the command to the response file,
47-
* If requested, will wait for command to finish, and can also write command
48-
* output to response file. See also documentation for Request / Response
49-
* types.
50-
*/
51-
async runCommand() {
52-
await this.io.prepareResponse();
53-
54-
let request: Request;
55-
56-
try {
57-
request = await this.io.readRequest();
58-
} catch (err) {
59-
await this.io.closeResponse();
60-
throw err;
61-
}
62-
63-
const { commandId, args, uuid, returnCommandOutput, waitForFinish } =
64-
request;
65-
66-
const warnings = [];
67-
68-
let commandPromise: Thenable<unknown> | undefined;
69-
70-
try {
71-
if (!vscode.window.state.focused) {
72-
if (this.backgroundWindowProtection) {
73-
throw new Error("This editor is not active");
74-
} else {
75-
warnings.push("This editor is not active");
76-
}
77-
}
78-
79-
if (!commandId.match(this.allowRegex)) {
80-
throw new Error("Command not in allowList");
42+
runCommand(commandId: string, args: any[], options: RequestCallbackOptions) {
43+
if (!vscode.window.state.focused) {
44+
if (this.backgroundWindowProtection) {
45+
throw new Error("This editor is not active");
46+
} else {
47+
options.warn("This editor is not active");
8148
}
82-
83-
if (this.denyRegex != null && commandId.match(this.denyRegex)) {
84-
throw new Error("Command in denyList");
85-
}
86-
87-
commandPromise = vscode.commands.executeCommand(commandId, ...args);
88-
89-
let commandReturnValue = null;
90-
91-
if (returnCommandOutput) {
92-
commandReturnValue = await commandPromise;
93-
} else if (waitForFinish) {
94-
await commandPromise;
95-
}
96-
97-
await this.io.writeResponse({
98-
error: null,
99-
uuid,
100-
returnValue: commandReturnValue,
101-
warnings,
102-
});
103-
} catch (err) {
104-
await this.io.writeResponse({
105-
error: (err as Error).message,
106-
uuid,
107-
warnings,
108-
});
10949
}
11050

111-
await this.io.closeResponse();
51+
if (!commandId.match(this.allowRegex)) {
52+
throw new Error("Command not in allowList");
53+
}
11254

113-
if (commandPromise != null) {
114-
await commandPromise;
55+
if (this.denyRegex != null && commandId.match(this.denyRegex)) {
56+
throw new Error("Command in denyList");
11557
}
58+
59+
return vscode.commands.executeCommand(commandId, ...args);
11660
}
11761
}

src/constants.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
// How old a request file needs to be before we declare it stale and are willing
2-
// to remove it
3-
export const STALE_TIMEOUT_MS = 60000;
4-
5-
// The amount of time that client is expected to wait for VSCode to perform a
6-
// command, in seconds
7-
export const VSCODE_COMMAND_TIMEOUT_MS = 3000;
1+
export const RPC_DIR_NAME = "vscode-command-server";

src/extension.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1+
import { NodeIo, TalonRpcServer } from "talon-rpc";
12
import * as vscode from "vscode";
2-
3-
import { NativeIo } from "./nativeIo";
43
import CommandRunner from "./commandRunner";
4+
import { RPC_DIR_NAME } from "./constants";
55
import { FocusedElementType } from "./types";
66

77
export async function activate(context: vscode.ExtensionContext) {
8-
const io = new NativeIo();
8+
const commandRunner = new CommandRunner();
9+
const io = new NodeIo(RPC_DIR_NAME);
10+
const rpc = new TalonRpcServer(io, commandRunner.runCommand);
11+
912
await io.initialize();
1013

11-
const commandRunner = new CommandRunner(io);
1214
let focusedElementType: FocusedElementType | undefined;
1315

1416
context.subscriptions.push(
1517
vscode.commands.registerCommand(
1618
"command-server.runCommand",
1719
async (focusedElementType_: FocusedElementType) => {
1820
focusedElementType = focusedElementType_;
19-
await commandRunner.runCommand();
21+
await rpc.executeRequest();
2022
focusedElementType = undefined;
2123
}
2224
),
25+
2326
vscode.commands.registerCommand(
2427
"command-server.getFocusedElementType",
2528
() => focusedElementType

src/io.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/nativeIo.ts

Lines changed: 0 additions & 122 deletions
This file was deleted.

src/paths.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)