diff --git a/packages/core-browser/src/react-providers/config-provider.tsx b/packages/core-browser/src/react-providers/config-provider.tsx index 819aaab3e0..28ed6fa14b 100644 --- a/packages/core-browser/src/react-providers/config-provider.tsx +++ b/packages/core-browser/src/react-providers/config-provider.tsx @@ -303,10 +303,6 @@ export interface AppConfig { * This is useful when your scenario is one-time use, and you can control the opening of the editor tab yourself. */ disableRestoreEditorGroupState?: boolean; - /** - * 启用 Diff 编辑器状态恢复逻辑 - */ - enableRestoreDiffEditorState?: boolean; } export interface ICollaborationClientOpts { diff --git a/packages/editor/src/browser/editor-collection.service.ts b/packages/editor/src/browser/editor-collection.service.ts index 2370d7e09d..213c1f95d3 100644 --- a/packages/editor/src/browser/editor-collection.service.ts +++ b/packages/editor/src/browser/editor-collection.service.ts @@ -9,7 +9,6 @@ import { Emitter as EventEmitter, ILineChange, ISelection, - LRUCache, OnEvent, URI, WithEventBus, @@ -32,7 +31,7 @@ import { IUndoStopOptions, ResourceDecorationNeedChangeEvent, } from '../common'; -import { IEditorDocumentModel, IEditorDocumentModelRef } from '../common/editor'; +import { IEditorDocumentModel, IEditorDocumentModelRef, isTextEditorViewState } from '../common/editor'; import { MonacoEditorDecorationApplier } from './decoration-applier'; import { EditorDocumentModelContentChangedEvent, IEditorDocumentModelService } from './doc-model/types'; @@ -514,7 +513,7 @@ export class BrowserCodeEditor extends BaseMonacoEditorWrapper implements ICodeE protected restoreState() { if (this.currentUri) { const state = this.editorState.get(this.currentUri.toString()); - if (state) { + if (isTextEditorViewState(state)) { this.monacoEditor.restoreViewState(state); } } @@ -608,8 +607,6 @@ export class BrowserDiffEditor extends WithEventBus implements IDiffEditor { public onRefOpen = this._onRefOpen.event; - private diffEditorModelCache = new LRUCache(100); - protected saveCurrentState() { if (this.currentUri) { const state = this.monacoDiffEditor.saveViewState(); @@ -619,10 +616,13 @@ export class BrowserDiffEditor extends WithEventBus implements IDiffEditor { } } - protected restoreState() { + protected restoreState(options: IResourceOpenOptions) { if (this.currentUri) { const state = this.editorState.get(this.currentUri.toString()); - if (state) { + if (isTextEditorViewState(state)) { + if (options.range || options.originalRange) { + state.modified!.cursorState = []; // 避免重复的选中态 + } this.monacoDiffEditor.restoreViewState(state); } } @@ -641,13 +641,6 @@ export class BrowserDiffEditor extends WithEventBus implements IDiffEditor { ); } - disposeModel(originalUri: string, modifiedUri: string) { - if (this.diffEditorModelCache.size > 0) { - const key = `${originalUri}-${modifiedUri}`; - this.diffEditorModelCache.delete(key); - } - } - async compare( originalDocModelRef: IEditorDocumentModelRef, modifiedDocModelRef: IEditorDocumentModelRef, @@ -662,17 +655,7 @@ export class BrowserDiffEditor extends WithEventBus implements IDiffEditor { } const original = this.originalDocModel.getMonacoModel(); const modified = this.modifiedDocModel.getMonacoModel(); - const key = `${original.uri.toString()}-${modified.uri.toString()}`; - let model: monaco.editor.IDiffEditorViewModel | undefined; - if (this.appConfig.enableRestoreDiffEditorState) { - model = this.diffEditorModelCache.get(key); - } - - if (!model) { - model = this.monacoDiffEditor.createViewModel({ original, modified }); - this.diffEditorModelCache.set(key, model); - } - + const model = this.monacoDiffEditor.createViewModel({ original, modified }); this.monacoDiffEditor.setModel(model); if (rawUri) { @@ -687,11 +670,14 @@ export class BrowserDiffEditor extends WithEventBus implements IDiffEditor { }), }); } + await model?.waitForDiff(); + + // 需要等待 Diff 渲染,否则无法获取当前的 Diff 代码折叠状态 + this.restoreState(options); if (options.range || options.originalRange) { const range = (options.range || options.originalRange) as monaco.IRange; const currentEditor = options.range ? this.modifiedEditor.monacoEditor : this.originalEditor.monacoEditor; - await model?.waitForDiff(); // 必须使用 setTimeout, 因为两边的 editor 出现时机问题,diffEditor 是异步显示和渲染 setTimeout(() => { currentEditor.revealRangeInCenter(range); @@ -706,8 +692,6 @@ export class BrowserDiffEditor extends WithEventBus implements IDiffEditor { currentEditor.revealRangeInCenter(range); }); }); - } else { - this.restoreState(); } this._onRefOpen.fire(originalDocModelRef); this._onRefOpen.fire(modifiedDocModelRef); diff --git a/packages/editor/src/browser/workbench-editor.service.ts b/packages/editor/src/browser/workbench-editor.service.ts index 6e5c950695..8588545ad3 100644 --- a/packages/editor/src/browser/workbench-editor.service.ts +++ b/packages/editor/src/browser/workbench-editor.service.ts @@ -1568,7 +1568,6 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup { const query = uri.getParsedQuery(); this.doDisposeDocRef(new URI(query.original)); this.doDisposeDocRef(new URI(query.modified)); - this.diffEditor?.disposeModel(query.original, query.modified); } else if (uri.scheme === 'mergeEditor') { this.mergeEditor && this.mergeEditor.dispose(); } else { diff --git a/packages/editor/src/common/editor.ts b/packages/editor/src/common/editor.ts index fe23f2c604..39a7fc358d 100644 --- a/packages/editor/src/common/editor.ts +++ b/packages/editor/src/common/editor.ts @@ -22,6 +22,8 @@ import { IDocModelUpdateOptions } from './types'; import type { EOL, + ICodeEditorViewState, + IDiffEditorViewState, IEditorOptions, ICodeEditor as IMonacoCodeEditor, ITextModel, @@ -317,8 +319,6 @@ export interface IDiffEditor extends IDisposable { getLineChanges(): ILineChange[] | null; onRefOpen: Event; - - disposeModel(originalUri: string, modifiedUri: string): void; } @Injectable() @@ -989,3 +989,23 @@ export function getSimpleEditorOptions(): IEditorOptions { * in case the column does not exist yet. */ export type EditorGroupColumn = number; + +export function isTextEditorViewState(candidate: unknown): candidate is ICodeEditorViewState | IDiffEditorViewState { + const viewState = candidate as (ICodeEditorViewState | IDiffEditorViewState) | undefined; + if (!viewState) { + return false; + } + + const diffEditorViewState = viewState as IDiffEditorViewState; + if (diffEditorViewState.modified) { + return isTextEditorViewState(diffEditorViewState.modified); + } + + const codeEditorViewState = viewState as ICodeEditorViewState; + + return !!( + codeEditorViewState.contributionsState && + codeEditorViewState.viewState && + Array.isArray(codeEditorViewState.cursorState) + ); +}