[6a3a178] | 1 | "use strict";
|
---|
| 2 | /**
|
---|
| 3 | * @license
|
---|
| 4 | * Copyright Google LLC All Rights Reserved.
|
---|
| 5 | *
|
---|
| 6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 7 | * found in the LICENSE file at https://angular.io/license
|
---|
| 8 | */
|
---|
| 9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 10 | exports.applyToUpdateRecorder = exports.ReplaceChange = exports.RemoveChange = exports.InsertChange = exports.NoopChange = void 0;
|
---|
| 11 | /**
|
---|
| 12 | * An operation that does nothing.
|
---|
| 13 | */
|
---|
| 14 | class NoopChange {
|
---|
| 15 | constructor() {
|
---|
| 16 | this.description = 'No operation.';
|
---|
| 17 | this.order = Infinity;
|
---|
| 18 | this.path = null;
|
---|
| 19 | }
|
---|
| 20 | apply() {
|
---|
| 21 | return Promise.resolve();
|
---|
| 22 | }
|
---|
| 23 | }
|
---|
| 24 | exports.NoopChange = NoopChange;
|
---|
| 25 | /**
|
---|
| 26 | * Will add text to the source code.
|
---|
| 27 | */
|
---|
| 28 | class InsertChange {
|
---|
| 29 | constructor(path, pos, toAdd) {
|
---|
| 30 | this.path = path;
|
---|
| 31 | this.pos = pos;
|
---|
| 32 | this.toAdd = toAdd;
|
---|
| 33 | if (pos < 0) {
|
---|
| 34 | throw new Error('Negative positions are invalid');
|
---|
| 35 | }
|
---|
| 36 | this.description = `Inserted ${toAdd} into position ${pos} of ${path}`;
|
---|
| 37 | this.order = pos;
|
---|
| 38 | }
|
---|
| 39 | /**
|
---|
| 40 | * This method does not insert spaces if there is none in the original string.
|
---|
| 41 | */
|
---|
| 42 | apply(host) {
|
---|
| 43 | return host.read(this.path).then((content) => {
|
---|
| 44 | const prefix = content.substring(0, this.pos);
|
---|
| 45 | const suffix = content.substring(this.pos);
|
---|
| 46 | return host.write(this.path, `${prefix}${this.toAdd}${suffix}`);
|
---|
| 47 | });
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | exports.InsertChange = InsertChange;
|
---|
| 51 | /**
|
---|
| 52 | * Will remove text from the source code.
|
---|
| 53 | */
|
---|
| 54 | class RemoveChange {
|
---|
| 55 | constructor(path, pos, toRemove) {
|
---|
| 56 | this.path = path;
|
---|
| 57 | this.pos = pos;
|
---|
| 58 | this.toRemove = toRemove;
|
---|
| 59 | if (pos < 0) {
|
---|
| 60 | throw new Error('Negative positions are invalid');
|
---|
| 61 | }
|
---|
| 62 | this.description = `Removed ${toRemove} into position ${pos} of ${path}`;
|
---|
| 63 | this.order = pos;
|
---|
| 64 | }
|
---|
| 65 | apply(host) {
|
---|
| 66 | return host.read(this.path).then((content) => {
|
---|
| 67 | const prefix = content.substring(0, this.pos);
|
---|
| 68 | const suffix = content.substring(this.pos + this.toRemove.length);
|
---|
| 69 | // TODO: throw error if toRemove doesn't match removed string.
|
---|
| 70 | return host.write(this.path, `${prefix}${suffix}`);
|
---|
| 71 | });
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | exports.RemoveChange = RemoveChange;
|
---|
| 75 | /**
|
---|
| 76 | * Will replace text from the source code.
|
---|
| 77 | */
|
---|
| 78 | class ReplaceChange {
|
---|
| 79 | constructor(path, pos, oldText, newText) {
|
---|
| 80 | this.path = path;
|
---|
| 81 | this.pos = pos;
|
---|
| 82 | this.oldText = oldText;
|
---|
| 83 | this.newText = newText;
|
---|
| 84 | if (pos < 0) {
|
---|
| 85 | throw new Error('Negative positions are invalid');
|
---|
| 86 | }
|
---|
| 87 | this.description = `Replaced ${oldText} into position ${pos} of ${path} with ${newText}`;
|
---|
| 88 | this.order = pos;
|
---|
| 89 | }
|
---|
| 90 | apply(host) {
|
---|
| 91 | return host.read(this.path).then((content) => {
|
---|
| 92 | const prefix = content.substring(0, this.pos);
|
---|
| 93 | const suffix = content.substring(this.pos + this.oldText.length);
|
---|
| 94 | const text = content.substring(this.pos, this.pos + this.oldText.length);
|
---|
| 95 | if (text !== this.oldText) {
|
---|
| 96 | return Promise.reject(new Error(`Invalid replace: "${text}" != "${this.oldText}".`));
|
---|
| 97 | }
|
---|
| 98 | // TODO: throw error if oldText doesn't match removed string.
|
---|
| 99 | return host.write(this.path, `${prefix}${this.newText}${suffix}`);
|
---|
| 100 | });
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | exports.ReplaceChange = ReplaceChange;
|
---|
| 104 | function applyToUpdateRecorder(recorder, changes) {
|
---|
| 105 | for (const change of changes) {
|
---|
| 106 | if (change instanceof InsertChange) {
|
---|
| 107 | recorder.insertLeft(change.pos, change.toAdd);
|
---|
| 108 | }
|
---|
| 109 | else if (change instanceof RemoveChange) {
|
---|
| 110 | recorder.remove(change.order, change.toRemove.length);
|
---|
| 111 | }
|
---|
| 112 | else if (change instanceof ReplaceChange) {
|
---|
| 113 | recorder.remove(change.order, change.oldText.length);
|
---|
| 114 | recorder.insertLeft(change.order, change.newText);
|
---|
| 115 | }
|
---|
| 116 | else if (!(change instanceof NoopChange)) {
|
---|
| 117 | throw new Error('Unknown Change type encountered when updating a recorder.');
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | exports.applyToUpdateRecorder = applyToUpdateRecorder;
|
---|