( range: CodeRange, fragments: CodeTag[], )
| 16 | * isolated fragment within. |
| 17 | */ |
| 18 | export function extractRange( |
| 19 | range: CodeRange, |
| 20 | fragments: CodeTag[], |
| 21 | ): [CodeTag[], number] { |
| 22 | const [from, to] = range; |
| 23 | let [fromRow, fromColumn] = from; |
| 24 | let [toRow, toColumn] = to; |
| 25 | if (fromRow > toRow || (fromRow === toRow && fromColumn > toColumn)) { |
| 26 | [fromRow, fromColumn] = to; |
| 27 | [toRow, toColumn] = from; |
| 28 | } |
| 29 | |
| 30 | let currentRow = 0; |
| 31 | let currentColumn = 0; |
| 32 | const newFragments: CodeTag[] = []; |
| 33 | let index = -1; |
| 34 | let found = false; |
| 35 | let extracted = ''; |
| 36 | |
| 37 | for (const fragment of fragments) { |
| 38 | if (found) { |
| 39 | newFragments.push(fragment); |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | const resolved = resolveCodeTag(fragment, false); |
| 44 | const lines = resolved.split('\n'); |
| 45 | const newRows = lines.length - 1; |
| 46 | const lastColumn = lines[newRows].length; |
| 47 | const nextColumn = newRows > 0 ? lastColumn : currentColumn + lastColumn; |
| 48 | |
| 49 | if ( |
| 50 | fromRow > currentRow + newRows || |
| 51 | (fromRow === currentRow + newRows && fromColumn > nextColumn) |
| 52 | ) { |
| 53 | currentRow += newRows; |
| 54 | currentColumn = nextColumn; |
| 55 | newFragments.push(fragment); |
| 56 | continue; |
| 57 | } |
| 58 | |
| 59 | for (let i = 0; i < resolved.length; i++) { |
| 60 | const char = resolved.charAt(i); |
| 61 | if (fromRow === currentRow && fromColumn >= currentColumn) { |
| 62 | if (fromColumn === currentColumn) { |
| 63 | index = newFragments.length + 1; |
| 64 | newFragments.push(resolved.slice(0, i), ''); |
| 65 | } else if (char === '\n') { |
| 66 | index = newFragments.length + 1; |
| 67 | newFragments.push( |
| 68 | resolved.slice(0, i) + ' '.repeat(fromColumn - currentColumn), |
| 69 | '', |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (index !== -1 && toRow === currentRow && toColumn >= currentColumn) { |
| 75 | if (toColumn === currentColumn) { |
no test coverage detected