(lines: readonly string[], selection: EditorSelection)
| 448 | * @returns Selected text |
| 449 | */ |
| 450 | export function getSelectedText(lines: readonly string[], selection: EditorSelection): string { |
| 451 | const [start, end] = normalizeSelection(selection); |
| 452 | |
| 453 | if (start.line === end.line) { |
| 454 | const line = lines[start.line] ?? ""; |
| 455 | return line.slice(start.column, end.column); |
| 456 | } |
| 457 | |
| 458 | const result: string[] = []; |
| 459 | |
| 460 | // First line |
| 461 | const firstLine = lines[start.line] ?? ""; |
| 462 | result.push(firstLine.slice(start.column)); |
| 463 | |
| 464 | // Middle lines |
| 465 | for (let i = start.line + 1; i < end.line; i++) { |
| 466 | result.push(lines[i] ?? ""); |
| 467 | } |
| 468 | |
| 469 | // Last line |
| 470 | const lastLine = lines[end.line] ?? ""; |
| 471 | result.push(lastLine.slice(0, end.column)); |
| 472 | |
| 473 | return result.join("\n"); |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Compute scroll position to ensure cursor is visible. |
no test coverage detected