Skip to content

Let ppat_any trigger completion in patterns #692

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 3 commits into from
Jan 12, 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 @@ -24,6 +24,7 @@
- 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
- Let `_` trigger completion in patterns. https://github.com/rescript-lang/rescript-vscode/pull/692

#### :nail_care: Polish

Expand Down
8 changes: 7 additions & 1 deletion analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
if locHasCursor pat.Parsetree.ppat_loc then Some v else None
in
match pat.ppat_desc with
| Ppat_any | Ppat_constant _ | Ppat_interval _ -> None
| Ppat_constant _ | Ppat_interval _ -> None
| Ppat_lazy p
| Ppat_constraint (p, _)
| Ppat_alias (p, _)
Expand All @@ -695,6 +695,12 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
match orPatWithItem with
| None when isPatternHole p1 || isPatternHole p2 -> Some ("", patternPath)
| v -> v)
| Ppat_any ->
(* We treat any `_` as an empty completion. This is mainly because we're
inserting `_` in snippets and automatically put the cursor there. So
letting it trigger an empty completion improves the ergonomics by a
lot. *)
someIfHasCursor ("", patternPath)
| Ppat_var {txt} -> someIfHasCursor (txt, patternPath)
| Ppat_construct ({txt = Lident "()"}, None) ->
(* switch s { | (<com>) }*)
Expand Down
3 changes: 3 additions & 0 deletions analysis/tests/src/CompletionPattern.res
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,6 @@ let s = (true, Some(true), [false])

// switch b { | #one | #three({test: true}, true | ) }
// ^com

// switch s { | (true, _, []) }
// ^com
33 changes: 33 additions & 0 deletions analysis/tests/src/expected/CompletionPattern.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -871,3 +871,36 @@ Completable: Cpattern Value[b]->polyvariantPayload::three($1)
"documentation": null
}]

Complete src/CompletionPattern.res 194:24
posCursor:[194:24] posNoWhite:[194:23] Found expr:[194:3->194:31]
posCursor:[194:24] posNoWhite:[194:23] Found pattern:[194:16->194:29]
posCursor:[194:24] posNoWhite:[194:23] Found pattern:[194:23->194:24]
Completable: Cpattern Value[s]->tuple($1)
[{
"label": "None",
"kind": 4,
"tags": [],
"detail": "bool",
"documentation": null
}, {
"label": "Some(_)",
"kind": 4,
"tags": [],
"detail": "bool",
"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
}]