Skip to content

Commit

Permalink
Merge pull request #124996 from microsoft/hediet/isUndoRedo
Browse files Browse the repository at this point in the history
Forwards the isUndoing/isRedoing flags to the extension host.
  • Loading branch information
hediet authored Jul 7, 2021
2 parents b47d277 + b81aebe commit 556f74a
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/vs/editor/common/model/mirrorTextModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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 IMirrorTextModel {
Expand Down
14 changes: 14 additions & 0 deletions src/vs/vscode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10130,6 +10130,14 @@ declare module 'vscode' {
readonly text: string;
}

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 {@link TextDocument document} change.
*/
Expand All @@ -10144,6 +10152,12 @@ declare module 'vscode' {
* An array of content changes.
*/
readonly contentChanges: readonly TextDocumentContentChangeEvent[];

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

/**
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
TextEditorLineNumbersStyle: extHostTypes.TextEditorLineNumbersStyle,
TextEditorRevealType: extHostTypes.TextEditorRevealType,
TextEditorSelectionChangeKind: extHostTypes.TextEditorSelectionChangeKind,
TextDocumentChangeReason: extHostTypes.TextDocumentChangeReason,
ThemeColor: extHostTypes.ThemeColor,
ThemeIcon: extHostTypes.ThemeIcon,
TreeItem: extHostTypes.TreeItem,
Expand Down
15 changes: 13 additions & 2 deletions src/vs/workbench/api/common/extHostDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as TypeConverters from 'vs/workbench/api/common/extHostTypeConverters';
import type * as vscode from 'vscode';
import { assertIsDefined } from 'vs/base/common/types';
import { deepFreeze } from 'vs/base/common/objects';
import { TextDocumentChangeReason } from 'vs/workbench/api/common/extHostTypes';

export class ExtHostDocuments implements ExtHostDocumentsShape {

Expand Down Expand Up @@ -134,7 +135,8 @@ export class ExtHostDocuments implements ExtHostDocumentsShape {
data._acceptIsDirty(isDirty);
this._onDidChangeDocument.fire({
document: data.document,
contentChanges: []
contentChanges: [],
reason: undefined
});
}

Expand All @@ -146,6 +148,14 @@ export class ExtHostDocuments implements ExtHostDocumentsShape {
}
data._acceptIsDirty(isDirty);
data.onEvents(events);

let reason: vscode.TextDocumentChangeReason | undefined = undefined;
if (events.isUndoing) {
reason = TextDocumentChangeReason.Undo;
} else if (events.isRedoing) {
reason = TextDocumentChangeReason.Redo;
}

this._onDidChangeDocument.fire(deepFreeze({
document: data.document,
contentChanges: events.changes.map((change) => {
Expand All @@ -155,7 +165,8 @@ export class ExtHostDocuments implements ExtHostDocumentsShape {
rangeLength: change.rangeLength,
text: change.text
};
})
}),
reason
}));
}

Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/api/common/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,11 @@ export enum TextEditorSelectionChangeKind {
Command = 3
}

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

/**
* These values match very carefully the values of `TrackedRangeStickiness`
*/
Expand Down
14 changes: 13 additions & 1 deletion src/vs/workbench/test/browser/api/extHostDocumentData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ suite('ExtHostDocumentData', () => {
}],
eol: undefined!,
versionId: undefined!,
isRedoing: false,
isUndoing: false,
});

// line didn't change
Expand Down Expand Up @@ -161,6 +163,8 @@ suite('ExtHostDocumentData', () => {
}],
eol: undefined!,
versionId: undefined!,
isRedoing: false,
isUndoing: false,
});

assertOffsetAt(0, 1, 1);
Expand All @@ -179,6 +183,8 @@ suite('ExtHostDocumentData', () => {
}],
eol: undefined!,
versionId: undefined!,
isRedoing: false,
isUndoing: false,
});

assertOffsetAt(0, 1, 1);
Expand All @@ -197,6 +203,8 @@ suite('ExtHostDocumentData', () => {
}],
eol: undefined!,
versionId: undefined!,
isRedoing: false,
isUndoing: false,
});

assertOffsetAt(0, 1, 1);
Expand All @@ -218,6 +226,8 @@ suite('ExtHostDocumentData', () => {
}],
eol: undefined!,
versionId: undefined!,
isRedoing: false,
isUndoing: false,
});

assertOffsetAt(0, 1, 1);
Expand Down Expand Up @@ -398,6 +408,8 @@ suite('ExtHostDocumentData updates line mapping', () => {
}],
eol: eol!,
versionId: undefined!,
isRedoing: false,
isUndoing: false,
};
}

Expand All @@ -423,7 +435,7 @@ suite('ExtHostDocumentData updates line mapping', () => {
'and this is line number two',
'it is followed by #3',
'and finished with the fourth.',
], { changes: [], eol: undefined!, versionId: 7 });
], { changes: [], eol: undefined!, versionId: 7, isRedoing: false, isUndoing: false });
});

test('after remove', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ suite('ExtHostDocumentSaveParticipant', () => {
text: 'bar'
}],
eol: undefined!,
versionId: 2
versionId: 2,
isRedoing: false,
isUndoing: false,
}, true);

e.waitUntil(Promise.resolve([TextEdit.insert(new Position(0, 0), 'bar')]));
Expand Down Expand Up @@ -338,7 +340,9 @@ suite('ExtHostDocumentSaveParticipant', () => {
rangeLength: undefined!,
}],
eol: undefined!,
versionId: documents.getDocumentData(uri)!.version + 1
versionId: documents.getDocumentData(uri)!.version + 1,
isRedoing: false,
isUndoing: false,
}, true);
// }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ suite('NotebookConcatDocument', function () {
rangeLength: 6,
rangeOffset: 12,
text: 'Hi'
}]
}],
isRedoing: false,
isUndoing: false,
}, false);
assertLines(doc, 'Hello', 'World', 'Hi World!', 'Hallo', 'Welt', 'Hallo Welt!');
assertLocation(doc, new Position(2, 12), new Location(notebook.apiNotebook.cellAt(0).document.uri, new Position(2, 9)), false);
Expand Down

0 comments on commit 556f74a

Please sign in to comment.