From 3b9f540eff493312ee1fe194eefdda8aac268578 Mon Sep 17 00:00:00 2001 From: Pokey Rule <755842+pokey@users.noreply.github.com> Date: Thu, 12 Oct 2023 16:51:12 +0100 Subject: [PATCH] Expand scope visualizer api --- .../src/ScopeVisualizerCommandApi.ts | 11 +++++-- packages/cursorless-vscode/src/extension.ts | 32 ++++++++++++++++--- .../cursorless-vscode/src/registerCommands.ts | 4 +-- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/packages/cursorless-vscode/src/ScopeVisualizerCommandApi.ts b/packages/cursorless-vscode/src/ScopeVisualizerCommandApi.ts index 77dcc80c6a..561776fe8f 100644 --- a/packages/cursorless-vscode/src/ScopeVisualizerCommandApi.ts +++ b/packages/cursorless-vscode/src/ScopeVisualizerCommandApi.ts @@ -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"; diff --git a/packages/cursorless-vscode/src/extension.ts b/packages/cursorless-vscode/src/extension.ts index 941817b47d..e24a986682 100644 --- a/packages/cursorless-vscode/src/extension.ts +++ b/packages/cursorless-vscode/src/extension.ts @@ -1,4 +1,5 @@ import { + Disposable, FakeIDE, getFakeCommandServerApi, IDE, @@ -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"; @@ -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, ); @@ -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) { @@ -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); + }, + }; }, }; } diff --git a/packages/cursorless-vscode/src/registerCommands.ts b/packages/cursorless-vscode/src/registerCommands.ts index 55c076ea8c..18b18c6299 100644 --- a/packages/cursorless-vscode/src/registerCommands.ts +++ b/packages/cursorless-vscode/src/registerCommands.ts @@ -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 {