Skip to content

Commit

Permalink
fix: unnecessary editor update options (#2133)
Browse files Browse the repository at this point in the history
* fix: unnecessary editor update options

* fix: unnecessary editor update options

* fix: test case
  • Loading branch information
Aaaaash authored Dec 30, 2022
1 parent 228a283 commit 949a000
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
6 changes: 5 additions & 1 deletion packages/editor/__tests__/browser/editor.collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ describe('editor collection service test', () => {

open(new URI('file:///test/test.js'));

setPref('editor.fontSize', 20);
setPref('editor.readOnly', false);

expect(options['fontSize']).toBe(20);

testEditor.updateOptions({ fontSize: 40 });
Expand All @@ -158,7 +161,7 @@ describe('editor collection service test', () => {

// 切换后仍然有这个option
expect(options['fontSize']).toBe(40);

setPref('editor.readOnly', true);
expect(options['readOnly']).toBeTruthy();

testEditor.updateOptions({ fontSize: undefined });
Expand All @@ -170,6 +173,7 @@ describe('editor collection service test', () => {
expect(options['fontSize']).toBe(35);

open(new URI('file:///test/test3.js'));
setPref('editor.readOnly', false);
expect(options['readOnly']).toBeFalsy();

setPref('editor.forceReadOnly', true);
Expand Down
41 changes: 22 additions & 19 deletions packages/editor/src/browser/editor-collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ISelection,
Disposable,
objects,
isEmptyObject,
} from '@opensumi/ide-core-common';
import { Emitter } from '@opensumi/ide-core-common';
import type {
Expand Down Expand Up @@ -269,24 +270,7 @@ export abstract class BaseMonacoEditorWrapper extends Disposable implements IEdi
constructor(public readonly monacoEditor: IMonacoCodeEditor, private type: EditorType) {
super();
this.decorationApplier = this.injector.get(MonacoEditorDecorationApplier, [this.monacoEditor]);
this.addDispose(
this.monacoEditor.onDidChangeModel(() => {
this._editorOptionsFromContribution = {};
const uri = this.currentUri;
if (uri) {
Promise.resolve(this.editorFeatureRegistry.runProvideEditorOptionsForUri(uri)).then((option) => {
if (!this.currentUri || !uri.isEqual(this.currentUri)) {
return; // uri可能已经变了
}
if (option && Object.keys(option).length > 0) {
this._editorOptionsFromContribution = option;
this._doUpdateOptions();
}
});
}
this._doUpdateOptions();
}),
);
this.addDispose(this.monacoEditor.onDidChangeModel(this.onDidChangeModel.bind(this)));
this.addDispose(
this.monacoEditor.onDidChangeModelLanguage(() => {
this._doUpdateOptions();
Expand All @@ -302,6 +286,25 @@ export abstract class BaseMonacoEditorWrapper extends Disposable implements IEdi
);
}

private async onDidChangeModel() {
this._editorOptionsFromContribution = {};
const uri = this.currentUri;
if (uri) {
Promise.resolve(this.editorFeatureRegistry.runProvideEditorOptionsForUri(uri)).then((options) => {
if (!this.currentUri || !uri.isEqual(this.currentUri)) {
return; // uri可能已经变了
}

if (options && Object.keys(options).length > 0) {
this._editorOptionsFromContribution = options;
if (!isEmptyObject(this._editorOptionsFromContribution)) {
this._doUpdateOptions();
}
}
});
}
}

public getType() {
return this.type;
}
Expand Down Expand Up @@ -498,7 +501,7 @@ export class BrowserCodeEditor extends BaseMonacoEditorWrapper implements ICodeE
}
}

async open(documentModelRef: IEditorDocumentModelRef): Promise<void> {
open(documentModelRef: IEditorDocumentModelRef): void {
this.saveCurrentState();
this._currentDocumentModelRef = documentModelRef;
const model = this.currentDocumentModel!.getMonacoModel();
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/common/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export interface ICodeEditor extends IEditor, IDisposable {
* 打开一个 document
* @param uri
*/
open(documentModelRef: IEditorDocumentModelRef, range?: IRange): Promise<void>;
open(documentModelRef: IEditorDocumentModelRef, range?: IRange): void;

focus(): void;

Expand Down

0 comments on commit 949a000

Please sign in to comment.