Skip to content

Commit

Permalink
fix: diff editor model not exist error (#4109)
Browse files Browse the repository at this point in the history
  • Loading branch information
erha19 authored Oct 22, 2024
1 parent 06b3924 commit 9fbc930
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
4 changes: 0 additions & 4 deletions packages/core-browser/src/react-providers/config-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
40 changes: 12 additions & 28 deletions packages/editor/src/browser/editor-collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
Emitter as EventEmitter,
ILineChange,
ISelection,
LRUCache,
OnEvent,
URI,
WithEventBus,
Expand All @@ -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';
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -608,8 +607,6 @@ export class BrowserDiffEditor extends WithEventBus implements IDiffEditor {

public onRefOpen = this._onRefOpen.event;

private diffEditorModelCache = new LRUCache<string, monaco.editor.IDiffEditorViewModel>(100);

protected saveCurrentState() {
if (this.currentUri) {
const state = this.monacoDiffEditor.saveViewState();
Expand All @@ -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);
}
}
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/browser/workbench-editor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 22 additions & 2 deletions packages/editor/src/common/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { IDocModelUpdateOptions } from './types';

import type {
EOL,
ICodeEditorViewState,
IDiffEditorViewState,
IEditorOptions,
ICodeEditor as IMonacoCodeEditor,
ITextModel,
Expand Down Expand Up @@ -317,8 +319,6 @@ export interface IDiffEditor extends IDisposable {
getLineChanges(): ILineChange[] | null;

onRefOpen: Event<IEditorDocumentModelRef>;

disposeModel(originalUri: string, modifiedUri: string): void;
}

@Injectable()
Expand Down Expand Up @@ -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)
);
}

1 comment on commit 9fbc930

@opensumi
Copy link
Contributor

@opensumi opensumi bot commented on 9fbc930 Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Next publish successful!

3.4.5-next-1729565551.0

Please sign in to comment.