-
-
Notifications
You must be signed in to change notification settings - Fork 113
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
feat(lsp): Add Inlay types #2005
Open
spotandjake
wants to merge
9
commits into
grain-lang:main
Choose a base branch
from
spotandjake:spotandjake-inlay-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3cc4ffe
feat(lsp): Add Inlay types
spotandjake 1a5872a
chore: Simplify `Function` inlays
spotandjake 848978c
feat(lsp): Implement Inlays For parameters
spotandjake 217b7e5
Do not show hints for top level statements
spotandjake da038be
chore: Disable type hints for functions.
spotandjake ffc8709
chore: Add flag to toggle the type hints
spotandjake 04048de
chore: Apply suggestions from code review
spotandjake d281379
chore: Apply suggestions from oscar code review
spotandjake e86c6f2
chore: Remove cmdline toggle
spotandjake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,14 +33,100 @@ let send_no_result = (~id: Protocol.message_id) => { | |
Protocol.response(~id, `Null); | ||
}; | ||
|
||
let build_hint = | ||
(position: Protocol.position, message: string): ResponseResult.inlay_hint => { | ||
{label: ": " ++ message, position}; | ||
}; | ||
|
||
let rec resolve_typ = (typ: Types.type_expr) => { | ||
switch (typ.desc) { | ||
| TTyLink(type_expr) | ||
| TTySubst(type_expr) => resolve_typ(type_expr) | ||
| _ => typ | ||
}; | ||
}; | ||
let string_of_typ = (typ: Types.type_expr) => { | ||
Printtyp.string_of_type_scheme(resolve_typ(typ)); | ||
}; | ||
|
||
let is_func_typ = (typ: Types.type_expr) => { | ||
switch (resolve_typ(typ).desc) { | ||
| TTyArrow(_, _, _) => true | ||
| _ => false | ||
}; | ||
}; | ||
|
||
let find_hints = program => { | ||
let hints = ref([]); | ||
open Typedtree; | ||
open Protocol; | ||
module Iterator = | ||
TypedtreeIter.MakeIterator({ | ||
include TypedtreeIter.DefaultIteratorArgument; | ||
// Inlay hints for various expressions can be included here. | ||
|
||
let enter_expression = ({exp_desc, exp_type}: expression) => | ||
switch (exp_desc) { | ||
| TExpLambda(bindings, _) => | ||
List.iter( | ||
({mb_pat, mb_loc}: match_branch) => { | ||
switch (mb_pat.pat_desc) { | ||
| TPatTuple(args) => | ||
switch (resolve_typ(exp_type).desc) { | ||
| TTyArrow(typ_args, _, _) => | ||
let argument_typs = | ||
List.map( | ||
((arg, typ: Types.type_expr)) => | ||
switch (arg) { | ||
| Default(_) => None | ||
| _ => Some(typ) | ||
}, | ||
typ_args, | ||
); | ||
if (List.length(argument_typs) == List.length(args)) { | ||
List.iter( | ||
((arg: pattern, typ: option(Types.type_expr))) => { | ||
switch (arg.pat_desc, typ) { | ||
| (TPatVar(_, _), Some(typ)) => | ||
let bind_end = arg.pat_loc.loc_end; | ||
let p: Protocol.position = { | ||
line: bind_end.pos_lnum - 1, | ||
character: bind_end.pos_cnum - bind_end.pos_bol, | ||
}; | ||
let typeSignature = string_of_typ(typ); | ||
hints := [build_hint(p, typeSignature), ...hints^]; | ||
| _ => () | ||
} | ||
}, | ||
List.combine(args, argument_typs), | ||
); | ||
}; | ||
| _ => () | ||
} | ||
| _ => () | ||
} | ||
}, | ||
bindings, | ||
) | ||
| _ => () | ||
}; | ||
|
||
let enter_binding = ({vb_pat, vb_expr}: value_binding, toplevel: bool) => | ||
if (!toplevel) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: cut down on the condition nesting, perhaps
|
||
switch (vb_pat) { | ||
| {pat_extra: [], pat_desc: TPatVar(_, {loc})} => | ||
let bind_end = loc.loc_end; | ||
let p: Protocol.position = { | ||
line: bind_end.pos_lnum - 1, | ||
character: bind_end.pos_cnum - bind_end.pos_bol, | ||
}; | ||
let typ = vb_pat.pat_type; | ||
if (!is_func_typ(typ)) { | ||
let typeSignature = string_of_typ(typ); | ||
hints := [build_hint(p, typeSignature), ...hints^]; | ||
}; | ||
| _ => () | ||
}; | ||
}; | ||
}); | ||
Iterator.iter_typed_program(program); | ||
hints^; | ||
|
This file contains 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
This file contains 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
Oops, something went wrong.
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.
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.
Would this ever be possible? If not should we do
failwith("Impossible: ...")
?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.
It seems as though there is a case where it can be a constructor.