122 lines
15 KiB
JavaScript
122 lines
15 KiB
JavaScript
|
"use strict";
|
||
|
/**
|
||
|
* @license
|
||
|
* Copyright Google LLC All Rights Reserved.
|
||
|
*
|
||
|
* Use of this source code is governed by an MIT-style license that can be
|
||
|
* found in the LICENSE file at https://angular.io/license
|
||
|
*/
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.applyToUpdateRecorder = exports.ReplaceChange = exports.RemoveChange = exports.InsertChange = exports.NoopChange = void 0;
|
||
|
/**
|
||
|
* An operation that does nothing.
|
||
|
*/
|
||
|
class NoopChange {
|
||
|
constructor() {
|
||
|
this.description = 'No operation.';
|
||
|
this.order = Infinity;
|
||
|
this.path = null;
|
||
|
}
|
||
|
apply() {
|
||
|
return Promise.resolve();
|
||
|
}
|
||
|
}
|
||
|
exports.NoopChange = NoopChange;
|
||
|
/**
|
||
|
* Will add text to the source code.
|
||
|
*/
|
||
|
class InsertChange {
|
||
|
constructor(path, pos, toAdd) {
|
||
|
this.path = path;
|
||
|
this.pos = pos;
|
||
|
this.toAdd = toAdd;
|
||
|
if (pos < 0) {
|
||
|
throw new Error('Negative positions are invalid');
|
||
|
}
|
||
|
this.description = `Inserted ${toAdd} into position ${pos} of ${path}`;
|
||
|
this.order = pos;
|
||
|
}
|
||
|
/**
|
||
|
* This method does not insert spaces if there is none in the original string.
|
||
|
*/
|
||
|
apply(host) {
|
||
|
return host.read(this.path).then((content) => {
|
||
|
const prefix = content.substring(0, this.pos);
|
||
|
const suffix = content.substring(this.pos);
|
||
|
return host.write(this.path, `${prefix}${this.toAdd}${suffix}`);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
exports.InsertChange = InsertChange;
|
||
|
/**
|
||
|
* Will remove text from the source code.
|
||
|
*/
|
||
|
class RemoveChange {
|
||
|
constructor(path, pos, toRemove) {
|
||
|
this.path = path;
|
||
|
this.pos = pos;
|
||
|
this.toRemove = toRemove;
|
||
|
if (pos < 0) {
|
||
|
throw new Error('Negative positions are invalid');
|
||
|
}
|
||
|
this.description = `Removed ${toRemove} into position ${pos} of ${path}`;
|
||
|
this.order = pos;
|
||
|
}
|
||
|
apply(host) {
|
||
|
return host.read(this.path).then((content) => {
|
||
|
const prefix = content.substring(0, this.pos);
|
||
|
const suffix = content.substring(this.pos + this.toRemove.length);
|
||
|
// TODO: throw error if toRemove doesn't match removed string.
|
||
|
return host.write(this.path, `${prefix}${suffix}`);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
exports.RemoveChange = RemoveChange;
|
||
|
/**
|
||
|
* Will replace text from the source code.
|
||
|
*/
|
||
|
class ReplaceChange {
|
||
|
constructor(path, pos, oldText, newText) {
|
||
|
this.path = path;
|
||
|
this.pos = pos;
|
||
|
this.oldText = oldText;
|
||
|
this.newText = newText;
|
||
|
if (pos < 0) {
|
||
|
throw new Error('Negative positions are invalid');
|
||
|
}
|
||
|
this.description = `Replaced ${oldText} into position ${pos} of ${path} with ${newText}`;
|
||
|
this.order = pos;
|
||
|
}
|
||
|
apply(host) {
|
||
|
return host.read(this.path).then((content) => {
|
||
|
const prefix = content.substring(0, this.pos);
|
||
|
const suffix = content.substring(this.pos + this.oldText.length);
|
||
|
const text = content.substring(this.pos, this.pos + this.oldText.length);
|
||
|
if (text !== this.oldText) {
|
||
|
return Promise.reject(new Error(`Invalid replace: "${text}" != "${this.oldText}".`));
|
||
|
}
|
||
|
// TODO: throw error if oldText doesn't match removed string.
|
||
|
return host.write(this.path, `${prefix}${this.newText}${suffix}`);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
exports.ReplaceChange = ReplaceChange;
|
||
|
function applyToUpdateRecorder(recorder, changes) {
|
||
|
for (const change of changes) {
|
||
|
if (change instanceof InsertChange) {
|
||
|
recorder.insertLeft(change.pos, change.toAdd);
|
||
|
}
|
||
|
else if (change instanceof RemoveChange) {
|
||
|
recorder.remove(change.order, change.toRemove.length);
|
||
|
}
|
||
|
else if (change instanceof ReplaceChange) {
|
||
|
recorder.remove(change.order, change.oldText.length);
|
||
|
recorder.insertLeft(change.order, change.newText);
|
||
|
}
|
||
|
else if (!(change instanceof NoopChange)) {
|
||
|
throw new Error('Unknown Change type encountered when updating a recorder.');
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
exports.applyToUpdateRecorder = applyToUpdateRecorder;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2hhbmdlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4vLi4vcGFja2FnZXMvc2NoZW1hdGljcy9hbmd1bGFyL3V0aWxpdHkvY2hhbmdlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQTs7Ozs7O0dBTUc7OztBQXdCSDs7R0FFRztBQUNILE1BQWEsVUFBVTtJQUF2QjtRQUNFLGdCQUFXLEdBQUcsZUFBZSxDQUFDO1FBQzlCLFVBQUssR0FBRyxRQUFRLENBQUM7UUFDakIsU0FBSSxHQUFHLElBQUksQ0FBQztJQUlkLENBQUM7SUFIQyxLQUFLO1FBQ0gsT0FBTyxPQUFPLENBQUMsT0FBTyxFQUFFLENBQUM7SUFDM0IsQ0FBQztDQUNGO0FBUEQsZ0NBT0M7QUFFRDs7R0FFRztBQUNILE1BQWEsWUFBWTtJQUl2QixZQUFtQixJQUFZLEVBQVMsR0FBVyxFQUFTLEtBQWE7UUFBdEQsU0FBSSxHQUFKLElBQUksQ0FBUTtRQUFTLFFBQUcsR0FBSCxHQUFHLENBQVE7UUFBUyxVQUFLLEdBQUwsS0FBSyxDQUFRO1FBQ3ZFLElBQUksR0FBRyxHQUFHLENBQUMsRUFBRTtZQUNYLE1BQU0sSUFBSSxLQUFLLENBQUMsZ0NBQWdDLENBQUMsQ0FBQztTQUNuRDtRQUNELElBQUksQ0FBQyxXQUFXLEdBQUcsWUFBWSxLQUFLLGtCQUFrQixHQUFHLE9BQU8sSUFBSSxFQUFFLENBQUM7UUFDdkUsS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