Skip to content

Commit bf29047

Browse files
Added focused element type (#17)
1 parent 050af7d commit bf29047

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
{
3838
"command": "command-server.runCommand",
3939
"title": "Read command description from a file and execute the command"
40+
},
41+
{
42+
"command": "command-server.getFocusedElementType",
43+
"title": "Returns the type of the currently focused element in vscode"
4044
}
4145
],
4246
"keybindings": [
@@ -49,6 +53,34 @@
4953
"command": "command-server.runCommand",
5054
"key": "ctrl+shift+alt+p",
5155
"mac": "cmd+shift+alt+p"
56+
},
57+
{
58+
"command": "command-server.runCommand",
59+
"key": "ctrl+shift+f17",
60+
"mac": "cmd+shift+f17",
61+
"when": "editorTextFocus",
62+
"args": "textEditor"
63+
},
64+
{
65+
"command": "command-server.runCommand",
66+
"key": "ctrl+shift+alt+p",
67+
"mac": "cmd+shift+alt+p",
68+
"when": "editorTextFocus",
69+
"args": "textEditor"
70+
},
71+
{
72+
"command": "command-server.runCommand",
73+
"key": "ctrl+shift+f17",
74+
"mac": "cmd+shift+f17",
75+
"when": "terminalFocus",
76+
"args": "terminal"
77+
},
78+
{
79+
"command": "command-server.runCommand",
80+
"key": "ctrl+shift+alt+p",
81+
"mac": "cmd+shift+alt+p",
82+
"when": "terminalFocus",
83+
"args": "terminal"
5284
}
5385
],
5486
"configuration": {

src/commandRunner.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as vscode from "vscode";
55
import { readRequest, writeResponse } from "./io";
66
import { getResponsePath } from "./paths";
77
import { any } from "./regex";
8-
import { Request, Response } from "./types";
8+
import { Request } from "./types";
99

1010
export default class CommandRunner {
1111
allowRegex!: RegExp;
@@ -53,7 +53,7 @@ export default class CommandRunner {
5353
async runCommand() {
5454
const responseFile = await open(getResponsePath(), "wx");
5555

56-
var request: Request;
56+
let request: Request;
5757

5858
try {
5959
request = await readRequest();
@@ -86,7 +86,7 @@ export default class CommandRunner {
8686

8787
const commandPromise = vscode.commands.executeCommand(commandId, ...args);
8888

89-
var commandReturnValue = null;
89+
let commandReturnValue = null;
9090

9191
if (returnCommandOutput) {
9292
commandReturnValue = await commandPromise;
@@ -102,7 +102,7 @@ export default class CommandRunner {
102102
});
103103
} catch (err) {
104104
await writeResponse(responseFile, {
105-
error: err.message,
105+
error: (err as Error).message,
106106
uuid,
107107
warnings,
108108
});

src/extension.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
import * as vscode from "vscode";
22

3-
import { initializeCommunicationDir } from "./initializeCommunicationDir";
43
import CommandRunner from "./commandRunner";
4+
import { initializeCommunicationDir } from "./initializeCommunicationDir";
55
import { getInboundSignal } from "./signal";
6+
import { FocusedElementType } from "./types";
67

78
export function activate(context: vscode.ExtensionContext) {
89
initializeCommunicationDir();
910

1011
const commandRunner = new CommandRunner();
12+
let focusedElementType: FocusedElementType | undefined;
1113

1214
context.subscriptions.push(
1315
vscode.commands.registerCommand(
1416
"command-server.runCommand",
15-
commandRunner.runCommand
17+
(focusedElementType_?: FocusedElementType) => {
18+
focusedElementType = focusedElementType_;
19+
return commandRunner.runCommand();
20+
}
21+
),
22+
vscode.commands.registerCommand(
23+
"command-server.getFocusedElementType",
24+
() => focusedElementType ?? null
1625
)
1726
);
1827

1928
return {
29+
/**
30+
* The type of the focused element in vscode at the moment of the command being executed.
31+
*/
32+
getFocusedElementType: () => focusedElementType,
33+
2034
/**
2135
* These signals can be used as a form of IPC to indicate that an event has
2236
* occurred.

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,8 @@ export interface Response {
5151
*/
5252
warnings: string[];
5353
}
54+
55+
/**
56+
* The type of the focused element in vscode at the moment of the command being executed.
57+
*/
58+
export type FocusedElementType = "textEditor" | "terminal";

0 commit comments

Comments
 (0)