[api] Add SourceFile line mapping methods#4420
Open
andrewbranch wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds line/character mapping helpers to the native-preview SourceFile API surface to close #4215, enabling consumers to convert between UTF-16 positions and { line, character } pairs.
Changes:
- Introduces
LineAndCharacterand addsgetLineStarts,getLineAndCharacterOfPosition, andgetPositionOfLineAndCharacterto theSourceFileinterface. - Implements these methods on
RemoteSourceFile, including a cached line-starts array and a binary search for position-to-line mapping. - Adds encoder round-trip tests covering line starts, UTF-16 semantics, and line/character ↔ position conversion.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| _packages/native-preview/test/encoder.test.ts | Adds tests validating line start computation, UTF-16 line/character mapping, caching, and round-tripping. |
| _packages/native-preview/src/ast/ast.ts | Extends the public AST types with LineAndCharacter and new SourceFile mapping APIs. |
| _packages/native-preview/src/api/node/node.ts | Implements the new mapping APIs on RemoteSourceFile using computeLineStarts + binary search. |
Comment on lines
+212
to
+216
| getLineAndCharacterOfPosition(position: number): LineAndCharacter { | ||
| const lineStarts = this.getLineStarts(); | ||
| const line = computeLineOfPosition(lineStarts, position); | ||
| return { line, character: position - lineStarts[line] }; | ||
| } |
Member
Author
There was a problem hiding this comment.
The Strada implementation doesn't guard, so I think it's fine as-is
Comment on lines
+218
to
+224
| getPositionOfLineAndCharacter(line: number, character: number): number { | ||
| const lineStarts = this.getLineStarts(); | ||
| if (line < 0 || line >= lineStarts.length) { | ||
| throw new Error(`Bad line number. Line: ${line}, lineStarts.length: ${lineStarts.length}`); | ||
| } | ||
| return lineStarts[line] + character; | ||
| } |
jakebailey
approved these changes
Jun 23, 2026
jakebailey
approved these changes
Jun 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4215