|
| 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 { Dimension, IDomPosition } from 'vs/base/browser/dom'; |
| 7 | +import { CancellationToken } from 'vs/base/common/cancellation'; |
| 8 | +import { IContextKeyService, IScopedContextKeyService } from 'vs/platform/contextkey/common/contextkey'; |
| 9 | +import { IEditorOptions } from 'vs/platform/editor/common/editor'; |
| 10 | +import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; |
| 11 | +import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection'; |
| 12 | +import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage'; |
| 13 | +import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry'; |
| 14 | +import { editorBackground, editorForeground, inputBackground } from 'vs/platform/theme/common/colorRegistry'; |
| 15 | +import { IThemeService } from 'vs/platform/theme/common/themeService'; |
| 16 | +import { EditorPane } from 'vs/workbench/browser/parts/editor/editorPane'; |
| 17 | +import { IEditorOpenContext } from 'vs/workbench/common/editor'; |
| 18 | +import { Memento } from 'vs/workbench/common/memento'; |
| 19 | +import { AideChatEditorInput } from 'vs/workbench/contrib/aideChat/browser/aideChatEditorInput'; |
| 20 | +import { AideChatWidget, IAideChatViewState } from 'vs/workbench/contrib/aideChat/browser/aideChatWidget'; |
| 21 | +import { IAideChatModel } from 'vs/workbench/contrib/aideChat/common/aideChatModel'; |
| 22 | +import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService'; |
| 23 | + |
| 24 | +export interface IAideChatEditorOptions extends IEditorOptions { |
| 25 | + target?: { sessionId: string }; |
| 26 | +} |
| 27 | + |
| 28 | +export class AideChatEditor extends EditorPane { |
| 29 | + private widget!: AideChatWidget; |
| 30 | + |
| 31 | + private _scopedContextKeyService!: IScopedContextKeyService; |
| 32 | + override get scopedContextKeyService() { |
| 33 | + return this._scopedContextKeyService; |
| 34 | + } |
| 35 | + |
| 36 | + private _memento: Memento | undefined; |
| 37 | + private _viewState: IAideChatViewState | undefined; |
| 38 | + |
| 39 | + constructor( |
| 40 | + group: IEditorGroup, |
| 41 | + @ITelemetryService telemetryService: ITelemetryService, |
| 42 | + @IThemeService themeService: IThemeService, |
| 43 | + @IInstantiationService private readonly instantiationService: IInstantiationService, |
| 44 | + @IStorageService private readonly storageService: IStorageService, |
| 45 | + @IContextKeyService private readonly contextKeyService: IContextKeyService, |
| 46 | + ) { |
| 47 | + super(AideChatEditorInput.EditorID, group, telemetryService, themeService, storageService); |
| 48 | + } |
| 49 | + |
| 50 | + protected override createEditor(parent: HTMLElement): void { |
| 51 | + this._scopedContextKeyService = this._register(this.contextKeyService.createScoped(parent)); |
| 52 | + const scopedInstantiationService = this.instantiationService.createChild(new ServiceCollection([IContextKeyService, this.scopedContextKeyService])); |
| 53 | + |
| 54 | + this.widget = this._register(scopedInstantiationService.createInstance( |
| 55 | + AideChatWidget, |
| 56 | + { |
| 57 | + listForeground: editorForeground, |
| 58 | + listBackground: editorBackground, |
| 59 | + inputEditorBackground: inputBackground, |
| 60 | + resultEditorBackground: editorBackground |
| 61 | + } |
| 62 | + )); |
| 63 | + this.widget.render(parent); |
| 64 | + } |
| 65 | + |
| 66 | + override async setInput(input: AideChatEditorInput, options: IAideChatEditorOptions | undefined, context: IEditorOpenContext, token: CancellationToken): Promise<void> { |
| 67 | + super.setInput(input, options, context, token); |
| 68 | + |
| 69 | + const editorModel = await input.resolve(); |
| 70 | + if (!editorModel) { |
| 71 | + throw new Error(`Failed to get model for chat editor. id: ${input.sessionId}`); |
| 72 | + } |
| 73 | + |
| 74 | + if (!this.widget) { |
| 75 | + throw new Error('ChatEditor lifecycle issue: no editor widget'); |
| 76 | + } |
| 77 | + |
| 78 | + this.updateModel(editorModel.model, options?.viewState ?? input.options.viewState); |
| 79 | + } |
| 80 | + |
| 81 | + private updateModel(model: IAideChatModel, viewState?: IAideChatViewState): void { |
| 82 | + this._memento = new Memento('aide-chat-editor', this.storageService); |
| 83 | + this._viewState = viewState ?? this._memento.getMemento(StorageScope.WORKSPACE, StorageTarget.MACHINE) as IAideChatViewState; |
| 84 | + this.widget.setModel(model, { ...this._viewState }); |
| 85 | + } |
| 86 | + |
| 87 | + override layout(dimension: Dimension, position?: IDomPosition): void { |
| 88 | + if (this.widget) { |
| 89 | + this.widget.layout(dimension.height, dimension.width); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments