Skip to content
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

Hover record type substitution #560

Merged
merged 4 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#### :bug: Bug Fix

- Fix issue where hovering over a field in record construction would show the type without instantiating its type arguments https://github.com/rescript-lang/rescript-vscode/pull/560
- Fix Incorrect semantic highlighting of `external` declarations https://github.com/rescript-lang/rescript-vscode/pull/517
- Fix issue where doc comment with nested comments inside is not shown properly on hover https://github.com/rescript-lang/rescript-vscode/pull/526
- Fix server crashes when open file is removed from disk with inlayHints enabled https://github.com/rescript-lang/rescript-vscode/issues/538
Expand Down
16 changes: 9 additions & 7 deletions analysis/src/ProcessExtra.ml
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ let getTypeAtPath ~env path =
| Some declaredType -> `Local declaredType
| None -> `Not_found)

let addForField ~env ~extra recordType fieldType {Asttypes.txt; loc} =
let addForField ~env ~extra ~recordType ~fieldType {Asttypes.txt; loc} =
match (Shared.dig recordType).desc with
| Tconstr (path, _args, _memo) ->
let t = getTypeAtPath ~env path in
Expand All @@ -232,12 +232,12 @@ let addForField ~env ~extra recordType fieldType {Asttypes.txt; loc} =
addLocItem extra nameLoc (Typed (name, fieldType, locType))
| _ -> ()

let addForRecord ~env ~extra recordType items =
let addForRecord ~env ~extra ~recordType items =
match (Shared.dig recordType).desc with
| Tconstr (path, _args, _memo) ->
let t = getTypeAtPath ~env path in
items
|> List.iter (fun ({Asttypes.txt; loc}, {Types.lbl_res}, _) ->
|> List.iter (fun ({Asttypes.txt; loc}, _, _) ->
(* let name = Longident.last(txt); *)
let name = handleConstructor txt in
let nameLoc = Utils.endOfLocation loc (String.length name) in
Expand All @@ -254,7 +254,7 @@ let addForRecord ~env ~extra recordType items =
GlobalReference (moduleName, path, Field name)
| _ -> NotFound
in
addLocItem extra nameLoc (Typed (name, lbl_res, locType)))
addLocItem extra nameLoc (Typed (name, recordType, locType)))
| _ -> ()

let addForConstructor ~env ~extra constructorType {Asttypes.txt; loc}
Expand Down Expand Up @@ -373,7 +373,8 @@ let pat ~(file : File.t) ~env ~extra (iter : Tast_iterator.iterator)
in
(* Log.log("Entering pattern " ++ Utils.showLocation(pat_loc)); *)
(match pattern.pat_desc with
| Tpat_record (items, _) -> addForRecord ~env ~extra pattern.pat_type items
| Tpat_record (items, _) ->
addForRecord ~env ~extra ~recordType:pattern.pat_type items
| Tpat_construct (lident, constructor, _) ->
addForConstructor ~env ~extra pattern.pat_type lident constructor
| Tpat_alias (_inner, ident, name) ->
Expand All @@ -400,7 +401,7 @@ let expr ~env ~(extra : extra) (iter : Tast_iterator.iterator)
(Some (expression.exp_type, Value))
path txt loc
| Texp_record {fields} ->
addForRecord ~env ~extra expression.exp_type
addForRecord ~env ~extra ~recordType:expression.exp_type
(fields |> Array.to_list
|> Utils.filterMap (fun (desc, item) ->
match item with
Expand All @@ -415,7 +416,8 @@ let expr ~env ~(extra : extra) (iter : Tast_iterator.iterator)
| Texp_construct (lident, constructor, _args) ->
addForConstructor ~env ~extra expression.exp_type lident constructor
| Texp_field (inner, lident, _label_description) ->
addForField ~env ~extra inner.exp_type expression.exp_type lident
addForField ~env ~extra ~recordType:inner.exp_type
~fieldType:expression.exp_type lident
| _ -> ());
Tast_iterator.default_iterator.expr iter expression

Expand Down
13 changes: 13 additions & 0 deletions analysis/tests/src/Hover.res
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,16 @@ module ModWithDocComment = {

/*** module level doc comment 2 */
}

module TypeSubstitutionRecords = {
type foo<'a> = {content: 'a, zzz: string}
type bar = {age: int}
type foobar = foo<bar>

// ^db+

let x1: foo<bar> = {content: {age: 42}, zzz: ""}
// ^hov
let x2: foobar = {content: {age: 42}, zzz: ""}
// ^hov
}
22 changes: 22 additions & 0 deletions analysis/tests/src/expected/Hover.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,25 @@ Hover src/Hover.res 142:9
Hover src/Hover.res 146:6
{"contents": "```rescript\nint\n```\n\n doc comment 2 "}


Hover src/Hover.res 165:23
Ident!! Dep
Ident!! JsLogger
Ident!! A
Ident!! B
Ident!! Comp
locItems:
165:22-165:29 Typed content foo<bar> (LocalReference Field(content))
[ref] Local defn Field(content)
{"contents": "```rescript\nfoo<bar>\n```\n\n```rescript\ntype foo<'a> = {content: 'a, zzz: string}\n```"}

Hover src/Hover.res 167:22
Ident!! Dep
Ident!! JsLogger
Ident!! A
Ident!! B
Ident!! Comp
locItems:
167:20-167:27 Typed content foobar NotFound
{"contents": "```rescript\nfoobar\n```\n\n```rescript\ntype foobar = foo<bar>\n```"}