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

complete unsaved tuples #679

Merged
merged 2 commits into from
Jan 6, 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
@@ -19,6 +19,7 @@
- Add snippet support in completion items. https://github.com/rescript-lang/rescript-vscode/pull/668
- Add support from completing polyvariants as values. https://github.com/rescript-lang/rescript-vscode/pull/669
- Add support for completion in patterns. https://github.com/rescript-lang/rescript-vscode/pull/670
- Add support for pattern completion of unsaved tuples. https://github.com/rescript-lang/rescript-vscode/pull/679

#### :nail_care: Polish

19 changes: 19 additions & 0 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
@@ -1458,6 +1458,25 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
})
| None -> [])
| None -> [])
| CTuple ctxPaths ->
(* Turn a list of context paths into a list of type expressions. *)
let typeExrps =
ctxPaths
|> List.map (fun contextPath ->
contextPath
|> getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles
~pos ~env ~exact:true ~scope)
|> List.filter_map (fun completionItems ->
match completionItems with
| {Completion.kind = Value typ} :: _ -> Some typ
| _ -> None)
in
if List.length ctxPaths = List.length typeExrps then
[
Completion.create ~name:"dummy" ~env
~kind:(Completion.Value (Ctype.newty (Ttuple typeExrps)));
]
else []

let getOpens ~debug ~rawOpens ~package ~env =
if debug && rawOpens <> [] then
5 changes: 5 additions & 0 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
@@ -260,6 +260,11 @@ let rec exprToContextPath (e : Parsetree.expression) =
match exprToContextPath e1 with
| None -> None
| Some contexPath -> Some (CPApply (contexPath, args |> List.map fst)))
| Pexp_tuple exprs ->
let exprsAsContextPaths = exprs |> List.filter_map exprToContextPath in
if List.length exprs = List.length exprsAsContextPaths then
Some (CTuple exprsAsContextPaths)
else None
| _ -> None

let rec getUnqualifiedName txt =
5 changes: 5 additions & 0 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
@@ -549,6 +549,7 @@ module Completable = struct
lhsLoc: Location.t;
(** The loc item for the left hand side of the pipe. *)
}
| CTuple of contextPath list

(** Additional context for a pattern completion where needed. *)
type patternContext = RecordField of {seenFields: string list}
@@ -648,6 +649,10 @@ module Completable = struct
| CPField (cp, s) -> contextPathToString cp ^ "." ^ str s
| CPObj (cp, s) -> contextPathToString cp ^ "[\"" ^ s ^ "\"]"
| CPPipe {contextPath; id} -> contextPathToString contextPath ^ "->" ^ id
| CTuple ctxPaths ->
"CTuple("
^ (ctxPaths |> List.map contextPathToString |> String.concat ", ")
^ ")"
in

function
5 changes: 5 additions & 0 deletions analysis/tests/src/CompletionExpressions.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let s = true
let f = Some([false])

// switch (s, f) { | }
// ^com
13 changes: 13 additions & 0 deletions analysis/tests/src/expected/CompletionExpressions.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Complete src/CompletionExpressions.res 3:20
XXX Not found!
Completable: Cpattern CTuple(Value[s], Value[f])
[{
"label": "(_, _)",
"kind": 12,
"tags": [],
"detail": "(bool, option<array<bool>>)",
"documentation": null,
"insertText": "(${1:_}, ${2:_})",
"insertTextFormat": 2
}]