forked from iann0036/cloud9-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editManager.ts
254 lines (218 loc) · 8.91 KB
/
editManager.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import * as vscode from 'vscode';
import * as Utils from './utils';
class PendingEdit {
private action_list:string[];
constructor() {
this.action_list = [];
}
getActionListItemLength(i:number) {
if (this.action_list[i][0] == "r")
return parseInt(this.action_list[i].substring(1));
return (this.action_list[i].length - 1);
}
splitActionListItem(i:number, cutpoint:number) {
if (this.action_list[i][0] == "r") {
let new_item = "r" + (parseInt(this.action_list[i].substring(1)) - cutpoint);
this.action_list[i] = "r" + cutpoint;
this.action_list.splice(i+1, 0, new_item);
}
if (this.action_list[i][0] == "d") {
let new_item = "d" + (this.action_list[i].substring(1 + cutpoint));
this.action_list[i] = this.action_list[i].substring(0, cutpoint + 1);
this.action_list.splice(i+1, 0, new_item);
}
if (this.action_list[i][0] == "i") {
let new_item = "i" + (this.action_list[i].substring(1 + cutpoint));
this.action_list[i] = this.action_list[i].substring(0, cutpoint + 1);
this.action_list.splice(i+1, 0, new_item);
}
}
addEdit(r:number, d, i) {
console.log("Start Add Edit");
if (this.action_list.length == 0) {
if (r) {
this.action_list.push("r" + r);
}
if (d) {
this.action_list.push("d" + d);
}
if (i) {
this.action_list.push("i" + i);
}
} else {
let offset = 0;
let index = 0;
if (r) {
let array_split_done = false;
while (!array_split_done) {
if (index >= this.action_list.length) {
this.action_list.push("r" + (r - offset));
array_split_done = true;
} else if (this.getActionListItemLength(index) > (r - offset)) {
// split
this.splitActionListItem(index, (r - offset));
array_split_done = true;
} else {
offset += this.getActionListItemLength(index);
index += 1;
}
}
}
if (d) {
index += 1;
this.action_list.splice(index, 0, ("d" + d));
let remaining_delete_length = d.length;
let index_offset = 1;
while (remaining_delete_length > this.getActionListItemLength(index + index_offset)) {
this.action_list.splice(index + index_offset, 1);
remaining_delete_length -= this.getActionListItemLength(index + index_offset);
index_offset += 1;
}
if (remaining_delete_length > 0) {
if (this.action_list[index + index_offset][0] == 'r') {
this.action_list[index + index_offset] = "r" + (parseInt(this.action_list[index + index_offset].substring(1)) - remaining_delete_length);
} else { // TODO: how does 2x 'd' work here?
this.action_list[index + index_offset] = this.action_list[index + index_offset][0] + this.action_list[index + index_offset].substring(remaining_delete_length + 1);
}
}
}
if (i) {
index += 1;
this.action_list.splice(index, 0, ("i" + i));
}
}
let index = 0;
while (index < this.action_list.length) {
if (this.action_list[index] == "r0") {
this.action_list.splice(index, 1); // remove r0's
} else {
// TODO: check for double "i" or double "d"
index += 1;
}
}
}
getEditList(prev_doc) {
let ret = this.action_list;
let remaining_chars = prev_doc.length;
this.action_list.forEach(item => {
if (item[0] == "r") {
remaining_chars -= parseInt(item.substring(1));
} else if (item[0] == "d") {
remaining_chars -= (item.length - 1);
}
});
if (remaining_chars > 0) {
ret.push("r" + remaining_chars); // TODO: This is broken
} else if (remaining_chars < 0) {
console.warn("Remaining chars = " + remaining_chars);
}
return ret;
}
empty() {
this.action_list = [];
}
isEmpty() {
return this.action_list.length == 0;
}
}
export class EditManager {
private pending_edit;
private last_unacknowledged_edit;
public lastKnownRemoteDocumentText;
public lastKnownLocalDocumentText;
public revNum;
public vfsid;
public recent_edits;
private recent_edits_iterator;
private doc_path;
constructor(
private eventEmitter,
private websocketProvider
) {
this.pending_edit = new PendingEdit();
this.last_unacknowledged_edit = null;
this.lastKnownRemoteDocumentText = {};
this.lastKnownLocalDocumentText = {};
this.recent_edits = {};
this.recent_edits_iterator = 0;
eventEmitter.on('ack', (ack) => {
if (this.last_unacknowledged_edit != null) {
if (ack >= this.last_unacknowledged_edit) {
this.last_unacknowledged_edit = null;
this.sendPendingEdits();
}
}
});
}
sendPendingEdits() {
if (this.pending_edit.isEmpty()) {
return;
}
console.log("SENDING PENDING EDITS");
setTimeout(() => {
let prev_text = this.lastKnownRemoteDocumentText[this.doc_path];
let edit = this.pending_edit.getEditList(prev_text);
this.pending_edit.empty();
this.revNum += 1;
let doc = vscode.window.activeTextEditor.document; // TODO: Not compensating for multi-doc
let selection = vscode.window.activeTextEditor.selection;
let seq = this.websocketProvider.send_ch4_message(
["call","collab","send",[this.vfsid,{"type":"EDIT_UPDATE","data":{"docId":this.doc_path,"op":edit,"revNum":this.revNum,"selection":[
selection.start.line,
selection.start.character,
selection.end.line,
selection.end.character,
selection.isReversed
]}}]]
);
this.last_unacknowledged_edit = seq;
this.lastKnownRemoteDocumentText[this.doc_path] = this.lastKnownLocalDocumentText[this.doc_path];
}, 1); // TODO: Fix bad selection hack
}
queuePendingEdit(r, d, i) {
this.pending_edit.addEdit(parseInt(r), d, i);
}
addRemoteEdit(edit: vscode.WorkspaceEdit) {
this.recent_edits[this.recent_edits_iterator] = edit;
setTimeout((recent_edits_iterator) => {
delete this.recent_edits[recent_edits_iterator];
}, 100, this.recent_edits_iterator); // TODO: Check/optimize this timeout
this.recent_edits_iterator += 1;
}
processTextDocumentChange(change, evt) {
if (Object.keys(this.recent_edits).length === 0 && this.recent_edits.constructor === Object) {
; // no recent edits
} else {
for (var i in this.recent_edits) {
let text_edits = this.recent_edits[i];
text_edits.forEach(textedit => {
if (textedit.range.contains(evt.document.positionAt(change.rangeOffset))) {
console.log("RECENT EDIT DETECTED, IGNORING ONDIDCHANGE TRIGGER");
delete this.recent_edits[i];
return;
}
});
}
}
console.log("NON REMOTE EDIT");
let path = Utils.GetShortFilePath(evt.document);
this.doc_path = path; // TODO: Check for changes to this
let prevText = this.lastKnownLocalDocumentText[path];
if (prevText === undefined) {
console.warn("undefined lastKnownLocalDocumentText for: " + path);
console.log(this.lastKnownLocalDocumentText);
return;
}
this.lastKnownLocalDocumentText[path] = evt.document.getText();
let delText = prevText.substring(change.rangeOffset, change.rangeOffset + change.rangeLength);
this.queuePendingEdit(
(change.rangeOffset != 0 ? change.rangeOffset.toString() : null),
(change.rangeLength > 0 ? delText : null),
(change.text.length > 0 ? change.text : null)
);
console.log("Queued pending");
if (this.last_unacknowledged_edit == null) {
this.sendPendingEdits();
}
}
}