|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { WindowIntervalTimer } from '../../../base/browser/dom.js'; |
| 7 | +import { BugIndicatingError } from '../../../base/common/errors.js'; |
| 8 | +import { Emitter, Event } from '../../../base/common/event.js'; |
| 9 | +import { Disposable } from '../../../base/common/lifecycle.js'; |
| 10 | +import { localize, localize2 } from '../../../nls.js'; |
| 11 | +import { Action2 } from '../../../platform/actions/common/actions.js'; |
| 12 | +import { ContextKeyExpr, IContextKeyService, RawContextKey } from '../../../platform/contextkey/common/contextkey.js'; |
| 13 | +import { InstantiationType, registerSingleton } from '../../../platform/instantiation/common/extensions.js'; |
| 14 | +import { createDecorator, ServicesAccessor } from '../../../platform/instantiation/common/instantiation.js'; |
| 15 | +import { IQuickInputService, IQuickPickItem } from '../../../platform/quickinput/common/quickInput.js'; |
| 16 | + |
| 17 | +export const IInlineCompletionsService = createDecorator<IInlineCompletionsService>('IInlineCompletionsService'); |
| 18 | + |
| 19 | +export interface IInlineCompletionsService { |
| 20 | + readonly _serviceBrand: undefined; |
| 21 | + |
| 22 | + onDidChangeIsSnoozing: Event<boolean>; |
| 23 | + |
| 24 | + /** |
| 25 | + * Get the remaining time (in ms) for which inline completions should be snoozed, |
| 26 | + * or 0 if not snoozed. |
| 27 | + */ |
| 28 | + readonly snoozeTimeLeft: number; |
| 29 | + |
| 30 | + /** |
| 31 | + * Snooze inline completions for the specified duration. If already snoozed, extend the snooze time. |
| 32 | + */ |
| 33 | + snooze(durationMs?: number): void; |
| 34 | + |
| 35 | + /** |
| 36 | + * Snooze inline completions for the specified duration. If already snoozed, overwrite the existing snooze time. |
| 37 | + */ |
| 38 | + setSnoozeDuration(durationMs: number): void; |
| 39 | + |
| 40 | + /** |
| 41 | + * Check if inline completions are currently snoozed. |
| 42 | + */ |
| 43 | + isSnoozing(): boolean; |
| 44 | + |
| 45 | + /** |
| 46 | + * Cancel the current snooze. |
| 47 | + */ |
| 48 | + cancelSnooze(): void; |
| 49 | +} |
| 50 | + |
| 51 | +const InlineCompletionsSnoozing = new RawContextKey<boolean>('inlineCompletions.snoozed', false, localize('inlineCompletions.snoozed', "Whether inline completions are currently snoozed")); |
| 52 | + |
| 53 | +export class InlineCompletionsService extends Disposable implements IInlineCompletionsService { |
| 54 | + declare readonly _serviceBrand: undefined; |
| 55 | + |
| 56 | + private _onDidChangeIsSnoozing = this._register(new Emitter<boolean>()); |
| 57 | + readonly onDidChangeIsSnoozing: Event<boolean> = this._onDidChangeIsSnoozing.event; |
| 58 | + |
| 59 | + private static readonly SNOOZE_DURATION = 300_000; // 5 minutes |
| 60 | + |
| 61 | + private _snoozeTimeEnd: undefined | number = undefined; |
| 62 | + get snoozeTimeLeft(): number { |
| 63 | + if (this._snoozeTimeEnd === undefined) { |
| 64 | + return 0; |
| 65 | + } |
| 66 | + return Math.max(0, this._snoozeTimeEnd - Date.now()); |
| 67 | + } |
| 68 | + |
| 69 | + private _timer: WindowIntervalTimer; |
| 70 | + |
| 71 | + constructor(@IContextKeyService private _contextKeyService: IContextKeyService) { |
| 72 | + super(); |
| 73 | + |
| 74 | + this._timer = this._register(new WindowIntervalTimer()); |
| 75 | + |
| 76 | + const inlineCompletionsSnoozing = InlineCompletionsSnoozing.bindTo(this._contextKeyService); |
| 77 | + this._register(this.onDidChangeIsSnoozing(() => inlineCompletionsSnoozing.set(this.isSnoozing()))); |
| 78 | + } |
| 79 | + |
| 80 | + snooze(durationMs: number = InlineCompletionsService.SNOOZE_DURATION): void { |
| 81 | + this.setSnoozeDuration(durationMs + this.snoozeTimeLeft); |
| 82 | + } |
| 83 | + |
| 84 | + setSnoozeDuration(durationMs: number): void { |
| 85 | + const wasSnoozing = this.isSnoozing(); |
| 86 | + |
| 87 | + if (this._snoozeTimeEnd === undefined) { |
| 88 | + this._snoozeTimeEnd = Date.now() + durationMs; |
| 89 | + } else if (this.snoozeTimeLeft > 0) { |
| 90 | + this._snoozeTimeEnd += durationMs; |
| 91 | + } else { |
| 92 | + this._snoozeTimeEnd = Date.now() + durationMs; |
| 93 | + } |
| 94 | + |
| 95 | + const isSnoozing = this.isSnoozing(); |
| 96 | + if (wasSnoozing !== isSnoozing) { |
| 97 | + this._onDidChangeIsSnoozing.fire(isSnoozing); |
| 98 | + } |
| 99 | + |
| 100 | + if (isSnoozing) { |
| 101 | + this._timer.cancelAndSet( |
| 102 | + () => { |
| 103 | + if (!this.isSnoozing()) { |
| 104 | + this._onDidChangeIsSnoozing.fire(false); |
| 105 | + } else { |
| 106 | + throw new BugIndicatingError('Snooze timer did not fire as expected'); |
| 107 | + } |
| 108 | + }, |
| 109 | + this.snoozeTimeLeft + 1, |
| 110 | + ); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + isSnoozing(): boolean { |
| 115 | + return this.snoozeTimeLeft > 0; |
| 116 | + } |
| 117 | + |
| 118 | + cancelSnooze(): void { |
| 119 | + if (this.isSnoozing()) { |
| 120 | + this._snoozeTimeEnd = undefined; |
| 121 | + this._timer.cancel(); |
| 122 | + this._onDidChangeIsSnoozing.fire(false); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +registerSingleton(IInlineCompletionsService, InlineCompletionsService, InstantiationType.Delayed); |
| 128 | + |
| 129 | +const snoozeInlineSuggestId = 'editor.action.inlineSuggest.snooze'; |
| 130 | +const cancelSnoozeInlineSuggestId = 'editor.action.inlineSuggest.cancelSnooze'; |
| 131 | + |
| 132 | +export class SnoozeInlineCompletion extends Action2 { |
| 133 | + public static ID = snoozeInlineSuggestId; |
| 134 | + constructor() { |
| 135 | + super({ |
| 136 | + id: SnoozeInlineCompletion.ID, |
| 137 | + title: localize2('action.inlineSuggest.snooze', "Snooze Inline Suggestions"), |
| 138 | + precondition: ContextKeyExpr.true(), |
| 139 | + f1: true, |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + public async run(accessor: ServicesAccessor): Promise<void> { |
| 144 | + const quickInputService = accessor.get(IQuickInputService); |
| 145 | + const inlineCompletionsService = accessor.get(IInlineCompletionsService); |
| 146 | + |
| 147 | + const items: IQuickPickItem[] = [ |
| 148 | + { label: '5 minutes', id: '5', picked: true }, |
| 149 | + { label: '10 minutes', id: '10' }, |
| 150 | + { label: '15 minutes', id: '15' }, |
| 151 | + { label: '30 minutes', id: '30' }, |
| 152 | + { label: '60 minutes', id: '60' } |
| 153 | + ]; |
| 154 | + |
| 155 | + const picked = await quickInputService.pick(items, { |
| 156 | + placeHolder: localize('snooze.placeholder', "Select snooze duration") |
| 157 | + }); |
| 158 | + |
| 159 | + if (picked) { |
| 160 | + const minutes = parseInt(picked.id!, 10); |
| 161 | + const durationMs = minutes * 60 * 1000; |
| 162 | + inlineCompletionsService.setSnoozeDuration(durationMs); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +export class CancelSnoozeInlineCompletion extends Action2 { |
| 168 | + public static ID = cancelSnoozeInlineSuggestId; |
| 169 | + constructor() { |
| 170 | + super({ |
| 171 | + id: CancelSnoozeInlineCompletion.ID, |
| 172 | + title: localize2('action.inlineSuggest.cancelSnooze', "Cancel Snooze Inline Suggestions"), |
| 173 | + precondition: InlineCompletionsSnoozing, |
| 174 | + f1: true, |
| 175 | + }); |
| 176 | + } |
| 177 | + |
| 178 | + public async run(accessor: ServicesAccessor): Promise<void> { |
| 179 | + accessor.get(IInlineCompletionsService).cancelSnooze(); |
| 180 | + } |
| 181 | +} |
0 commit comments