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.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code Lens #513
Code Lens #513
Changes from 7 commits
f508796
005388a
564f905
d64ed9a
c79a8f9
47cf51b
1246434
e81288b
8d76d6f
980beeb
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A "gotcha" that I needed some time to figure out is that these commands that you add to the code can only be 3 chars. I had more than 3 chars and couldn't figure out why it didn't pick them up in the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to generalise that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This creates an AST iterator targeting value bindings,
let whatever = ...
. Since we're only looking for function definitions, we can target value bindings only, since that's the only place they'll appear.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pattern matches the value binding to target 2 things:
Ppat_var
means it's a variable, likelet whatever
. Assignments can also be destructureslet {something} = ....
, but this ensures we only target variables.Pexp_fun
means it's a function definition.pvb_pat
is the pattern, which is essentially the left hand side oflet whatever = something
.pvb_expr
is the expression being assigned, so the right hand side after=
. We ensure that it is indeed a variable being assignedPpat_var
, and that it's being assigned a function definitionPexp_fun
.Notice that we're also extracting
ppat_loc
frompvb_pat
. That's the location of the variable name itself (whatever
inlet whatever = ....
). That location is important, because we'll extract what type that location has, and that will lead us to the type definition for the function itself. Same logic as if you were to hover the variable holding the function - that's what'll produce the correct function type, not hovering the actual function itself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This creates an iterator, and ensures that
value_binding
is the only thing we're overriding with our own logic. There's a ton of other things you can override as well if you'd want to.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This here looks up the compiler artifact for
path
, which will be the file we're extracting code lenses from. The compiler artifacts are what holds the type information as the compiler has compiled successfully.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We then use a helper to get the full type via the compiler artifact for the target file, using the positions where we found variables being assigned functions. This will lead us to the full, correct type definitions for all functions assigned to variables.
A type at a location can be a bunch of different things - we target
Typed
specifically here, because we know that's the only thing our function definition can be. InTyped
,typeExpr
is what we're interested in.typeExpr
comes from the type checker, and we have helpers that can print those type definitions as strings.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering whether extension config should go to its own file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean the type definition?