1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var coffee_lex_1 = require("coffee-lex");
|
5 | var notNull_1 = require("../utils/notNull");
|
6 | var NodePatcher_1 = require("./NodePatcher");
|
7 | var SharedBlockPatcher = (function (_super) {
|
8 | tslib_1.__extends(SharedBlockPatcher, _super);
|
9 | function SharedBlockPatcher(patcherContext, statements) {
|
10 | var _this = _super.call(this, patcherContext) || this;
|
11 | _this.shouldPatchInline = null;
|
12 | _this.statements = statements;
|
13 | return _this;
|
14 | }
|
15 | |
16 |
|
17 |
|
18 | SharedBlockPatcher.prototype.insertStatementsAtIndex = function (statements, index) {
|
19 | var _this = this;
|
20 | var separator = this.inline() ? '; ' : ';\n';
|
21 | if (index === this.statements.length) {
|
22 | var lastStatement = this.statements[this.statements.length - 1];
|
23 | var terminatorTokenIndex = this.context.sourceTokens.indexOfTokenMatchingPredicate(function (token) { return token.type === coffee_lex_1.SourceType.NEWLINE || token.type === coffee_lex_1.SourceType.SEMICOLON; }, lastStatement.outerEndTokenIndex);
|
24 | var insertionPoint_1 = terminatorTokenIndex
|
25 | ? notNull_1.default(this.sourceTokenAtIndex(terminatorTokenIndex)).start
|
26 | : lastStatement.outerEnd;
|
27 | insertionPoint_1 = Math.min(insertionPoint_1, this.getBoundingPatcher().innerEnd);
|
28 | var indent_1 = lastStatement.getIndent();
|
29 | statements.forEach(function (line) {
|
30 | var sep = line.trim().startsWith('//') ? '\n' : separator;
|
31 | _this.insert(insertionPoint_1, "" + sep + indent_1 + line);
|
32 | });
|
33 | }
|
34 | else {
|
35 | var statementToInsertBefore = this.statements[index];
|
36 | var insertionPoint_2 = statementToInsertBefore.outerStart;
|
37 | var indent_2 = statementToInsertBefore.getIndent();
|
38 | statements.forEach(function (line) {
|
39 | var sep = line.trim().startsWith('//') ? '\n' : separator;
|
40 | _this.insert(insertionPoint_2, "" + line + sep + indent_2);
|
41 | });
|
42 | }
|
43 | };
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | SharedBlockPatcher.prototype.insertLineBefore = function (statement, indent) {
|
66 | if (indent === void 0) { indent = this.getIndent(); }
|
67 | if (this.inline()) {
|
68 | this.insert(this.outerStart, statement + "; ");
|
69 | }
|
70 | else if (this.node.inline) {
|
71 | this.insert(this.outerStart, "" + indent + statement + ";\n");
|
72 | }
|
73 | else {
|
74 | var insertIndex = this.outerStart;
|
75 | while (insertIndex > 0 && this.context.source[insertIndex] !== '\n') {
|
76 | insertIndex--;
|
77 | }
|
78 | this.insert(insertIndex, "\n" + indent + statement + ";");
|
79 | }
|
80 | };
|
81 | SharedBlockPatcher.prototype.insertLineAfter = function (statement, indent) {
|
82 | if (this.inline()) {
|
83 | this.insert(this.outerEnd, "; " + statement);
|
84 | }
|
85 | else {
|
86 | this.insert(this.outerEnd, "\n" + indent + statement + ";");
|
87 | }
|
88 | };
|
89 | |
90 |
|
91 |
|
92 |
|
93 | SharedBlockPatcher.prototype.inline = function () {
|
94 | if (this.shouldPatchInline !== null) {
|
95 | return this.shouldPatchInline;
|
96 | }
|
97 | return this.node.inline;
|
98 | };
|
99 | return SharedBlockPatcher;
|
100 | }(NodePatcher_1.default));
|
101 | exports.default = SharedBlockPatcher;
|