Skip to content

Fix (graphql-language-service) Hover for first line#4162

Open
lesleydreyer wants to merge 7 commits into
graphql:mainfrom
lesleydreyer:hover-fix
Open

Fix (graphql-language-service) Hover for first line#4162
lesleydreyer wants to merge 7 commits into
graphql:mainfrom
lesleydreyer:hover-fix

Conversation

@lesleydreyer
Copy link
Copy Markdown
Contributor

@lesleydreyer lesleydreyer commented Jan 30, 2026

fix hover for line 1 (0) in graphiql -> monaco-graphiql when hovering the first line

closes #4157 closes #3168

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jan 30, 2026

⚠️ No Changeset found

Latest commit: d5f8b01

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@lesleydreyer lesleydreyer changed the title Hover Fix WIP Hover Fix Jan 31, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 31, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 64.02%. Comparing base (54ed55c) to head (d5f8b01).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4162      +/-   ##
==========================================
+ Coverage   63.96%   64.02%   +0.05%     
==========================================
  Files          35       35              
  Lines        3086     3091       +5     
  Branches      949      949              
==========================================
+ Hits         1974     1979       +5     
  Misses       1107     1107              
  Partials        5        5              
Files with missing lines Coverage Δ
...l-language-service/src/interface/getDiagnostics.ts 66.21% <100.00%> (+0.93%) ⬆️
...hql-language-service/src/parser/CharacterStream.ts 78.26% <100.00%> (+0.73%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@lesleydreyer lesleydreyer changed the title WIP Hover Fix Hover Fix Feb 3, 2026
@lesleydreyer lesleydreyer changed the title Hover Fix Fix Hover for first line Feb 3, 2026
@lesleydreyer lesleydreyer changed the title Fix Hover for first line Fix (graphql-language-service) Hover for first line Feb 19, 2026
Copy link
Copy Markdown
Contributor

@trevor-scheer trevor-scheer left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @lesleydreyer! At some point I'll have a closer look at this, but I left a couple comments in case you feel like poking around a bit more to get this right. Hopefully I'll have some more helpful comments for you in the near future.

Comment on lines +253 to +254
end: { character: 23, line: -1 },
start: { character: 22, line: -1 },
Copy link
Copy Markdown
Contributor

@trevor-scheer trevor-scheer Apr 15, 2026

Choose a reason for hiding this comment

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

This smells funny, I suspect we need to normalize the off by 1 line number at the LSP/Monaco boundary where the line off by 1 is happening (presumably).

I can spend some more time investigating at some point but there's probably a more comprehensive solution that results in line numbers we'd actually expect here (presumably 0).

// LSP may count from 1, but monaco counts from 0
for (
let i = 0;
location.line === 0 ? i <= location.line : i < location.line;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Related to the comment above, this doesn't feel like the correct place for this fix.

trevor-scheer added a commit that referenced this pull request May 6, 2026
Hover is broken in `monaco-graphql`. On the first line it throws and
renders nothing; on later lines it doesn't crash but highlights the
wrong token.

`GraphQLWorker.doHover` passes a 0-indexed position to `getRange`, which
actually wants a 1-indexed `SourceLocation` (per the GraphQL spec, and
what its other callers pass via `error.locations[0]`). On line 0 the
loop never runs and `Expected Parser stream to be available` fires. On
later lines `getRange` parses to end-of-line and ignores `column`, so
the range comes back pointing at the previous line's last token.

`getRange` was the wrong tool here anyway. It was built for diagnostics,
not for finding the token under a cursor. `getTokenAtPosition` is what
we want, and it's what the rest of the language service already uses.

Fixes #4157
Fixes #3168

Supersedes #4162, which tried to special-case `line === 0` inside
`getRange` itself. That left `getRange` returning a `Position` with
`line: -1` (which becomes `0` after `toMonacoRange`'s `+1`, invalid in
Monaco's 1-indexed API), and didn't address the bigger problem that
`getRange` doesn't compute a token range to begin with.

## Validation

```bash
yarn install
yarn build
yarn workspace example-monaco-graphql-react-vite dev
```

Open the URL Vite prints, with browser devtools open.

1. Hover the word `query` on the first line. Before: console logs
`Error: Expected Parser stream to be available` and no popup appears.
After: popup renders and `query` is highlighted.
2. Press Enter to push the query down a line, then hover a field name.
Before: popup appears but the highlight is on the previous line's last
token. After: the field under the cursor is highlighted.
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.

[graphql-language-service] hover broken [language-service] getRange does not work for line 0

2 participants