Skip to content
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
32 changes: 32 additions & 0 deletions analysis/src/completion_expressions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,38 @@ let rec traverse_expr (exp : Parsetree.expression) ~expr_path ~pos
`[None, <com>, None]`) *)
Some ("", next_expr_path)
| _ -> None)
| Pexp_spread (Spread_array, elements) -> (
(* Complete an item like an element of an array literal, and a spread
operand like the whole literal, whose type it has. *)
let next_expr_path = [Completable.NArray] @ expr_path in
let item_with_cursor =
elements
|> List.find_map (function
| Parsetree.Pspread_item e ->
e
|> traverse_expr ~expr_path:next_expr_path
~first_char_before_cursor_no_white ~pos
| Pspread e ->
e |> traverse_expr ~expr_path ~first_char_before_cursor_no_white ~pos)
in
match item_with_cursor with
| Some _ -> item_with_cursor
| None
when loc_has_cursor exp.pexp_loc
&& (first_char_before_cursor_no_white = Some ','
|| first_char_before_cursor_no_white = Some '[')
&& not
(elements
|> List.exists (function
| Parsetree.Pspread e | Pspread_item e ->
loc_has_cursor e.pexp_loc)) ->
(* A new item at the start or after a comma, e.g. `[...xs, <com>]` *)
Some ("", next_expr_path)
| None -> None)
| Pexp_spread (Spread_list, _) ->
(* Like a list literal without a spread, a chain of [::], which gets no
typed completion either: the element type of a list is not extracted. *)
None
| Pexp_dict entries -> (
(* Complete inside the value of the entry with the cursor, or for the type
of the values at an expression hole, e.g. `dict{"a": <com>}` *)
Expand Down
1 change: 1 addition & 0 deletions changes/array-spread-completion.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Complete elements of an array literal with a spread, such as `[...xs, <cursor>]`, from the array's element type.
52 changes: 52 additions & 0 deletions tests/analysis_tests/tests/src/CompletionSpreadLiteral.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
type kind = First | Second

let base: array<kind> = [First]
let baseList: list<kind> = list{First}
let takesArray = (xs: array<kind>) => ignore(xs)
let pair = (a: kind, b: kind) => [a, b]

// let ys: array<kind> = [...base, ]
// ^com

// let ys: array<kind> = [...base, Fi]
// ^com

// let ys: array<kind> = [ ...base]
// ^com

// let ys: array<kind> = [Se, ...base]
// ^com

// let ys: array<kind> = [First, ...base, ]
// ^com

// let ys: array<kind> = [...base, First, ...base, ]
// ^com

// let ys: array<kind> = [...base, , ...base]
// ^com

// let ys: array<kind> = [...base, Fi, ...base]
// ^com

// let ys: array<kind> = [...base, ...[Se]]
// ^com

// let ys: array<kind> = [First, ...ba]
// ^com

// let ys: array<kind> = [...pair(First, ), First]
// ^com

// let _ = takesArray([...base, ])
// ^com

// let _ = takesArray([...base, Se, ...base])
// ^com

// let ys = [...base, Fi]
// ^com

// A list literal gets no typed completion, with or without a spread
// let ys: list<kind> = list{...baseList, Fi, ...baseList}
// ^com
Loading
Loading