1 | "use strict";
|
2 | var __values = (this && this.__values) || function(o) {
|
3 | var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
4 | if (m) return m.call(o);
|
5 | if (o && typeof o.length === "number") return {
|
6 | next: function () {
|
7 | if (o && i >= o.length) o = void 0;
|
8 | return { value: o && o[i++], done: !o };
|
9 | }
|
10 | };
|
11 | throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | exports.commitChanges = exports.createChangeRecorder = exports.createRemoveChange = exports.createReplaceChange = exports.ReplaceChange = exports.RemoveChange = exports.InsertChange = exports.NoopChange = void 0;
|
15 |
|
16 |
|
17 |
|
18 | var NoopChange = (function () {
|
19 | function NoopChange() {
|
20 | this.description = 'No operation.';
|
21 | this.order = Infinity;
|
22 | this.path = null;
|
23 | }
|
24 | NoopChange.prototype.apply = function () {
|
25 | return Promise.resolve();
|
26 | };
|
27 | return NoopChange;
|
28 | }());
|
29 | exports.NoopChange = NoopChange;
|
30 |
|
31 |
|
32 |
|
33 | var InsertChange = (function () {
|
34 | function InsertChange(path, pos, toAdd) {
|
35 | this.path = path;
|
36 | this.pos = pos;
|
37 | this.toAdd = toAdd;
|
38 | if (pos < 0) {
|
39 | throw new Error('Negative positions are invalid');
|
40 | }
|
41 | this.description = "Inserted ".concat(toAdd, " into position ").concat(pos, " of ").concat(path);
|
42 | this.order = pos;
|
43 | }
|
44 | |
45 |
|
46 |
|
47 | InsertChange.prototype.apply = function (host) {
|
48 | var _this = this;
|
49 | return host.read(this.path).then(function (content) {
|
50 | var prefix = content.substring(0, _this.pos);
|
51 | var suffix = content.substring(_this.pos);
|
52 | return host.write(_this.path, "".concat(prefix).concat(_this.toAdd).concat(suffix));
|
53 | });
|
54 | };
|
55 | return InsertChange;
|
56 | }());
|
57 | exports.InsertChange = InsertChange;
|
58 |
|
59 |
|
60 |
|
61 | var RemoveChange = (function () {
|
62 | function RemoveChange(path, pos, end) {
|
63 | this.path = path;
|
64 | this.pos = pos;
|
65 | this.end = end;
|
66 | if (pos < 0 || end < 0) {
|
67 | throw new Error('Negative positions are invalid');
|
68 | }
|
69 | this.description = "Removed text in position ".concat(pos, " to ").concat(end, " of ").concat(path);
|
70 | this.order = pos;
|
71 | }
|
72 | RemoveChange.prototype.apply = function (host) {
|
73 | var _this = this;
|
74 | return host.read(this.path).then(function (content) {
|
75 | var prefix = content.substring(0, _this.pos);
|
76 | var suffix = content.substring(_this.end);
|
77 |
|
78 | return host.write(_this.path, "".concat(prefix).concat(suffix));
|
79 | });
|
80 | };
|
81 | return RemoveChange;
|
82 | }());
|
83 | exports.RemoveChange = RemoveChange;
|
84 |
|
85 |
|
86 |
|
87 | var ReplaceChange = (function () {
|
88 | function ReplaceChange(path, pos, oldText, newText) {
|
89 | this.path = path;
|
90 | this.pos = pos;
|
91 | this.oldText = oldText;
|
92 | this.newText = newText;
|
93 | if (pos < 0) {
|
94 | throw new Error('Negative positions are invalid');
|
95 | }
|
96 | this.description = "Replaced ".concat(oldText, " into position ").concat(pos, " of ").concat(path, " with ").concat(newText);
|
97 | this.order = pos;
|
98 | }
|
99 | ReplaceChange.prototype.apply = function (host) {
|
100 | var _this = this;
|
101 | return host.read(this.path).then(function (content) {
|
102 | var prefix = content.substring(0, _this.pos);
|
103 | var suffix = content.substring(_this.pos + _this.oldText.length);
|
104 | var text = content.substring(_this.pos, _this.pos + _this.oldText.length);
|
105 | if (text !== _this.oldText) {
|
106 | return Promise.reject(new Error("Invalid replace: \"".concat(text, "\" != \"").concat(_this.oldText, "\".")));
|
107 | }
|
108 |
|
109 | return host.write(_this.path, "".concat(prefix).concat(_this.newText).concat(suffix));
|
110 | });
|
111 | };
|
112 | return ReplaceChange;
|
113 | }());
|
114 | exports.ReplaceChange = ReplaceChange;
|
115 | function createReplaceChange(sourceFile, node, oldText, newText) {
|
116 | return new ReplaceChange(sourceFile.fileName, node.getStart(sourceFile), oldText, newText);
|
117 | }
|
118 | exports.createReplaceChange = createReplaceChange;
|
119 | function createRemoveChange(sourceFile, node, from, to) {
|
120 | if (from === void 0) { from = node.getStart(sourceFile); }
|
121 | if (to === void 0) { to = node.getEnd(); }
|
122 | return new RemoveChange(sourceFile.fileName, from, to);
|
123 | }
|
124 | exports.createRemoveChange = createRemoveChange;
|
125 | function createChangeRecorder(tree, path, changes) {
|
126 | var e_1, _a;
|
127 | var recorder = tree.beginUpdate(path);
|
128 | try {
|
129 | for (var changes_1 = __values(changes), changes_1_1 = changes_1.next(); !changes_1_1.done; changes_1_1 = changes_1.next()) {
|
130 | var change = changes_1_1.value;
|
131 | if (change instanceof InsertChange) {
|
132 | recorder.insertLeft(change.pos, change.toAdd);
|
133 | }
|
134 | else if (change instanceof RemoveChange) {
|
135 | recorder.remove(change.pos, change.end - change.pos);
|
136 | }
|
137 | else if (change instanceof ReplaceChange) {
|
138 | recorder.remove(change.pos, change.oldText.length);
|
139 | recorder.insertLeft(change.pos, change.newText);
|
140 | }
|
141 | }
|
142 | }
|
143 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
144 | finally {
|
145 | try {
|
146 | if (changes_1_1 && !changes_1_1.done && (_a = changes_1.return)) _a.call(changes_1);
|
147 | }
|
148 | finally { if (e_1) throw e_1.error; }
|
149 | }
|
150 | return recorder;
|
151 | }
|
152 | exports.createChangeRecorder = createChangeRecorder;
|
153 | function commitChanges(tree, path, changes) {
|
154 | if (changes.length === 0) {
|
155 | return false;
|
156 | }
|
157 | var recorder = createChangeRecorder(tree, path, changes);
|
158 | tree.commitUpdate(recorder);
|
159 | return true;
|
160 | }
|
161 | exports.commitChanges = commitChanges;
|
162 |
|
\ | No newline at end of file |