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: add textdocument change event reason property #80

Merged
merged 1 commit into from
Dec 6, 2021
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
10 changes: 6 additions & 4 deletions packages/editor/src/browser/doc-model/editor-document-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class EditorDocumentModel extends Disposable implements IEditorDocumentMo
});
}
this._previousVersionId = e.versionId;
this.notifyChangeEvent(e.changes);
this.notifyChangeEvent(e.changes, e.isRedoing, e.isUndoing);
});

this.addDispose(monacoModel);
Expand Down Expand Up @@ -215,7 +215,7 @@ export class EditorDocumentModel extends Disposable implements IEditorDocumentMo
(this.monacoModel as any)._commandManager.clear();
this._persistVersionId = this.monacoModel.getAlternativeVersionId();
this.savingTasks = [];
this.notifyChangeEvent();
this.notifyChangeEvent([], false, false);
this.baseContent = content;
}

Expand Down Expand Up @@ -366,7 +366,7 @@ export class EditorDocumentModel extends Disposable implements IEditorDocumentMo

setPersist(versionId: number) {
this._persistVersionId = versionId;
this.notifyChangeEvent();
this.notifyChangeEvent([], false, false);
}

async reload() {
Expand Down Expand Up @@ -451,7 +451,7 @@ export class EditorDocumentModel extends Disposable implements IEditorDocumentMo
return this._baseContentMd5!;
}

private notifyChangeEvent(changes: IEditorDocumentModelContentChange[] = []) {
private notifyChangeEvent(changes: IEditorDocumentModelContentChange[] = [], isRedoing: boolean, isUndoing: boolean) {
if (!this.closeAutoSave && this.savable && this.editorPreferences['editor.autoSave'] === 'afterDelay') {
this.tryAutoSaveAfterDelay();
}
Expand All @@ -461,6 +461,8 @@ export class EditorDocumentModel extends Disposable implements IEditorDocumentMo
dirty: this.dirty,
changes,
eol: this.eol,
isRedoing,
isUndoing,
versionId: this.monacoModel.getVersionId(),
}));

Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/browser/doc-model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ export interface IEditorDocumentModelContentChangedEventPayload {
changes: IEditorDocumentModelContentChange[];
eol: string;
versionId: number;
isRedoing: boolean;
isUndoing: boolean;
}

export class EditorDocumentModelOptionChangedEvent extends BasicEvent<IEditorDocumentModelOptionChangedEventPayload> {}
Expand Down
3 changes: 3 additions & 0 deletions packages/extension/src/browser/vscode/api/main.thread.doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,15 @@ export class MainThreadExtensionDocumentData extends WithEventBus implements IMa
if (!this.isDocSyncEnabled(e.payload.uri)) {
return;
}

this.proxy.$fireModelChangedEvent({
changes: e.payload.changes,
uri: e.payload.uri.toString(),
eol: e.payload.eol,
dirty: e.payload.dirty,
versionId: e.payload.versionId,
isRedoing: e.payload.isRedoing,
isUndoing: e.payload.isUndoing,
});
}

Expand Down
10 changes: 10 additions & 0 deletions packages/extension/src/common/vscode/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export interface IModelChangedEvent {
* The new version id the model has transitioned to.
*/
readonly versionId: number;
/**
* Flag that indicates that this event was generated while undoing.
*/
readonly isUndoing: boolean;
/**
* Flag that indicates that this event was generated while redoing.
*/
readonly isRedoing: boolean;
}

