Skip to content

Commit d689c5b

Browse files
pokeyfidgetingbits
authored and
fidgetingbits
committed
Expand scope visualizer api (cursorless-dev#1946)
- Required by cursorless-dev#1942 to be able to tell which scope is currently being visualized to highlight it in the sidebar ## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [ ] I have not broken the cheatsheet
1 parent d67e397 commit d689c5b

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import { ScopeType } from "@cursorless/common";
1+
import { Disposable, ScopeType } from "@cursorless/common";
22

3-
export interface ScopeVisualizerCommandApi {
3+
export type ScopeVisualizerListener = (
4+
scopeType: ScopeType | undefined,
5+
visualizationType: VisualizationType | undefined,
6+
) => void;
7+
8+
export interface ScopeVisualizer {
49
start(scopeType: ScopeType, visualizationType: VisualizationType): void;
510
stop(): void;
11+
readonly scopeType: ScopeType | undefined;
12+
onDidChangeScopeType(listener: ScopeVisualizerListener): Disposable;
613
}
714

815
export type VisualizationType = "content" | "removal" | "iteration";

packages/cursorless-vscode/src/extension.ts

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
Disposable,
23
FakeIDE,
34
getFakeCommandServerApi,
45
IDE,
@@ -35,7 +36,8 @@ import { KeyboardCommands } from "./keyboard/KeyboardCommands";
3536
import { registerCommands } from "./registerCommands";
3637
import { ReleaseNotes } from "./ReleaseNotes";
3738
import {
38-
ScopeVisualizerCommandApi,
39+
ScopeVisualizer,
40+
ScopeVisualizerListener,
3941
VisualizationType,
4042
} from "./ScopeVisualizerCommandApi";
4143
import { StatusBarItem } from "./StatusBarItem";
@@ -91,13 +93,14 @@ export async function activate(
9193

9294
const statusBarItem = StatusBarItem.create("cursorless.showQuickPick");
9395
const keyboardCommands = KeyboardCommands.create(context, statusBarItem);
96+
const scopeVisualizer = createScopeVisualizer(normalizedIde, scopeProvider);
9497

9598
registerCommands(
9699
context,
97100
vscodeIDE,
98101
commandApi,
99102
testCaseRecorder,
100-
createScopeVisualizerCommandApi(normalizedIde, scopeProvider),
103+
scopeVisualizer,
101104
keyboardCommands,
102105
hats,
103106
);
@@ -155,11 +158,14 @@ function createTreeSitter(parseTreeApi: ParseTreeApi): TreeSitter {
155158
};
156159
}
157160

158-
function createScopeVisualizerCommandApi(
161+
function createScopeVisualizer(
159162
ide: IDE,
160163
scopeProvider: ScopeProvider,
161-
): ScopeVisualizerCommandApi {
164+
): ScopeVisualizer {
162165
let scopeVisualizer: VscodeScopeVisualizer | undefined;
166+
let currentScopeType: ScopeType | undefined;
167+
168+
const listeners: ScopeVisualizerListener[] = [];
163169

164170
return {
165171
start(scopeType: ScopeType, visualizationType: VisualizationType) {
@@ -171,11 +177,29 @@ function createScopeVisualizerCommandApi(
171177
visualizationType,
172178
);
173179
scopeVisualizer.start();
180+
currentScopeType = scopeType;
181+
listeners.forEach((listener) => listener(scopeType, visualizationType));
174182
},
175183

176184
stop() {
177185
scopeVisualizer?.dispose();
178186
scopeVisualizer = undefined;
187+
currentScopeType = undefined;
188+
listeners.forEach((listener) => listener(undefined, undefined));
189+
},
190+
191+
get scopeType() {
192+
return currentScopeType;
193+
},
194+
195+
onDidChangeScopeType(listener: ScopeVisualizerListener): Disposable {
196+
listeners.push(listener);
197+
198+
return {
199+
dispose() {
200+
listeners.splice(listeners.indexOf(listener), 1);
201+
},
202+
};
179203
},
180204
};
181205
}

packages/cursorless-vscode/src/registerCommands.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import { showDocumentation, showQuickPick } from "./commands";
1414
import { VscodeIDE } from "./ide/vscode/VscodeIDE";
1515
import { VscodeHats } from "./ide/vscode/hats/VscodeHats";
1616
import { KeyboardCommands } from "./keyboard/KeyboardCommands";
17-
import { ScopeVisualizerCommandApi } from "./ScopeVisualizerCommandApi";
17+
import { ScopeVisualizer } from "./ScopeVisualizerCommandApi";
1818

1919
export function registerCommands(
2020
extensionContext: vscode.ExtensionContext,
2121
vscodeIde: VscodeIDE,
2222
commandApi: CommandApi,
2323
testCaseRecorder: TestCaseRecorder,
24-
scopeVisualizer: ScopeVisualizerCommandApi,
24+
scopeVisualizer: ScopeVisualizer,
2525
keyboardCommands: KeyboardCommands,
2626
hats: VscodeHats,
2727
): void {

0 commit comments

Comments
 (0)