(offset: number)
| 1402 | } |
| 1403 | |
| 1404 | public getPositionFromOffset(offset: number): Position { |
| 1405 | const lines = this.wrappedLines |
| 1406 | for (let line = 0; line < lines.length; line++) { |
| 1407 | const currentLine = lines[line]! |
| 1408 | const nextLine = lines[line + 1] |
| 1409 | if ( |
| 1410 | offset >= currentLine.startOffset && |
| 1411 | (!nextLine || offset < nextLine.startOffset) |
| 1412 | ) { |
| 1413 | // Calculate string position within the line |
| 1414 | const stringPosInLine = offset - currentLine.startOffset |
| 1415 | |
| 1416 | // Handle leading whitespace for wrapped lines |
| 1417 | let displayColumn: number |
| 1418 | if (currentLine.isPrecededByNewline) { |
| 1419 | // For lines preceded by newline, calculate display width directly |
| 1420 | displayColumn = this.stringIndexToDisplayWidth( |
| 1421 | currentLine.text, |
| 1422 | stringPosInLine, |
| 1423 | ) |
| 1424 | } else { |
| 1425 | // For wrapped lines, we need to account for trimmed whitespace |
| 1426 | const leadingWhitespace = |
| 1427 | currentLine.text.length - currentLine.text.trimStart().length |
| 1428 | if (stringPosInLine < leadingWhitespace) { |
| 1429 | // Cursor is in the trimmed whitespace area, position at start |
| 1430 | displayColumn = 0 |
| 1431 | } else { |
| 1432 | // Calculate display width from the trimmed text |
| 1433 | const trimmedText = currentLine.text.trimStart() |
| 1434 | const posInTrimmed = stringPosInLine - leadingWhitespace |
| 1435 | displayColumn = this.stringIndexToDisplayWidth( |
| 1436 | trimmedText, |
| 1437 | posInTrimmed, |
| 1438 | ) |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | return { |
| 1443 | line, |
| 1444 | column: Math.max(0, displayColumn), |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | // If we're past the last character, return the end of the last line |
| 1450 | const line = lines.length - 1 |
| 1451 | const lastLine = this.wrappedLines[line]! |
| 1452 | return { |
| 1453 | line, |
| 1454 | column: stringWidth(lastLine.text), |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | public get lineCount(): number { |
| 1459 | return this.wrappedLines.length |
no test coverage detected