Skip to content

Conversation

@jakebailey
Copy link
Member

@DanielRosenwasser noticed I made this same mistake. GetRangeOfTokenAtPosition implicitly skips trivia, but then also scans a token. This is wasteful for callers that could just skip trivia.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes two call sites by replacing GetRangeOfTokenAtPosition().Pos() with SkipTrivia() to avoid unnecessary token scanning. The function GetRangeOfTokenAtPosition internally calls SkipTrivia and then scans a full token to compute a range, which is wasteful when only the position after trivia is needed.

Key Changes

  • Replaced scanner.GetRangeOfTokenAtPosition(file, lastToken.End()).Pos() with scanner.SkipTrivia(file.Text(), lastToken.End()) in semicolon detection logic
  • Replaced scanner.GetRangeOfTokenAtPosition(sourceFile, node.Pos()).Pos() with scanner.SkipTrivia(sourceFile.Text(), node.Pos()) in with statement error reporting

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
internal/ls/lsutil/utilities.go Optimized ProbablyUsesSemicolons to skip trivia directly when determining line positions for semicolon inference
internal/checker/checker.go Optimized checkWithStatement to skip trivia directly when computing error span start position

sourceFile := ast.GetSourceFileOfNode(node)
if !c.hasParseDiagnostics(sourceFile) {
start := scanner.GetRangeOfTokenAtPosition(sourceFile, node.Pos()).Pos()
start := scanner.SkipTrivia(sourceFile.Text(), node.Pos())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're going to be so much faster when erroring on with statements now. 😎

@jakebailey jakebailey added this pull request to the merge queue Nov 14, 2025
Merged via the queue into main with commit bbde564 Nov 14, 2025
28 checks passed
@jakebailey jakebailey deleted the jabaile/just-skip-trivia branch November 14, 2025 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants