MCPcopy Create free account
hub / github.com/angular/angular / positionToOffset

Function positionToOffset

packages/compiler-cli/preprocessor/src/tcb_util.ts:43–66  ·  view source on GitHub ↗
(
  content: string,
  position: {line: number; character: number},
)

Source from the content-addressed store, hash-verified

41}
42
43export function positionToOffset(
44 content: string,
45 position: {line: number; character: number},
46): number {
47 const lineStarts = getLineStarts(content);
48 const line = Math.max(0, position.line);
49 if (line >= lineStarts.length) {
50 return content.length; // Fallback
51 }
52 const lineStart = lineStarts[line];
53 const nextLineStart = lineStarts[line + 1] ?? content.length;
54
55 let lineLength = nextLineStart - lineStart;
56 if (lineLength > 0 && content[nextLineStart - 1] === '\n') {
57 lineLength--;
58 if (lineLength > 0 && content[nextLineStart - 2] === '\r') {
59 lineLength--;
60 }
61 }
62
63 const character = Math.max(0, Math.min(position.character, lineLength)); // Clamp character
64
65 return lineStart + character;
66}
67
68/**
69 * Returns the 1-based line number containing `offset`, reusing the cached line-start index.

Callers 6

translatePositionFunction · 0.85
getDefinitionMethod · 0.85
getSetupFunction · 0.85
assertTextSpansFunction · 0.85
getDocumentContextFunction · 0.85

Calls 3

getLineStartsFunction · 0.85
maxMethod · 0.45
minMethod · 0.45

Tested by

no test coverage detected