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

Handle optional fields in completion #691

Merged
merged 4 commits into from
Jan 11, 2023
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 @@ -22,6 +22,7 @@
- Add support for pattern completion of unsaved tuples. https://github.com/rescript-lang/rescript-vscode/pull/679
- Add support for completion in typed expressions. https://github.com/rescript-lang/rescript-vscode/pull/682
- Complete for `React.element` creator functions (`React.string` etc) when in JSX context. https://github.com/rescript-lang/rescript-vscode/pull/681
- Handle optional record fields in expression/pattern completion. https://github.com/rescript-lang/rescript-vscode/pull/691

#### :nail_care: Polish

Expand Down
4 changes: 3 additions & 1 deletion analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,9 @@ let rec resolveNested typ ~env ~package ~nested =
|> List.find_opt (fun (field : field) -> field.fname.txt = fieldName)
with
| None -> None
| Some {typ} -> typ |> resolveNested ~env ~package ~nested)
| Some {typ; optional} ->
let typ = if optional then Utils.unwrapIfOption typ else typ in
typ |> resolveNested ~env ~package ~nested)
| NRecordBody {seenFields}, Some (Trecord {env; typeExpr}) ->
Some (typeExpr, env, Some (Completable.RecordField {seenFields}))
| ( NVariantPayload {constructorName = "Some"; itemNum = 0},
Expand Down
21 changes: 18 additions & 3 deletions analysis/src/ProcessCmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@ let rec forTypeSignatureItem ~(env : SharedTypes.Env.t) ~(exported : Exported.t)
| Type_record (fields, _) ->
Record
(fields
|> List.map (fun {Types.ld_id; ld_type} ->
|> List.map (fun {Types.ld_id; ld_type; ld_attributes} ->
let astamp = Ident.binding_time ld_id in
let name = Ident.name ld_id in
{
stamp = astamp;
fname = Location.mknoloc name;
typ = ld_type;
optional =
Res_parsetree_viewer.hasOptionalAttribute
ld_attributes;
})));
}
~name ~stamp:(Ident.binding_time ident) ~env type_attributes
Expand Down Expand Up @@ -212,10 +215,22 @@ let forTypeDeclaration ~env ~(exported : Exported.t)
(fields
|> List.map
(fun
{Typedtree.ld_id; ld_name = fname; ld_type = {ctyp_type}}
{
Typedtree.ld_id;
ld_name = fname;
ld_type = {ctyp_type};
ld_attributes;
}
->
let fstamp = Ident.binding_time ld_id in
{stamp = fstamp; fname; typ = ctyp_type})));
{
stamp = fstamp;
fname;
typ = ctyp_type;
optional =
Res_parsetree_viewer.hasOptionalAttribute
ld_attributes;
})));
}
~name ~stamp ~env typ_attributes
(Exported.add exported Exported.Type)
Expand Down
7 changes: 6 additions & 1 deletion analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ module ModulePath = struct
loop modulePath [tipName]
end

type field = {stamp: int; fname: string Location.loc; typ: Types.type_expr}
type field = {
stamp: int;
fname: string Location.loc;
typ: Types.type_expr;
optional: bool;
}

module Constructor = struct
type t = {
Expand Down
12 changes: 12 additions & 0 deletions analysis/tests/src/CompletionExpressions.res
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,15 @@ let fnTakingOtherRecord = (r: otherRecord) => {

// let _ = fnTakingOtherRecord({otherField: })
// ^com

type recordWithOptionalField = {
someField: int,
someOptField?: bool,
}

let fnTakingRecordWithOptionalField = (r: recordWithOptionalField) => {
ignore(r)
}

// let _ = fnTakingRecordWithOptionalField({someOptField: })
// ^com
18 changes: 18 additions & 0 deletions analysis/tests/src/expected/CompletionExpressions.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,21 @@ Completable: Cexpression CArgument Value[fnTakingOtherRecord]($0)->recordField(o
"insertTextFormat": 2
}]

Complete src/CompletionExpressions.res 108:57
posCursor:[108:57] posNoWhite:[108:56] Found expr:[108:11->108:60]
Pexp_apply ...[108:11->108:42] (...[108:43->108:60])
Completable: Cexpression CArgument Value[fnTakingRecordWithOptionalField]($0)->recordField(someOptField)
[{
"label": "true",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}, {
"label": "false",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}]