Improve Inlay Hints Handling with [@merlin.hide] #1894
+218
−97
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.
Description
Previously, inlay hints logic relied on ghost locations to avoid annotating generated code. But it lead to some inconsistencies because ghost locations are used in non-generated code, for instance:
In this case, an inlay hint appears for
x
inf
, but no hint is shown forg
.In VS Code, this results in the following display:
![image](https://private-user-images.githubusercontent.com/5814258/409654429-721014a6-7842-415c-8f3e-dd38e57722fd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4MzI3OTUsIm5iZiI6MTczODgzMjQ5NSwicGF0aCI6Ii81ODE0MjU4LzQwOTY1NDQyOS03MjEwMTRhNi03ODQyLTQxNWMtOGYzZS1kZDM4ZTU3NzIyZmQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwNiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDZUMDkwMTM1WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NzQ3MGQwYzJmZWQ0NjFhMzUwMDZmN2JiYWJiYTJlZjAxMzk4NzQ3NjhiYWEzM2QyMmUwZWJmMGJkZTQwNGJjOSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.UKR5h12rpX6ZTOuRMD4pEXzEzLQPoyWNMFjDYZLRmSE)
This is because
is syntactic sugar for:
where
fun _ ->
introduces a ghost location (the whole fun node).This is why you needed to disable the check on ghost location in your
samples.t
test.Proposed Fix
This PR replaces ghost location handling with the [@merlin.hide] attribute. This allows for more precise control over which parts of the AST should be ignored for inlay hints.
To make this approach work correctly, I refactored the main AST iterator to use
Ast_iterators.iter_only_visible
. The iterator now ensures attributes are not skipped by recursing on non-immediate sub-terms.Also, this restructuring simplifies the code and makes inlay hints more consistent across different contexts (e.g., top-level
let
bindings, class-levellet
bindings, and expressionlet
bindings).Additional Changes
samples.t
with new test cases illustrating the improved inlay hints behavior.avoid-ghost-location
option (both in the CLI and protocol).Related
Screenshot