diff --git a/analysis/src/completion_expressions.ml b/analysis/src/completion_expressions.ml index 5a69c4bc84..85bc133412 100644 --- a/analysis/src/completion_expressions.ml +++ b/analysis/src/completion_expressions.ml @@ -47,6 +47,38 @@ let rec traverse_expr (exp : Parsetree.expression) ~expr_path ~pos `[None, , 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, ]` *) + 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": }` *) diff --git a/changes/array-spread-completion.fix.md b/changes/array-spread-completion.fix.md new file mode 100644 index 0000000000..e26ba9fe65 --- /dev/null +++ b/changes/array-spread-completion.fix.md @@ -0,0 +1 @@ +- Complete elements of an array literal with a spread, such as `[...xs, ]`, from the array's element type. diff --git a/tests/analysis_tests/tests/src/CompletionSpreadLiteral.res b/tests/analysis_tests/tests/src/CompletionSpreadLiteral.res new file mode 100644 index 0000000000..4a82907a7b --- /dev/null +++ b/tests/analysis_tests/tests/src/CompletionSpreadLiteral.res @@ -0,0 +1,52 @@ +type kind = First | Second + +let base: array = [First] +let baseList: list = list{First} +let takesArray = (xs: array) => ignore(xs) +let pair = (a: kind, b: kind) => [a, b] + +// let ys: array = [...base, ] +// ^com + +// let ys: array = [...base, Fi] +// ^com + +// let ys: array = [ ...base] +// ^com + +// let ys: array = [Se, ...base] +// ^com + +// let ys: array = [First, ...base, ] +// ^com + +// let ys: array = [...base, First, ...base, ] +// ^com + +// let ys: array = [...base, , ...base] +// ^com + +// let ys: array = [...base, Fi, ...base] +// ^com + +// let ys: array = [...base, ...[Se]] +// ^com + +// let ys: array = [First, ...ba] +// ^com + +// let ys: array = [...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 = list{...baseList, Fi, ...baseList} +// ^com diff --git a/tests/analysis_tests/tests/src/expected/CompletionSpreadLiteral.res.txt b/tests/analysis_tests/tests/src/expected/CompletionSpreadLiteral.res.txt new file mode 100644 index 0000000000..d6772b1262 --- /dev/null +++ b/tests/analysis_tests/tests/src/expected/CompletionSpreadLiteral.res.txt @@ -0,0 +1,513 @@ +Complete src/CompletionSpreadLiteral.res 7:35 +XXX Not found! +Completable: Cexpression array->array +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath array +ContextPath Type[kind] +Path kind +[ + { + "detail": "First", + "documentation": { + "kind": "markdown", + "value": "```rescript\nFirst\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "First", + "insertTextFormat": 2, + "kind": 4, + "label": "First", + "tags": [] + }, + { + "detail": "Second", + "documentation": { + "kind": "markdown", + "value": "```rescript\nSecond\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "Second", + "insertTextFormat": 2, + "kind": 4, + "label": "Second", + "tags": [] + } +] + +Complete src/CompletionSpreadLiteral.res 10:37 +XXX Not found! +Completable: Cexpression array=Fi->array +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath array +ContextPath Type[kind] +Path kind +[ + { + "detail": "First", + "documentation": { + "kind": "markdown", + "value": "```rescript\nFirst\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "First", + "insertTextFormat": 2, + "kind": 4, + "label": "First", + "sortText": "A First", + "tags": [] + }, + { "detail": "module File", "kind": 9, "label": "File", "tags": [] }, + { + "data": { + "modulePath": "Firebase", + "filePath": "src/CompletionSpreadLiteral.res" + }, + "detail": "module Firebase", + "kind": 9, + "label": "Firebase", + "tags": [] + }, + { + "data": { + "modulePath": "FirstClassModules", + "filePath": "src/CompletionSpreadLiteral.res" + }, + "detail": "module FirstClassModules", + "kind": 9, + "label": "FirstClassModules", + "tags": [] + } +] + +Complete src/CompletionSpreadLiteral.res 13:26 +XXX Not found! +Completable: Cexpression array->array +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath array +ContextPath Type[kind] +Path kind +[ + { + "detail": "First", + "documentation": { + "kind": "markdown", + "value": "```rescript\nFirst\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "First", + "insertTextFormat": 2, + "kind": 4, + "label": "First", + "tags": [] + }, + { + "detail": "Second", + "documentation": { + "kind": "markdown", + "value": "```rescript\nSecond\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "Second", + "insertTextFormat": 2, + "kind": 4, + "label": "Second", + "tags": [] + } +] + +Complete src/CompletionSpreadLiteral.res 16:28 +XXX Not found! +Completable: Cexpression array=Se->array +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath array +ContextPath Type[kind] +Path kind +[ + { + "detail": "Second", + "documentation": { + "kind": "markdown", + "value": "```rescript\nSecond\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "Second", + "insertTextFormat": 2, + "kind": 4, + "label": "Second", + "sortText": "A Second", + "tags": [] + }, + { "detail": "module Set", "kind": 9, "label": "Set", "tags": [] } +] + +Complete src/CompletionSpreadLiteral.res 19:42 +XXX Not found! +Completable: Cexpression array->array +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath array +ContextPath Type[kind] +Path kind +[ + { + "detail": "First", + "documentation": { + "kind": "markdown", + "value": "```rescript\nFirst\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "First", + "insertTextFormat": 2, + "kind": 4, + "label": "First", + "tags": [] + }, + { + "detail": "Second", + "documentation": { + "kind": "markdown", + "value": "```rescript\nSecond\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "Second", + "insertTextFormat": 2, + "kind": 4, + "label": "Second", + "tags": [] + } +] + +Complete src/CompletionSpreadLiteral.res 22:51 +XXX Not found! +Completable: Cexpression array->array +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath array +ContextPath Type[kind] +Path kind +[ + { + "detail": "First", + "documentation": { + "kind": "markdown", + "value": "```rescript\nFirst\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "First", + "insertTextFormat": 2, + "kind": 4, + "label": "First", + "tags": [] + }, + { + "detail": "Second", + "documentation": { + "kind": "markdown", + "value": "```rescript\nSecond\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "Second", + "insertTextFormat": 2, + "kind": 4, + "label": "Second", + "tags": [] + } +] + +Complete src/CompletionSpreadLiteral.res 25:34 +XXX Not found! +Completable: Cexpression array->array +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath array +ContextPath Type[kind] +Path kind +[ + { + "detail": "First", + "documentation": { + "kind": "markdown", + "value": "```rescript\nFirst\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "First", + "insertTextFormat": 2, + "kind": 4, + "label": "First", + "tags": [] + }, + { + "detail": "Second", + "documentation": { + "kind": "markdown", + "value": "```rescript\nSecond\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "Second", + "insertTextFormat": 2, + "kind": 4, + "label": "Second", + "tags": [] + } +] + +Complete src/CompletionSpreadLiteral.res 28:37 +XXX Not found! +Completable: Cexpression array=Fi->array +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath array +ContextPath Type[kind] +Path kind +[ + { + "detail": "First", + "documentation": { + "kind": "markdown", + "value": "```rescript\nFirst\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "First", + "insertTextFormat": 2, + "kind": 4, + "label": "First", + "sortText": "A First", + "tags": [] + }, + { "detail": "module File", "kind": 9, "label": "File", "tags": [] }, + { + "data": { + "modulePath": "Firebase", + "filePath": "src/CompletionSpreadLiteral.res" + }, + "detail": "module Firebase", + "kind": 9, + "label": "Firebase", + "tags": [] + }, + { + "data": { + "modulePath": "FirstClassModules", + "filePath": "src/CompletionSpreadLiteral.res" + }, + "detail": "module FirstClassModules", + "kind": 9, + "label": "FirstClassModules", + "tags": [] + } +] + +Complete src/CompletionSpreadLiteral.res 31:41 +XXX Not found! +Completable: Cexpression array=Se->array +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath array +ContextPath Type[kind] +Path kind +[ + { + "detail": "Second", + "documentation": { + "kind": "markdown", + "value": "```rescript\nSecond\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "Second", + "insertTextFormat": 2, + "kind": 4, + "label": "Second", + "sortText": "A Second", + "tags": [] + }, + { "detail": "module Set", "kind": 9, "label": "Set", "tags": [] } +] + +Complete src/CompletionSpreadLiteral.res 34:38 +XXX Not found! +Completable: Cexpression array=ba +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath array +ContextPath Type[kind] +Path kind +[ + { "detail": "list", "kind": 12, "label": "baseList", "tags": [] }, + { "detail": "array", "kind": 12, "label": "base", "tags": [] } +] + +Complete src/CompletionSpreadLiteral.res 37:41 +pos_cursor:[37:41] pos_no_white:[37:39] Found expr:[37:25->37:50] +pos_cursor:[37:41] pos_no_white:[37:39] Found expr:[37:29->37:42] +Pexp_apply ...[37:29->37:33] (...[37:34->37:39]) +Completable: Cexpression CArgument Value[pair]($1) +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath CArgument Value[pair]($1) +ContextPath Value[pair] +Path pair +[ + { + "detail": "First", + "documentation": { + "kind": "markdown", + "value": "```rescript\nFirst\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "First", + "insertTextFormat": 2, + "kind": 4, + "label": "First", + "tags": [] + }, + { + "detail": "Second", + "documentation": { + "kind": "markdown", + "value": "```rescript\nSecond\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "Second", + "insertTextFormat": 2, + "kind": 4, + "label": "Second", + "tags": [] + } +] + +Complete src/CompletionSpreadLiteral.res 40:32 +pos_cursor:[40:32] pos_no_white:[40:30] Found expr:[40:11->40:34] +Pexp_apply ...[40:11->40:21] (...[40:22->40:33]) +Completable: Cexpression CArgument Value[takesArray]($0)->array +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath CArgument Value[takesArray]($0) +ContextPath Value[takesArray] +Path takesArray +[ + { + "detail": "First", + "documentation": { + "kind": "markdown", + "value": "```rescript\nFirst\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "First", + "insertTextFormat": 2, + "kind": 4, + "label": "First", + "tags": [] + }, + { + "detail": "Second", + "documentation": { + "kind": "markdown", + "value": "```rescript\nSecond\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "Second", + "insertTextFormat": 2, + "kind": 4, + "label": "Second", + "tags": [] + } +] + +Complete src/CompletionSpreadLiteral.res 43:34 +pos_cursor:[43:34] pos_no_white:[43:33] Found expr:[43:11->43:45] +Pexp_apply ...[43:11->43:21] (...[43:22->43:44]) +Completable: Cexpression CArgument Value[takesArray]($0)=Se->array +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath CArgument Value[takesArray]($0) +ContextPath Value[takesArray] +Path takesArray +[ + { + "detail": "Second", + "documentation": { + "kind": "markdown", + "value": "```rescript\nSecond\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "insertText": "Second", + "insertTextFormat": 2, + "kind": 4, + "label": "Second", + "sortText": "A Second", + "tags": [] + }, + { "detail": "module Set", "kind": 9, "label": "Set", "tags": [] } +] + +Complete src/CompletionSpreadLiteral.res 46:24 +pos_cursor:[46:24] pos_no_white:[46:23] Found expr:[46:12->46:25] +pos_cursor:[46:24] pos_no_white:[46:23] Found expr:[46:22->46:24] +Pexp_construct Fi:[46:22->46:24] None +Completable: Cpath Value[Fi] +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath Value[Fi] +Path Fi +[ + { + "detail": "First", + "documentation": { + "kind": "markdown", + "value": "```rescript\nFirst\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "kind": 4, + "label": "First", + "tags": [] + }, + { "detail": "module File", "kind": 9, "label": "File", "tags": [] }, + { + "data": { + "modulePath": "Firebase", + "filePath": "src/CompletionSpreadLiteral.res" + }, + "detail": "module Firebase", + "kind": 9, + "label": "Firebase", + "tags": [] + }, + { + "data": { + "modulePath": "FirstClassModules", + "filePath": "src/CompletionSpreadLiteral.res" + }, + "detail": "module FirstClassModules", + "kind": 9, + "label": "FirstClassModules", + "tags": [] + } +] + +Complete src/CompletionSpreadLiteral.res 50:44 +pos_cursor:[50:44] pos_no_white:[50:43] Found expr:[50:24->50:58] +pos_cursor:[50:44] pos_no_white:[50:43] Found expr:[50:42->50:44] +Pexp_construct Fi:[50:42->50:44] None +Completable: Cpath Value[Fi] +Package opens Stdlib.place holder Pervasives.JsxModules.place holder +Resolved opens 1 Stdlib +ContextPath Value[Fi] +Path Fi +[ + { + "detail": "First", + "documentation": { + "kind": "markdown", + "value": "```rescript\nFirst\n```\n\n```rescript\ntype kind = First | Second\n```" + }, + "kind": 4, + "label": "First", + "tags": [] + }, + { "detail": "module File", "kind": 9, "label": "File", "tags": [] }, + { + "data": { + "modulePath": "Firebase", + "filePath": "src/CompletionSpreadLiteral.res" + }, + "detail": "module Firebase", + "kind": 9, + "label": "Firebase", + "tags": [] + }, + { + "data": { + "modulePath": "FirstClassModules", + "filePath": "src/CompletionSpreadLiteral.res" + }, + "detail": "module FirstClassModules", + "kind": 9, + "label": "FirstClassModules", + "tags": [] + } +] +