(position: Position)
| 1359 | } |
| 1360 | |
| 1361 | public getOffsetFromPosition(position: Position): number { |
| 1362 | const wrappedLine = this.getLine(position.line) |
| 1363 | |
| 1364 | // Handle blank lines specially |
| 1365 | if (wrappedLine.text.length === 0 && wrappedLine.endsWithNewline) { |
| 1366 | return wrappedLine.startOffset |
| 1367 | } |
| 1368 | |
| 1369 | // Account for leading whitespace |
| 1370 | const leadingWhitespace = wrappedLine.isPrecededByNewline |
| 1371 | ? 0 |
| 1372 | : wrappedLine.text.length - wrappedLine.text.trimStart().length |
| 1373 | |
| 1374 | // Convert display column to string index |
| 1375 | const displayColumnWithLeading = position.column + leadingWhitespace |
| 1376 | const stringIndex = this.displayWidthToStringIndex( |
| 1377 | wrappedLine.text, |
| 1378 | displayColumnWithLeading, |
| 1379 | ) |
| 1380 | |
| 1381 | // Calculate the actual offset |
| 1382 | const offset = wrappedLine.startOffset + stringIndex |
| 1383 | |
| 1384 | // For normal lines |
| 1385 | const lineEnd = wrappedLine.startOffset + wrappedLine.text.length |
| 1386 | |
| 1387 | // Don't allow going past the end of the current line into the next line |
| 1388 | // unless we're at the very end of the text |
| 1389 | let maxOffset = lineEnd |
| 1390 | const lineDisplayWidth = stringWidth(wrappedLine.text) |
| 1391 | if (wrappedLine.endsWithNewline && position.column > lineDisplayWidth) { |
| 1392 | // Allow positioning after the newline |
| 1393 | maxOffset = lineEnd + 1 |
| 1394 | } |
| 1395 | |
| 1396 | return Math.min(offset, maxOffset) |
| 1397 | } |
| 1398 | |
| 1399 | public getLineLength(line: number): number { |
| 1400 | const wrappedLine = this.getLine(line) |
no test coverage detected