|
| 1 | +import { Disposable } from "@cursorless/common"; |
| 2 | +import { pull } from "lodash"; |
| 3 | +import { |
| 4 | + IterationScopeChangeEventCallback, |
| 5 | + IterationScopeRangeConfig, |
| 6 | + ScopeChangeEventCallback, |
| 7 | + ScopeRangeConfig, |
| 8 | +} from ".."; |
| 9 | +import { Debouncer } from "../core/Debouncer"; |
| 10 | +import { ide } from "../singletons/ide.singleton"; |
| 11 | +import { ScopeRangeProvider } from "./ScopeRangeProvider"; |
| 12 | + |
| 13 | +/** |
| 14 | + * Watches for changes to the scope ranges of visible editors and notifies |
| 15 | + * listeners when they change. |
| 16 | + */ |
| 17 | +export class ScopeRangeWatcher { |
| 18 | + private disposables: Disposable[] = []; |
| 19 | + private debouncer = new Debouncer(() => this.onChange()); |
| 20 | + private listeners: (() => void)[] = []; |
| 21 | + |
| 22 | + constructor(private scopeRangeProvider: ScopeRangeProvider) { |
| 23 | + this.disposables.push( |
| 24 | + // An Event which fires when the array of visible editors has changed. |
| 25 | + ide().onDidChangeVisibleTextEditors(this.debouncer.run), |
| 26 | + // An event that fires when a text document opens |
| 27 | + ide().onDidOpenTextDocument(this.debouncer.run), |
| 28 | + // An Event that fires when a text document closes |
| 29 | + ide().onDidCloseTextDocument(this.debouncer.run), |
| 30 | + // An event that is emitted when a text document is changed. This usually |
| 31 | + // happens when the contents changes but also when other things like the |
| 32 | + // dirty-state changes. |
| 33 | + ide().onDidChangeTextDocument(this.debouncer.run), |
| 34 | + ide().onDidChangeTextEditorVisibleRanges(this.debouncer.run), |
| 35 | + this.debouncer, |
| 36 | + ); |
| 37 | + |
| 38 | + this.onDidChangeScopeRanges = this.onDidChangeScopeRanges.bind(this); |
| 39 | + this.onDidChangeIterationScopeRanges = |
| 40 | + this.onDidChangeIterationScopeRanges.bind(this); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Registers a callback to be run when the scope ranges change for any visible |
| 45 | + * editor. The callback will be run immediately once for each visible editor |
| 46 | + * with the current scope ranges. |
| 47 | + * @param callback The callback to run when the scope ranges change |
| 48 | + * @param config The configuration for the scope ranges |
| 49 | + * @returns A {@link Disposable} which will stop the callback from running |
| 50 | + */ |
| 51 | + onDidChangeScopeRanges( |
| 52 | + callback: ScopeChangeEventCallback, |
| 53 | + config: ScopeRangeConfig, |
| 54 | + ): Disposable { |
| 55 | + const fn = () => { |
| 56 | + ide().visibleTextEditors.forEach((editor) => { |
| 57 | + callback( |
| 58 | + editor, |
| 59 | + this.scopeRangeProvider.provideScopeRanges(editor, config), |
| 60 | + ); |
| 61 | + }); |
| 62 | + }; |
| 63 | + |
| 64 | + this.listeners.push(fn); |
| 65 | + |
| 66 | + fn(); |
| 67 | + |
| 68 | + return { |
| 69 | + dispose: () => { |
| 70 | + pull(this.listeners, fn); |
| 71 | + }, |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Registers a callback to be run when the iteration scope ranges change for |
| 77 | + * any visible editor. The callback will be run immediately once for each |
| 78 | + * visible editor with the current iteration scope ranges. |
| 79 | + * @param callback The callback to run when the scope ranges change |
| 80 | + * @param config The configuration for the scope ranges |
| 81 | + * @returns A {@link Disposable} which will stop the callback from running |
| 82 | + */ |
| 83 | + onDidChangeIterationScopeRanges( |
| 84 | + callback: IterationScopeChangeEventCallback, |
| 85 | + config: IterationScopeRangeConfig, |
| 86 | + ): Disposable { |
| 87 | + const fn = () => { |
| 88 | + ide().visibleTextEditors.forEach((editor) => { |
| 89 | + callback( |
| 90 | + editor, |
| 91 | + this.scopeRangeProvider.provideIterationScopeRanges(editor, config), |
| 92 | + ); |
| 93 | + }); |
| 94 | + }; |
| 95 | + |
| 96 | + this.listeners.push(fn); |
| 97 | + |
| 98 | + fn(); |
| 99 | + |
| 100 | + return { |
| 101 | + dispose: () => { |
| 102 | + pull(this.listeners, fn); |
| 103 | + }, |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + private onChange() { |
| 108 | + this.listeners.forEach((listener) => listener()); |
| 109 | + } |
| 110 | + |
| 111 | + dispose(): void { |
| 112 | + this.disposables.forEach(({ dispose }) => { |
| 113 | + try { |
| 114 | + dispose(); |
| 115 | + } catch (e) { |
| 116 | + // do nothing; some of the VSCode disposables misbehave, and we don't |
| 117 | + // want that to prevent us from disposing the rest of the disposables |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | +} |
0 commit comments