1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.addImports = addImports;
7exports.appendContentsInsideDeclarationBlock = appendContentsInsideDeclarationBlock;
8exports.findNewInstanceCodeBlock = findNewInstanceCodeBlock;
9
10function _commonCodeMod() {
11  const data = require("../utils/commonCodeMod");
12
13  _commonCodeMod = function () {
14    return data;
15  };
16
17  return data;
18}
19
20function _matchBrackets() {
21  const data = require("../utils/matchBrackets");
22
23  _matchBrackets = function () {
24    return data;
25  };
26
27  return data;
28}
29
30/**
31 * Find java or kotlin new class instance code block
32 *
33 * @param contents source contents
34 * @param classDeclaration class declaration or just a class name
35 * @param language 'java' | 'kt'
36 * @returns `CodeBlock` for start/end offset and code block contents
37 */
38function findNewInstanceCodeBlock(contents, classDeclaration, language) {
39  const isJava = language === 'java';
40  let start = isJava ? contents.indexOf(` new ${classDeclaration}(`) : contents.search(new RegExp(` (object\\s*:\\s*)?${classDeclaration}\\(`));
41
42  if (start < 0) {
43    return null;
44  } // `+ 1` for the prefix space
45
46
47  start += 1;
48  let end = (0, _matchBrackets().findMatchingBracketPosition)(contents, '(', start); // For anonymous class, should search further to the {} block.
49  // ```java
50  // new Foo() {
51  //   @Override
52  //   protected void interfaceMethod {}
53  // };
54  // ```
55  //
56  // ```kotlin
57  // object : Foo() {
58  //   override fun interfaceMethod {}
59  // }
60  // ```
61
62  const nextBrace = contents.indexOf('{', end + 1);
63  const isAnonymousClass = nextBrace >= end && !!contents.substring(end + 1, nextBrace).match(/^\s*$/);
64
65  if (isAnonymousClass) {
66    end = (0, _matchBrackets().findMatchingBracketPosition)(contents, '{', end);
67  }
68
69  return {
70    start,
71    end,
72    code: contents.substring(start, end + 1)
73  };
74}
75/**
76 * Append contents to the end of code declaration block, support class or method declarations.
77 *
78 * @param srcContents source contents
79 * @param declaration class declaration or method declaration
80 * @param insertion code to append
81 * @returns updated contents
82 */
83
84
85function appendContentsInsideDeclarationBlock(srcContents, declaration, insertion) {
86  const start = srcContents.search(new RegExp(`\\s*${declaration}.*?[\\(\\{]`));
87
88  if (start < 0) {
89    throw new Error(`Unable to find code block - declaration[${declaration}]`);
90  }
91
92  const end = (0, _matchBrackets().findMatchingBracketPosition)(srcContents, '{', start);
93  return (0, _commonCodeMod().insertContentsAtOffset)(srcContents, insertion, end);
94}
95
96function addImports(source, imports, isJava) {
97  const lines = source.split('\n');
98  const lineIndexWithPackageDeclaration = lines.findIndex(line => line.match(/^package .*;?$/));
99
100  for (const javaImport of imports) {
101    if (!source.includes(javaImport)) {
102      const importStatement = `import ${javaImport}${isJava ? ';' : ''}`;
103      lines.splice(lineIndexWithPackageDeclaration + 1, 0, importStatement);
104    }
105  }
106
107  return lines.join('\n');
108}
109//# sourceMappingURL=codeMod.js.map