Skip to content

Commit fbd491d

Browse files
committed
When hovering over a field in record construction, show the instantiated type.
1 parent 28eedaf commit fbd491d

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#### :bug: Bug Fix
2323

24+
- 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
2425
- Fix Incorrect semantic highlighting of `external` declarations https://github.com/rescript-lang/rescript-vscode/pull/517
2526
- Fix issue where doc comment with nested comments inside is not shown properly on hover https://github.com/rescript-lang/rescript-vscode/pull/526
2627
- Fix server crashes when open file is removed from disk with inlayHints enabled https://github.com/rescript-lang/rescript-vscode/issues/538

analysis/src/ProcessExtra.ml

+9-7
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ let getTypeAtPath ~env path =
210210
| Some declaredType -> `Local declaredType
211211
| None -> `Not_found)
212212

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

235-
let addForRecord ~env ~extra recordType items =
235+
let addForRecord ~env ~extra ~recordType items =
236236
match (Shared.dig recordType).desc with
237237
| Tconstr (path, _args, _memo) ->
238238
let t = getTypeAtPath ~env path in
239239
items
240-
|> List.iter (fun ({Asttypes.txt; loc}, {Types.lbl_res}, _) ->
240+
|> List.iter (fun ({Asttypes.txt; loc}, _, _) ->
241241
(* let name = Longident.last(txt); *)
242242
let name = handleConstructor txt in
243243
let nameLoc = Utils.endOfLocation loc (String.length name) in
@@ -254,7 +254,7 @@ let addForRecord ~env ~extra recordType items =
254254
GlobalReference (moduleName, path, Field name)
255255
| _ -> NotFound
256256
in
257-
addLocItem extra nameLoc (Typed (name, lbl_res, locType)))
257+
addLocItem extra nameLoc (Typed (name, recordType, locType)))
258258
| _ -> ()
259259

260260
let addForConstructor ~env ~extra constructorType {Asttypes.txt; loc}
@@ -373,7 +373,8 @@ let pat ~(file : File.t) ~env ~extra (iter : Tast_iterator.iterator)
373373
in
374374
(* Log.log("Entering pattern " ++ Utils.showLocation(pat_loc)); *)
375375
(match pattern.pat_desc with
376-
| Tpat_record (items, _) -> addForRecord ~env ~extra pattern.pat_type items
376+
| Tpat_record (items, _) ->
377+
addForRecord ~env ~extra ~recordType:pattern.pat_type items
377378
| Tpat_construct (lident, constructor, _) ->
378379
addForConstructor ~env ~extra pattern.pat_type lident constructor
379380
| Tpat_alias (_inner, ident, name) ->
@@ -400,7 +401,7 @@ let expr ~env ~(extra : extra) (iter : Tast_iterator.iterator)
400401
(Some (expression.exp_type, Value))
401402
path txt loc
402403
| Texp_record {fields} ->
403-
addForRecord ~env ~extra expression.exp_type
404+
addForRecord ~env ~extra ~recordType:expression.exp_type
404405
(fields |> Array.to_list
405406
|> Utils.filterMap (fun (desc, item) ->
406407
match item with
@@ -415,7 +416,8 @@ let expr ~env ~(extra : extra) (iter : Tast_iterator.iterator)
415416
| Texp_construct (lident, constructor, _args) ->
416417
addForConstructor ~env ~extra expression.exp_type lident constructor
417418
| Texp_field (inner, lident, _label_description) ->
418-
addForField ~env ~extra inner.exp_type expression.exp_type lident
419+
addForField ~env ~extra ~recordType:inner.exp_type
420+
~fieldType:expression.exp_type lident
419421
| _ -> ());
420422
Tast_iterator.default_iterator.expr iter expression
421423

analysis/tests/src/expected/Hover.res.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ Ident!! A
112112
Ident!! B
113113
Ident!! Comp
114114
locItems:
115-
165:22-165:29 Typed content foo<'a> (LocalReference Field(content))
115+
165:22-165:29 Typed content foo<bar> (LocalReference Field(content))
116116
[ref] Local defn Field(content)
117-
{"contents": "```rescript\nfoo<'a>\n```\n\n```rescript\ntype foo<'a> = {content: 'a, zzz: string}\n```"}
117+
{"contents": "```rescript\nfoo<bar>\n```\n\n```rescript\ntype foo<'a> = {content: 'a, zzz: string}\n```"}
118118

119119
Hover src/Hover.res 167:22
120120
Ident!! Dep
@@ -123,6 +123,6 @@ Ident!! A
123123
Ident!! B
124124
Ident!! Comp
125125
locItems:
126-
167:20-167:27 Typed content foo<'a> NotFound
127-
{"contents": "```rescript\nfoo<'a>\n```\n\n```rescript\ntype foo<'a> = {content: 'a, zzz: string}\n```"}
126+
167:20-167:27 Typed content foobar NotFound
127+
{"contents": "```rescript\nfoobar\n```\n\n```rescript\ntype foobar = foo<bar>\n```"}
128128

0 commit comments

Comments
 (0)