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