forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditStack.ts
More file actions
148 lines (118 loc) · 3.9 KB
/
editStack.ts
File metadata and controls
148 lines (118 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { onUnexpectedError } from 'vs/base/common/errors';
import { ICursorStateComputer, IEditableTextModel, IIdentifiedSingleEditOperation } from 'vs/editor/common/editorCommon';
import { Selection } from 'vs/editor/common/core/selection';
interface IEditOperation {
operations: IIdentifiedSingleEditOperation[];
}
interface IStackElement {
beforeVersionId: number;
beforeCursorState: Selection[];
editOperations: IEditOperation[];
afterCursorState: Selection[];
afterVersionId: number;
}
export interface IUndoRedoResult {
selections: Selection[];
recordedVersionId: number;
}
export class EditStack {
private model: IEditableTextModel;
private currentOpenStackElement: IStackElement;
private past: IStackElement[];
private future: IStackElement[];
constructor(model: IEditableTextModel) {
this.model = model;
this.currentOpenStackElement = null;
this.past = [];
this.future = [];
}
public pushStackElement(): void {
if (this.currentOpenStackElement !== null) {
this.past.push(this.currentOpenStackElement);
this.currentOpenStackElement = null;
}
}
public clear(): void {
this.currentOpenStackElement = null;
this.past = [];
this.future = [];
}
public pushEditOperation(beforeCursorState: Selection[], editOperations: IIdentifiedSingleEditOperation[], cursorStateComputer: ICursorStateComputer): Selection[] {
// No support for parallel universes :(
this.future = [];
if (!this.currentOpenStackElement) {
this.currentOpenStackElement = {
beforeVersionId: this.model.getAlternativeVersionId(),
beforeCursorState: beforeCursorState,
editOperations: [],
afterCursorState: null,
afterVersionId: -1
};
}
var inverseEditOperation: IEditOperation = {
operations: this.model.applyEdits(editOperations)
};
this.currentOpenStackElement.editOperations.push(inverseEditOperation);
try {
this.currentOpenStackElement.afterCursorState = cursorStateComputer ? cursorStateComputer(inverseEditOperation.operations) : null;
} catch (e) {
onUnexpectedError(e);
this.currentOpenStackElement.afterCursorState = null;
}
this.currentOpenStackElement.afterVersionId = this.model.getVersionId();
return this.currentOpenStackElement.afterCursorState;
}
public undo(): IUndoRedoResult {
this.pushStackElement();
if (this.past.length > 0) {
var pastStackElement = this.past.pop();
try {
// Apply all operations in reverse order
for (var i = pastStackElement.editOperations.length - 1; i >= 0; i--) {
pastStackElement.editOperations[i] = {
operations: this.model.applyEdits(pastStackElement.editOperations[i].operations)
};
}
} catch (e) {
this.clear();
return null;
}
this.future.push(pastStackElement);
return {
selections: pastStackElement.beforeCursorState,
recordedVersionId: pastStackElement.beforeVersionId
};
}
return null;
}
public redo(): IUndoRedoResult {
if (this.future.length > 0) {
if (this.currentOpenStackElement) {
throw new Error('How is this possible?');
}
var futureStackElement = this.future.pop();
try {
// Apply all operations
for (var i = 0; i < futureStackElement.editOperations.length; i++) {
futureStackElement.editOperations[i] = {
operations: this.model.applyEdits(futureStackElement.editOperations[i].operations)
};
}
} catch (e) {
this.clear();
return null;
}
this.past.push(futureStackElement);
return {
selections: futureStackElement.afterCursorState,
recordedVersionId: futureStackElement.afterVersionId
};
}
return null;
}
}