(position: Position)
| 1384 | } |
| 1385 | |
| 1386 | public getOffsetFromPosition(position: Position): number { |
| 1387 | const wrappedLine = this.getLine(position.line) |
| 1388 | |
| 1389 | // Handle blank lines specially |
| 1390 | if (wrappedLine.text.length === 0 && wrappedLine.endsWithNewline) { |
| 1391 | return wrappedLine.startOffset |
| 1392 | } |
| 1393 | |
| 1394 | // Account for leading whitespace |
| 1395 | const leadingWhitespace = wrappedLine.isPrecededByNewline |
| 1396 | ? 0 |
| 1397 | : wrappedLine.text.length - wrappedLine.text.trimStart().length |
| 1398 | |
| 1399 | // Convert display column to string index |
| 1400 | const displayColumnWithLeading = position.column + leadingWhitespace |
| 1401 | const stringIndex = this.displayWidthToStringIndex( |
| 1402 | wrappedLine.text, |
| 1403 | displayColumnWithLeading, |
| 1404 | ) |
| 1405 | |
| 1406 | // Calculate the actual offset |
| 1407 | const offset = wrappedLine.startOffset + stringIndex |
| 1408 | |
| 1409 | // For normal lines |
| 1410 | const lineEnd = wrappedLine.startOffset + wrappedLine.text.length |
| 1411 | |
| 1412 | // Don't allow going past the end of the current line into the next line |
| 1413 | // unless we're at the very end of the text |
| 1414 | let maxOffset = lineEnd |
| 1415 | const lineDisplayWidth = stringWidth(wrappedLine.text) |
| 1416 | if (wrappedLine.endsWithNewline && position.column > lineDisplayWidth) { |
| 1417 | // Allow positioning after the newline |
| 1418 | maxOffset = lineEnd + 1 |
| 1419 | } |
| 1420 | |
| 1421 | return Math.min(offset, maxOffset) |
| 1422 | } |
| 1423 | |
| 1424 | public getLineLength(line: number): number { |
| 1425 | const wrappedLine = this.getLine(line) |
no test coverage detected