UNPKG

6.15 kBJavaScriptView Raw
1"use strict";
2var __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};
13exports.__esModule = true;
14exports.commitChanges = exports.createChangeRecorder = exports.createRemoveChange = exports.createReplaceChange = exports.ReplaceChange = exports.RemoveChange = exports.InsertChange = exports.NoopChange = void 0;
15/**
16 * An operation that does nothing.
17 */
18var NoopChange = /** @class */ (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}());
29exports.NoopChange = NoopChange;
30/**
31 * Will add text to the source code.
32 */
33var InsertChange = /** @class */ (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 " + toAdd + " into position " + pos + " of " + path;
42 this.order = pos;
43 }
44 /**
45 * This method does not insert spaces if there is none in the original string.
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, "" + prefix + _this.toAdd + suffix);
53 });
54 };
55 return InsertChange;
56}());
57exports.InsertChange = InsertChange;
58/**
59 * Will remove text from the source code.
60 */
61var RemoveChange = /** @class */ (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 " + pos + " to " + end + " of " + 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 // TODO: throw error if toRemove doesn't match removed string.
78 return host.write(_this.path, "" + prefix + suffix);
79 });
80 };
81 return RemoveChange;
82}());
83exports.RemoveChange = RemoveChange;
84/**
85 * Will replace text from the source code.
86 */
87var ReplaceChange = /** @class */ (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 " + oldText + " into position " + pos + " of " + path + " with " + 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: \"" + text + "\" != \"" + _this.oldText + "\"."));
107 }
108 // TODO: throw error if oldText doesn't match removed string.
109 return host.write(_this.path, "" + prefix + _this.newText + suffix);
110 });
111 };
112 return ReplaceChange;
113}());
114exports.ReplaceChange = ReplaceChange;
115function createReplaceChange(sourceFile, node, oldText, newText) {
116 return new ReplaceChange(sourceFile.fileName, node.getStart(sourceFile), oldText, newText);
117}
118exports.createReplaceChange = createReplaceChange;
119function 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}
124exports.createRemoveChange = createRemoveChange;
125function 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}
152exports.createChangeRecorder = createChangeRecorder;
153function 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}
161exports.commitChanges = commitChanges;
162//# sourceMappingURL=change.js.map
\No newline at end of file