Skip to content

Commit

Permalink
Expand scope visualizer api (cursorless-dev#1946)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
pokey authored and fidgetingbits committed Nov 3, 2023
1 parent d67e397 commit d689c5b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
11 changes: 9 additions & 2 deletions packages/cursorless-vscode/src/ScopeVisualizerCommandApi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { ScopeType } from "@cursorless/common";
import { Disposable, ScopeType } from "@cursorless/common";

export interface ScopeVisualizerCommandApi {
export type ScopeVisualizerListener = (
scopeType: ScopeType | undefined,
visualizationType: VisualizationType | undefined,
) => void;

export interface ScopeVisualizer {
start(scopeType: ScopeType, visualizationType: VisualizationType): void;
stop(): void;
readonly scopeType: ScopeType | undefined;
onDidChangeScopeType(listener: ScopeVisualizerListener): Disposable;
}

export type VisualizationType = "content" | "removal" | "iteration";
32 changes: 28 additions & 4 deletions packages/cursorless-vscode/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Disposable,
FakeIDE,
getFakeCommandServerApi,
IDE,
Expand Down Expand Up @@ -35,7 +36,8 @@ import { KeyboardCommands } from "./keyboard/KeyboardCommands";
import { registerCommands } from "./registerCommands";
import { ReleaseNotes } from "./ReleaseNotes";
import {
ScopeVisualizerCommandApi,
ScopeVisualizer,
ScopeVisualizerListener,
VisualizationType,
} from "./ScopeVisualizerCommandApi";
import { StatusBarItem } from "./StatusBarItem";
Expand Down Expand Up @@ -91,13 +93,14 @@ export async function activate(

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

registerCommands(
context,
vscodeIDE,
commandApi,
testCaseRecorder,
createScopeVisualizerCommandApi(normalizedIde, scopeProvider),
scopeVisualizer,
keyboardCommands,
hats,
);
Expand Down Expand Up @@ -155,11 +158,14 @@ function createTreeSitter(parseTreeApi: ParseTreeApi): TreeSitter {
};
}

function createScopeVisualizerCommandApi(
function createScopeVisualizer(
ide: IDE,
scopeProvider: ScopeProvider,
): ScopeVisualizerCommandApi {
): ScopeVisualizer {
let scopeVisualizer: VscodeScopeVisualizer | undefined;
let currentScopeType: ScopeType | undefined;

const listeners: ScopeVisualizerListener[] = [];

return {
start(scopeType: ScopeType, visualizationType: VisualizationType) {
Expand All @@ -171,11 +177,29 @@ function createScopeVisualizerCommandApi(
visualizationType,
);
scopeVisualizer.start();
currentScopeType = scopeType;
listeners.forEach((listener) => listener(scopeType, visualizationType));
},

stop() {
scopeVisualizer?.dispose();
scopeVisualizer = undefined;
currentScopeType = undefined;
listeners.forEach((listener) => listener(undefined, undefined));
},

get scopeType() {
return currentScopeType;
},

onDidChangeScopeType(listener: ScopeVisualizerListener): Disposable {
listeners.push(listener);

return {
dispose() {
listeners.splice(listeners.indexOf(listener), 1);
},
};
},
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cursorless-vscode/src/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import { showDocumentation, showQuickPick } from "./commands";
import { VscodeIDE } from "./ide/vscode/VscodeIDE";
import { VscodeHats } from "./ide/vscode/hats/VscodeHats";
import { KeyboardCommands } from "./keyboard/KeyboardCommands";
import { ScopeVisualizerCommandApi } from "./ScopeVisualizerCommandApi";
import { ScopeVisualizer } from "./ScopeVisualizerCommandApi";

export function registerCommands(
extensionContext: vscode.ExtensionContext,
vscodeIde: VscodeIDE,
commandApi: CommandApi,
testCaseRecorder: TestCaseRecorder,
scopeVisualizer: ScopeVisualizerCommandApi,
scopeVisualizer: ScopeVisualizer,
keyboardCommands: KeyboardCommands,
hats: VscodeHats,
): void {
Expand Down

0 comments on commit d689c5b

Please sign in to comment.