export interface IMainThreadDocumentsShape extends IDisposable {
Expand Down Expand Up @@ -51,6 +59,8 @@ export interface IExtensionDocumentModelChangedEvent {
versionId: number;
eol: string;
dirty: boolean;
isRedoing: boolean;
isUndoing: boolean;
}

export interface IExtensionDocumentModelOptionsChangedEvent {
Expand Down
5 changes: 5 additions & 0 deletions packages/extension/src/common/vscode/ext-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,11 @@ export enum TextDocumentSaveReason {
FocusOut = 3,
}

export enum TextDocumentChangeReason {
Undo = 1,
Redo = 2,
}

export { TextEditorRevealType } from './editor';

@es5ClassCompat
Expand Down
58 changes: 31 additions & 27 deletions packages/extension/src/hosted/api/vscode/doc/doc-manager.host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type vscode from 'vscode';

import { ExtensionDocumentDataManager, IExtensionDocumentModelChangedEvent, IExtensionDocumentModelOpenedEvent, IExtensionDocumentModelOptionsChangedEvent, IExtensionDocumentModelRemovedEvent, IExtensionDocumentModelSavedEvent, IExtensionDocumentModelWillSaveEvent, IMainThreadDocumentsShape, IMainThreadWorkspace, MainThreadAPIIdentifier } from '../../../../common/vscode';
import { TextEdit as TextEditConverter, toRange } from '../../../../common/vscode/converter';
import { TextEdit, Uri } from '../../../../common/vscode/ext-types';
import { TextDocumentChangeReason, TextEdit, Uri } from '../../../../common/vscode/ext-types';
import type * as model from '../../../../common/vscode/model.api';
import { ExtHostDocumentData, setWordDefinitionFor } from './ext-data.host';
import { BinaryBuffer } from '@opensumi/ide-core-common/lib/utils/buffer';
Expand All @@ -16,7 +16,6 @@ export class ExtensionDocumentDataManagerImpl implements ExtensionDocumentDataMa
private readonly rpcProtocol: IRPCProtocol;
private readonly _proxy: IMainThreadDocumentsShape;
private readonly _workspaceProxy: IMainThreadWorkspace;
private readonly _logService: any;

private _documents: Map<string, ExtHostDocumentData> = new Map();
private _contentProviders: Map<string, vscode.TextDocumentContentProvider> = new Map();
Expand All @@ -37,12 +36,6 @@ export class ExtensionDocumentDataManagerImpl implements ExtensionDocumentDataMa
this.rpcProtocol = rpcProtocol;
this._proxy = this.rpcProtocol.getProxy(MainThreadAPIIdentifier.MainThreadDocuments);
this._workspaceProxy = this.rpcProtocol.getProxy(MainThreadAPIIdentifier.MainThreadWorkspace);
this._logService = {
trace() {
// tslint:disable-next-line:no-console
console.log.apply(console, arguments as any);
},
};
}

get allDocumentData() {
Expand Down Expand Up @@ -166,27 +159,38 @@ export class ExtensionDocumentDataManagerImpl implements ExtensionDocumentDataMa
}

$fireModelChangedEvent(e: IExtensionDocumentModelChangedEvent) {
const { uri, changes, versionId, eol, dirty } = e;
const { uri, changes, versionId, eol, dirty, isRedoing, isUndoing } = e;
const document = this._documents.get(uri);
if (document) {
document.onEvents({
eol,
versionId,
changes,
});
document._acceptIsDirty(dirty);
this._onDidChangeTextDocument.fire({
document: document.document,
contentChanges: changes.map((change) => {
return {
...change,
range: toRange(change.range) as any,
};
}),
});
} else {
// TODO: 增加消息后台未接受到的情况
if (!document) {
return;
}
document.onEvents({
eol,
versionId,
changes,
isRedoing,
isUndoing,
});
document._acceptIsDirty(dirty);

let reason: vscode.TextDocumentChangeReason | undefined;

if (isRedoing) {
reason = TextDocumentChangeReason.Redo;
} else if (isUndoing) {
reason = TextDocumentChangeReason.Undo;
}

this._onDidChangeTextDocument.fire({
document: document.document,
contentChanges: changes.map((change) => {
return {
...change,
range: toRange(change.range) as any,
};
}),
reason,
});
}

$fireModelOpenedEvent(e: IExtensionDocumentModelOpenedEvent) {
Expand Down
14 changes: 14 additions & 0 deletions packages/types/sumi-worker/sumi-worker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,14 @@ declare module 'sumi-worker' {
dispose(): void;
}

export enum TextDocumentChangeReason {
/** The text change is caused by an undo operation. */
Undo = 1,

/** The text change is caused by an redo operation. */
Redo = 2,
}

/**
* An event describing a transactional [document](#TextDocument) change.
*/
Expand All @@ -1693,6 +1701,12 @@ declare module 'sumi-worker' {
* An array of content changes.
*/
readonly contentChanges: ReadonlyArray<TextDocumentContentChangeEvent>;

/**
* The reason why the document was changed.
* Is `undefined` if the reason is not known.
*/
readonly reason: TextDocumentChangeReason | undefined;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions packages/types/vscode/typings/vscode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,14 @@ declare module 'vscode' {
dispose(): void;
}

export enum TextDocumentChangeReason {
/** The text change is caused by an undo operation. */
Undo = 1,

/** The text change is caused by an redo operation. */
Redo = 2,
}

/**
* An event describing a transactional [document](#TextDocument) change.
*/
Expand All @@ -2211,6 +2219,12 @@ declare module 'vscode' {
* An array of content changes.
*/
readonly contentChanges: ReadonlyArray<TextDocumentContentChangeEvent>;

/**
* The reason why the document was changed.
* Is `undefined` if the reason is not known.
*/
readonly reason: TextDocumentChangeReason | undefined;
}

/**
Expand Down