(offset: number)
| 1427 | } |
| 1428 | |
| 1429 | public getPositionFromOffset(offset: number): Position { |
| 1430 | const lines = this.wrappedLines |
| 1431 | for (let line = 0; line < lines.length; line++) { |
| 1432 | const currentLine = lines[line]! |
| 1433 | const nextLine = lines[line + 1] |
| 1434 | if ( |
| 1435 | offset >= currentLine.startOffset && |
| 1436 | (!nextLine || offset < nextLine.startOffset) |
| 1437 | ) { |
| 1438 | // Calculate string position within the line |
| 1439 | const stringPosInLine = offset - currentLine.startOffset |
| 1440 | |
| 1441 | // Handle leading whitespace for wrapped lines |
| 1442 | let displayColumn: number |
| 1443 | if (currentLine.isPrecededByNewline) { |
| 1444 | // For lines preceded by newline, calculate display width directly |
| 1445 | displayColumn = this.stringIndexToDisplayWidth( |
| 1446 | currentLine.text, |
| 1447 | stringPosInLine, |
| 1448 | ) |
| 1449 | } else { |
| 1450 | // For wrapped lines, we need to account for trimmed whitespace |
| 1451 | const leadingWhitespace = |
| 1452 | currentLine.text.length - currentLine.text.trimStart().length |
| 1453 | if (stringPosInLine < leadingWhitespace) { |
| 1454 | // Cursor is in the trimmed whitespace area, position at start |
| 1455 | displayColumn = 0 |
| 1456 | } else { |
| 1457 | // Calculate display width from the trimmed text |
| 1458 | const trimmedText = currentLine.text.trimStart() |
| 1459 | const posInTrimmed = stringPosInLine - leadingWhitespace |
| 1460 | displayColumn = this.stringIndexToDisplayWidth( |
| 1461 | trimmedText, |
| 1462 | posInTrimmed, |
| 1463 | ) |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | return { |
| 1468 | line, |
| 1469 | column: Math.max(0, displayColumn), |
| 1470 | } |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | // If we're past the last character, return the end of the last line |
| 1475 | const line = lines.length - 1 |
| 1476 | const lastLine = this.wrappedLines[line]! |
| 1477 | return { |
| 1478 | line, |
| 1479 | column: stringWidth(lastLine.text), |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | public get lineCount(): number { |
| 1484 | return this.wrappedLines.length |
no test coverage detected