MCPcopy Index your code
hub / github.com/claude-code-best/claude-code / getOffsetFromPosition

Method getOffsetFromPosition

src/utils/Cursor.ts:1361–1397  ·  view source on GitHub ↗
(position: Position)

Source from the content-addressed store, hash-verified

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)

Callers 2

PromptInputFunction · 0.80
getOffsetMethod · 0.80

Calls 2

getLineMethod · 0.95

Tested by

no test coverage detected