Skip to content

expand options in typed expr/pattern completion #690

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

Merged
merged 2 commits into from
Jan 11, 2023
Merged
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
@@ -23,6 +23,7 @@
- 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
- Expand options in completion to make working with options a bit more ergonomic. https://github.com/rescript-lang/rescript-vscode/pull/690

#### :nail_care: Polish

18 changes: 17 additions & 1 deletion analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
@@ -1796,7 +1796,7 @@ let printConstructorArgs argsLen ~asSnippet =
if List.length !args > 0 then "(" ^ (!args |> String.concat ", ") ^ ")"
else ""

let completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
let rec completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
~completionContext =
match t |> extractType ~env ~package:full.package with
| Some (Tbool env) ->
@@ -1849,6 +1849,21 @@ let completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
~env ())
|> filterItems ~prefix
| Some (Toption (env, t)) ->
let innerType = Utils.unwrapIfOption t in
let expandedCompletions =
innerType
|> completeTypedValue ~env ~full ~prefix ~completionContext
|> List.map (fun (c : Completion.t) ->
{
c with
name = "Some(" ^ c.name ^ ")";
sortText = None;
insertText =
(match c.insertText with
| None -> None
| Some insertText -> Some ("Some(" ^ insertText ^ ")"));
})
in
[
Completion.create ~name:"None"
~kind:(Label (t |> Shared.typeToString))
@@ -1857,6 +1872,7 @@ let completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
~kind:(Label (t |> Shared.typeToString))
~env ~insertText:"Some(${1:_})" ();
]
@ expandedCompletions
|> filterItems ~prefix
| Some (Tuple (env, exprs, typ)) ->
let numExprs = List.length exprs in
8 changes: 8 additions & 0 deletions analysis/tests/src/CompletionExpressions.res
Original file line number Diff line number Diff line change
@@ -108,3 +108,11 @@ let fnTakingRecordWithOptionalField = (r: recordWithOptionalField) => {

// let _ = fnTakingRecordWithOptionalField({someOptField: })
// ^com
type recordWithOptVariant = {someVariant: option<someVariant>}

let fnTakingRecordWithOptVariant = (r: recordWithOptVariant) => {
ignore(r)
}

// let _ = fnTakingRecordWithOptVariant({someVariant: })
// ^com
88 changes: 88 additions & 0 deletions analysis/tests/src/expected/CompletionExpressions.res.txt
Original file line number Diff line number Diff line change
@@ -167,6 +167,14 @@ Completable: Cexpression CArgument Value[fnTakingRecord]($0)->recordField(nested
"documentation": null,
"insertText": "Some(${1:_})",
"insertTextFormat": 2
}, {
"label": "Some({})",
"kind": 12,
"tags": [],
"detail": "otherRecord",
"documentation": null,
"insertText": "Some({$0})",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this intended in addition to the previous completion?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Logic being that you might want to do one of three things:

  • Add None
  • Add Some but with any
  • Add Some, plus whatever value for the nested type you're interested in

That's why I'm adding it in addition rather than fully replacing the completions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK seems sensible.

"insertTextFormat": 2
}]

Complete src/CompletionExpressions.res 44:46
@@ -328,6 +336,18 @@ Completable: Cexpression CArgument Value[fnTakingArray]($0)->array
"documentation": null,
"insertText": "Some(${1:_})",
"insertTextFormat": 2
}, {
"label": "Some(true)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}, {
"label": "Some(false)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}]

Complete src/CompletionExpressions.res 75:26
@@ -378,6 +398,18 @@ Completable: Cexpression CArgument Value[fnTakingArray]($0)->array
"documentation": null,
"insertText": "Some(${1:_})",
"insertTextFormat": 2
}, {
"label": "Some(true)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}, {
"label": "Some(false)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}]

Complete src/CompletionExpressions.res 84:31
@@ -398,6 +430,18 @@ Completable: Cexpression CArgument Value[fnTakingArray]($0)->array
"documentation": null,
"insertText": "Some(${1:_})",
"insertTextFormat": 2
}, {
"label": "Some(true)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}, {
"label": "Some(false)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}]

Complete src/CompletionExpressions.res 89:38
@@ -445,3 +489,47 @@ Completable: Cexpression CArgument Value[fnTakingRecordWithOptionalField]($0)->r
"documentation": null
}]

Complete src/CompletionExpressions.res 116:53
posCursor:[116:53] posNoWhite:[116:52] Found expr:[116:11->116:56]
Pexp_apply ...[116:11->116:39] (...[116:40->116:56])
Completable: Cexpression CArgument Value[fnTakingRecordWithOptVariant]($0)->recordField(someVariant)
[{
"label": "None",
"kind": 4,
"tags": [],
"detail": "someVariant",
"documentation": null
}, {
"label": "Some(_)",
"kind": 4,
"tags": [],
"detail": "someVariant",
"documentation": null,
"insertText": "Some(${1:_})",
"insertTextFormat": 2
}, {
"label": "Some(One)",
"kind": 4,
"tags": [],
"detail": "One\n\ntype someVariant = One | Two | Three(int, string)",
"documentation": null,
"insertText": "Some(One)",
"insertTextFormat": 2
}, {
"label": "Some(Two)",
"kind": 4,
"tags": [],
"detail": "Two\n\ntype someVariant = One | Two | Three(int, string)",
"documentation": null,
"insertText": "Some(Two)",
"insertTextFormat": 2
}, {
"label": "Some(Three(_, _))",
"kind": 4,
"tags": [],
"detail": "Three(int, string)\n\ntype someVariant = One | Two | Three(int, string)",
"documentation": null,
"insertText": "Some(Three(${1:_}, ${2:_}))",
"insertTextFormat": 2
}]

60 changes: 60 additions & 0 deletions analysis/tests/src/expected/CompletionPattern.res.txt
Original file line number Diff line number Diff line change
@@ -497,6 +497,18 @@ Completable: Cpattern Value[p]->variantPayload::Test($2)
"documentation": null,
"insertText": "Some(${1:_})",
"insertTextFormat": 2
}, {
"label": "Some(true)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}, {
"label": "Some(false)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}]

Complete src/CompletionPattern.res 140:23
@@ -573,6 +585,18 @@ Completable: Cpattern Value[v]->polyvariantPayload::test($2)
"documentation": null,
"insertText": "Some(${1:_})",
"insertTextFormat": 2
}, {
"label": "Some(true)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}, {
"label": "Some(false)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}]

Complete src/CompletionPattern.res 156:24
@@ -647,6 +671,18 @@ Completable: Cpattern Value[s]->tuple($1)
"documentation": null,
"insertText": "Some(${1:_})",
"insertTextFormat": 2
}, {
"label": "Some(true)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}, {
"label": "Some(false)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}]

Complete src/CompletionPattern.res 170:22
@@ -667,6 +703,18 @@ Completable: Cpattern Value[s]->tuple($1)
"documentation": null,
"insertText": "Some(${1:_})",
"insertTextFormat": 2
}, {
"label": "Some(true)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}, {
"label": "Some(false)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}]

Complete src/CompletionPattern.res 173:35
@@ -700,6 +748,18 @@ Completable: Cpattern Value[s]->tuple($1)
"documentation": null,
"insertText": "Some(${1:_})",
"insertTextFormat": 2
}, {
"label": "Some(true)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}, {
"label": "Some(false)",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}]

Complete src/CompletionPattern.res 179:21