* Gets surrounding text of a match. * @param {string} content * @param {string} word * @param {number} start * @param {number} end
(content, word, start, end)
| 163 | * @param {number} end |
| 164 | */ |
| 165 | function getSurrounding(content, word, start, end) { |
| 166 | const max = 160; |
| 167 | let lineStart = start; |
| 168 | while (lineStart > 0) { |
| 169 | const previous = content[lineStart - 1]; |
| 170 | if (previous === "\n" || previous === "\r") break; |
| 171 | lineStart--; |
| 172 | } |
| 173 | |
| 174 | let lineEnd = end; |
| 175 | while (lineEnd < content.length) { |
| 176 | const current = content[lineEnd]; |
| 177 | if (current === "\n" || current === "\r") break; |
| 178 | lineEnd++; |
| 179 | } |
| 180 | |
| 181 | let snippetStart = lineStart; |
| 182 | let snippetEnd = lineEnd; |
| 183 | if (lineEnd - lineStart > max) { |
| 184 | const matchLength = Math.max(1, end - start); |
| 185 | const remaining = Math.max(0, max - matchLength); |
| 186 | const left = Math.floor(remaining / 2); |
| 187 | const right = remaining - left; |
| 188 | snippetStart = Math.max(lineStart, start - left); |
| 189 | snippetEnd = Math.min(lineEnd, end + right); |
| 190 | } |
| 191 | |
| 192 | let line = content.substring(snippetStart, snippetEnd).trim(); |
| 193 | if (snippetStart > lineStart) line = `...${line}`; |
| 194 | if (snippetEnd < lineEnd) line = `${line}...`; |
| 195 | |
| 196 | return [line, word].map((text) => text.replace(/[\r\n]+/g, " ⏎ ")); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Determines the line and column numbers for a given position in the file. |