|
| 1 | +import { |
| 2 | + Disposable, |
| 3 | + ScopeType, |
| 4 | + simpleScopeTypeTypes, |
| 5 | +} from "@cursorless/common"; |
| 6 | +import { pull } from "lodash"; |
| 7 | +import { ScopeSupport, ScopeSupportEventCallback } from ".."; |
| 8 | +import { Debouncer } from "../core/Debouncer"; |
| 9 | +import { ide } from "../singletons/ide.singleton"; |
| 10 | +import { ScopeSupportChecker } from "./ScopeSupportChecker"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Watches for changes to the scope support of the active editor and notifies |
| 14 | + * listeners when it changes. Watches support for all scopes at the same time. |
| 15 | + */ |
| 16 | +export class ScopeSupportWatcher { |
| 17 | + private disposables: Disposable[] = []; |
| 18 | + private debouncer = new Debouncer(() => this.onChange()); |
| 19 | + private listeners: ScopeSupportEventCallback[] = []; |
| 20 | + |
| 21 | + constructor(private scopeSupportChecker: ScopeSupportChecker) { |
| 22 | + this.disposables.push( |
| 23 | + // An event that fires when a text document opens |
| 24 | + ide().onDidOpenTextDocument(this.debouncer.run), |
| 25 | + // An Event that fires when a text document closes |
| 26 | + ide().onDidCloseTextDocument(this.debouncer.run), |
| 27 | + // An Event which fires when the active editor has changed. Note that the event also fires when the active editor changes to undefined. |
| 28 | + ide().onDidChangeActiveTextEditor(this.debouncer.run), |
| 29 | + // An event that is emitted when a text document is changed. This usually |
| 30 | + // happens when the contents changes but also when other things like the |
| 31 | + // dirty-state changes. |
| 32 | + ide().onDidChangeTextDocument(this.debouncer.run), |
| 33 | + this.debouncer, |
| 34 | + ); |
| 35 | + |
| 36 | + this.onDidChangeScopeSupport = this.onDidChangeScopeSupport.bind(this); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Registers a callback to be run when the scope ranges change for any visible |
| 41 | + * editor. The callback will be run immediately once for each visible editor |
| 42 | + * with the current scope ranges. |
| 43 | + * @param callback The callback to run when the scope ranges change |
| 44 | + * @param config The configuration for the scope ranges |
| 45 | + * @returns A {@link Disposable} which will stop the callback from running |
| 46 | + */ |
| 47 | + onDidChangeScopeSupport(callback: ScopeSupportEventCallback): Disposable { |
| 48 | + callback(this.getSupportLevels()); |
| 49 | + |
| 50 | + this.listeners.push(callback); |
| 51 | + |
| 52 | + return { |
| 53 | + dispose: () => { |
| 54 | + pull(this.listeners, callback); |
| 55 | + }, |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + private onChange() { |
| 60 | + if (this.listeners.length === 0) { |
| 61 | + // Don't bother if no one is listening |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const supportLevels = this.getSupportLevels(); |
| 66 | + |
| 67 | + this.listeners.forEach((listener) => listener(supportLevels)); |
| 68 | + } |
| 69 | + |
| 70 | + private getSupportLevels() { |
| 71 | + console.log("getSupportLevels"); |
| 72 | + const activeTextEditor = ide().activeTextEditor; |
| 73 | + |
| 74 | + const getScopeTypeSupport = |
| 75 | + activeTextEditor == null |
| 76 | + ? () => ScopeSupport.unsupported |
| 77 | + : (scopeType: ScopeType) => |
| 78 | + this.scopeSupportChecker.getScopeSupport( |
| 79 | + activeTextEditor, |
| 80 | + scopeType, |
| 81 | + ); |
| 82 | + |
| 83 | + const scopeTypes: ScopeType[] = simpleScopeTypeTypes |
| 84 | + // Ignore instance pseuto-scope for now |
| 85 | + .filter((scopeTypeType) => scopeTypeType !== "instance") |
| 86 | + .map((scopeTypeType) => ({ |
| 87 | + type: scopeTypeType, |
| 88 | + })); |
| 89 | + |
| 90 | + // TODO: Support surrounding pairs |
| 91 | + |
| 92 | + const supportLevels = scopeTypes.map((scopeType) => ({ |
| 93 | + scopeType, |
| 94 | + support: getScopeTypeSupport(scopeType), |
| 95 | + })); |
| 96 | + return supportLevels; |
| 97 | + } |
| 98 | + |
| 99 | + dispose(): void { |
| 100 | + this.disposables.forEach(({ dispose }) => { |
| 101 | + try { |
| 102 | + dispose(); |
| 103 | + } catch (e) { |
| 104 | + // do nothing; some of the VSCode disposables misbehave, and we don't |
| 105 | + // want that to prevent us from disposing the rest of the disposables |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | +} |
0 commit comments