* Search for a string in the content of a file. * @param {object} arg - The content of the file to search. * @param {import('lib/fileList').Tree} arg.file - The file. * @param {string} arg.content - The file content. * @param {RegExp} arg.search - The string to search for.
({ file, content, search })
| 106 | * @param {RegExp} arg.search - The string to search for. |
| 107 | */ |
| 108 | function searchInFile({ file, content, search }) { |
| 109 | const matches = []; |
| 110 | |
| 111 | let text = `${file.name}`; |
| 112 | let match; |
| 113 | |
| 114 | if (text.length > 30) { |
| 115 | text = `...${text.slice(-30)}`; |
| 116 | } |
| 117 | |
| 118 | while ((match = search.exec(content))) { |
| 119 | const [word] = match; |
| 120 | const start = match.index; |
| 121 | const end = start + word.length; |
| 122 | const position = { |
| 123 | start: getLineColumn(content, start), |
| 124 | end: getLineColumn(content, end), |
| 125 | }; |
| 126 | const [line, renderText] = getSurrounding(content, word, start, end); |
| 127 | text += `\n\t${line.trim()}`; |
| 128 | matches.push({ match: word, position, renderText, line: line.trim() }); |
| 129 | } |
| 130 | |
| 131 | self.postMessage({ |
| 132 | action: "search-result", |
| 133 | data: { |
| 134 | file, |
| 135 | matches, |
| 136 | text, |
| 137 | }, |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Replace a string in the content of a file. |
nothing calls this directly
no test coverage detected