-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathExecuteTextEdits.rsc
More file actions
58 lines (44 loc) · 1.61 KB
/
ExecuteTextEdits.rsc
File metadata and controls
58 lines (44 loc) · 1.61 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
module analysis::diff::edits::ExecuteTextEdits
extend analysis::diff::edits::TextEdits;
import DateTime;
import IO;
import List;
import String;
@synopsis{Execute file changes, including in-file edits if present.}
@deprecated{Replaced by ((executeFileSystemChanges)) due to a rename of the concept.}
void executeDocumentEdits(list[FileSystemChange] edits) {
executeFileSystemChanges(edits);
}
@synopsis{Execute file changes, including in-file edits if present.}
void executeFileSystemChanges(list[FileSystemChange] edits) {
for (e <- edits) {
executeFileSystemChange(e);
}
}
void executeFileSystemChange(removed(loc f)) {
remove(f.top);
}
void executeFileSystemChange(created(loc f)) {
writeFile(f, "");
}
void executeFileSystemChange(renamed(loc from, loc to)) {
move(from.top, to.top, overwrite=true);
}
void executeFileSystemChange(changed(loc file)) {
setLastModified(file, now());
}
@synopsis{Edit a file according to the given ((TextEdit)) instructions}
void executeFileSystemChange(changed(loc file, list[TextEdit] edits)) {
str content = readFile(file);
content = executeTextEdits(content, edits);
writeFile(file.top, content);
}
str executeTextEdits(str content, list[TextEdit] edits) {
// assert isSorted(edits, less=bool (TextEdit e1, TextEdit e2) {
// return e1.range.offset < e2.range.offset;
// });
int cursor = 0;
// linear-time streamed reconstruction of the entire text
return "<for (replace(loc range, str repl) <- edits) {><content[cursor..range.offset]><repl><
cursor = range.offset + range.length;}><content[cursor..]>";
}