From b649b61a2e40570cb140a426cd63ff14dbc5d9a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E5=A3=B0?= Date: Mon, 29 Jul 2024 16:30:28 +0800 Subject: [PATCH] feat: control whether previewer to dispose when editor closed (#3888) * feat: add dispose option * fix: show previewer support option * fix: store options --- .../inline-diff/inline-diff-previewer.ts | 29 ++++++++++++++++--- .../widget/inline-diff/inline-diff.handler.ts | 19 +++++++----- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/packages/ai-native/src/browser/widget/inline-diff/inline-diff-previewer.ts b/packages/ai-native/src/browser/widget/inline-diff/inline-diff-previewer.ts index f11865d472..383581a441 100644 --- a/packages/ai-native/src/browser/widget/inline-diff/inline-diff-previewer.ts +++ b/packages/ai-native/src/browser/widget/inline-diff/inline-diff-previewer.ts @@ -13,6 +13,14 @@ import { SerializableState } from '../inline-stream-diff/live-preview.decoration import { InlineDiffWidget } from './inline-diff-widget'; +export interface IDiffPreviewerOptions { + disposeWhenEditorClosed: boolean; +} + +export interface IExtendedSerializedState extends SerializableState { + readonly options: IDiffPreviewerOptions; +} + @Injectable({ multiple: true }) export abstract class BaseInlineDiffPreviewer extends Disposable { @Autowired(INJECTOR_TOKEN) @@ -22,7 +30,17 @@ export abstract class BaseInlineDiffPreviewer extends Dis protected model: ITextModel; - constructor(protected readonly monacoEditor: ICodeEditor, protected readonly selection: Selection) { + get disposeWhenEditorClosed() { + return this.options.disposeWhenEditorClosed; + } + + constructor( + protected readonly monacoEditor: ICodeEditor, + protected readonly selection: Selection, + public options: IDiffPreviewerOptions = { + disposeWhenEditorClosed: true, + }, + ) { super(); this.node = this.createNode(); this.model = this.monacoEditor.getModel()!; @@ -226,10 +244,13 @@ export class LiveInlineDiffPreviewer extends BaseInlineDiffPreviewer | undefined >(); - protected _store = new Map(); + protected _store = new Map(); constructor() { super(); @@ -85,13 +87,13 @@ export class InlineDiffHandler extends IAIMonacoContribHandler { return this.restoreState(monacoEditor, state); } - restoreState(monacoEditor: monaco.ICodeEditor, state: SerializableState) { + restoreState(monacoEditor: monaco.ICodeEditor, state: IExtendedSerializedState) { const oldDiffPreviewer = this._editorsStore.get(monacoEditor); if (oldDiffPreviewer) { oldDiffPreviewer.dispose(); } - const previewer = this.createDiffPreviewer(monacoEditor, state.selection); + const previewer = this.createDiffPreviewer(monacoEditor, state.selection, state.options); if (previewer instanceof LiveInlineDiffPreviewer) { previewer.restoreState(state); } @@ -142,13 +144,14 @@ export class InlineDiffHandler extends IAIMonacoContribHandler { options: { crossSelection: monaco.Selection; chatResponse?: ChatResponse | InlineChatController; + previewerOptions?: IDiffPreviewerOptions; }, ): BaseInlineDiffPreviewer { const { crossSelection, chatResponse } = options; const disposable = new Disposable(); - const previewer = this.createDiffPreviewer(monacoEditor, crossSelection); + const previewer = this.createDiffPreviewer(monacoEditor, crossSelection, options.previewerOptions); const onFinish = () => { previewer.layout(); @@ -201,7 +204,7 @@ export class InlineDiffHandler extends IAIMonacoContribHandler { return previewer; } - createDiffPreviewer(monacoEditor: monaco.ICodeEditor, selection: monaco.Selection) { + createDiffPreviewer(monacoEditor: monaco.ICodeEditor, selection: monaco.Selection, options?: IDiffPreviewerOptions) { let previewer: BaseInlineDiffPreviewer; const inlineDiffMode = this.preferenceService.getValid( @@ -209,9 +212,9 @@ export class InlineDiffHandler extends IAIMonacoContribHandler { EInlineDiffPreviewMode.inlineLive, ); if (inlineDiffMode === EInlineDiffPreviewMode.sideBySide) { - previewer = this.injector.get(SideBySideInlineDiffWidget, [monacoEditor, selection]); + previewer = this.injector.get(SideBySideInlineDiffWidget, [monacoEditor, selection, options]); } else { - previewer = this.injector.get(LiveInlineDiffPreviewer, [monacoEditor, selection]); + previewer = this.injector.get(LiveInlineDiffPreviewer, [monacoEditor, selection, options]); } previewer.show(selection.startLineNumber - 1, selection.endLineNumber - selection.startLineNumber + 2); @@ -278,7 +281,7 @@ export class InlineDiffHandler extends IAIMonacoContribHandler { const previewer = this._editorsStore.get(e.payload.group.codeEditor.monacoEditor); if (previewer) { - if (previewer.isModel(uriString)) { + if (previewer.disposeWhenEditorClosed && previewer.isModel(uriString)) { previewer.dispose(); } }