Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: control whether previewer to dispose when editor closed #3888

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<N extends IDisposable> extends Disposable {
@Autowired(INJECTOR_TOKEN)
Expand All @@ -22,7 +30,17 @@ export abstract class BaseInlineDiffPreviewer<N extends IDisposable> 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()!;
Expand Down Expand Up @@ -226,10 +244,13 @@ export class LiveInlineDiffPreviewer extends BaseInlineDiffPreviewer<InlineStrea
this.node.addLinesToDiff(content);
this.onEnd();
}
serializeState(): SerializableState {
return this.node.serializeState();
serializeState(): IExtendedSerializedState {
return {
...this.node.serializeState(),
options: this.options,
};
}
restoreState(state: SerializableState): void {
restoreState(state: IExtendedSerializedState): void {
this.node.restoreState(state);
}
get onPartialEditEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { InlineChatController } from '../inline-chat/inline-chat-controller';
import { EResultKind } from '../inline-chat/inline-chat.service';
import {
BaseInlineDiffPreviewer,
IDiffPreviewerOptions,
IExtendedSerializedState,
LiveInlineDiffPreviewer,
SideBySideInlineDiffWidget,
} from '../inline-diff/inline-diff-previewer';
Expand Down Expand Up @@ -54,7 +56,7 @@ export class InlineDiffHandler extends IAIMonacoContribHandler {
BaseInlineDiffPreviewer<InlineDiffWidget | InlineStreamDiffHandler> | undefined
>();

protected _store = new Map<string, SerializableState>();
protected _store = new Map<string, IExtendedSerializedState>();

constructor() {
super();
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -142,13 +144,14 @@ export class InlineDiffHandler extends IAIMonacoContribHandler {
options: {
crossSelection: monaco.Selection;
chatResponse?: ChatResponse | InlineChatController;
previewerOptions?: IDiffPreviewerOptions;
},
): BaseInlineDiffPreviewer<InlineDiffWidget | InlineStreamDiffHandler> {
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();
Expand Down Expand Up @@ -201,17 +204,17 @@ 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<InlineDiffWidget | InlineStreamDiffHandler>;

const inlineDiffMode = this.preferenceService.getValid<EInlineDiffPreviewMode>(
AINativeSettingSectionsId.InlineDiffPreviewMode,
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);
Expand Down Expand Up @@ -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();
}
}
Expand Down
Loading