diff --git a/CHANGELOG.md b/CHANGELOG.md index 22fabea6d..619a2afe9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - Fix JSX prop special case in end of JSX element. https://github.com/rescript-lang/rescript-vscode/pull/984 - preserve URI format in canonicalizeUri. https://github.com/rescript-lang/rescript-vscode/pull/990 - Remove workaround for canonicalize function in tests https://github.com/rescript-lang/rescript-vscode/pull/992 +- Get completions for writing new field names in a record body expressions in more cases. https://github.com/rescript-lang/rescript-vscode/pull/997 #### :nail_care: Polish diff --git a/analysis/src/CompletionExpressions.ml b/analysis/src/CompletionExpressions.ml index d000294da..8b5b233e3 100644 --- a/analysis/src/CompletionExpressions.ml +++ b/analysis/src/CompletionExpressions.ml @@ -84,6 +84,10 @@ let rec traverseExpr (exp : Parsetree.expression) ~exprPath ~pos (* An expression hole means for example `{someField: }`. We want to complete for the type of `someField`. *) someIfHasCursor ("", [Completable.NFollowRecordField {fieldName = fname}] @ exprPath) + | Pexp_ident {txt = Lident txt} when fname = txt -> + (* This is a heuristic for catching writing field names. ReScript has punning for record fields, but the AST doesn't, + so punning is represented as the record field name and identifier being the same: {someField}. *) + someIfHasCursor (txt, [Completable.NRecordBody {seenFields}] @ exprPath) | Pexp_ident {txt = Lident txt} -> (* A var means `{someField: s}` or similar. Complete for identifiers or values. *) someIfHasCursor (txt, exprPath) @@ -94,12 +98,21 @@ let rec traverseExpr (exp : Parsetree.expression) ~exprPath ~pos ([Completable.NFollowRecordField {fieldName = fname}] @ exprPath) ) | None, None -> ( + if Debug.verbose () then ( + Printf.printf "[traverse_expr] No field with cursor and no expr hole.\n"; + + match firstCharBeforeCursorNoWhite with + | None -> () + | Some c -> + Printf.printf "[traverse_expr] firstCharBeforeCursorNoWhite: %c.\n" c); + (* Figure out if we're completing for a new field. If the cursor is inside of the record body, but no field has the cursor, and there's no pattern hole. Check the first char to the left of the cursor, - ignoring white space. If that's a comma, we assume you're completing for a new field. *) + ignoring white space. If that's a comma or {, we assume you're completing for a new field, + since you're either between 2 fields (comma to the left) or at the start of the record ({). *) match firstCharBeforeCursorNoWhite with - | Some ',' -> + | Some (',' | '{') -> someIfHasCursor ("", [Completable.NRecordBody {seenFields}] @ exprPath) | _ -> None)) | Pexp_construct diff --git a/analysis/tests/src/CompletionExpressions.res b/analysis/tests/src/CompletionExpressions.res index 78186f18d..8dbff050a 100644 --- a/analysis/tests/src/CompletionExpressions.res +++ b/analysis/tests/src/CompletionExpressions.res @@ -362,3 +362,26 @@ let someTyp: someTyp = {test: true} // switch someTyp. { | _ => () } // ^com + +type config = { + includeName: bool, + operator?: [#"and" | #or], + showMore: bool, +} + +type hookReturn = {name: string} + +let hook = (config: config) => { + ignore(config) + { + name: "tester", + } +} + +let {name} = hook({ + // ^com + // ope + // ^com + includeName: true, + showMore: true, +}) diff --git a/analysis/tests/src/expected/CompletionExpressions.res.txt b/analysis/tests/src/expected/CompletionExpressions.res.txt index 1cddba3b7..938f96191 100644 --- a/analysis/tests/src/expected/CompletionExpressions.res.txt +++ b/analysis/tests/src/expected/CompletionExpressions.res.txt @@ -1423,3 +1423,39 @@ Path someTyp "documentation": {"kind": "markdown", "value": "```rescript\ntest: bool\n```\n\n```rescript\ntype someTyp = {test: bool}\n```"} }] +Complete src/CompletionExpressions.res 380:22 +posCursor:[380:22] posNoWhite:[380:18] Found expr:[380:13->386:2] +Pexp_apply ...[380:13->380:17] (...[380:18->386:1]) +Completable: Cexpression CArgument Value[hook]($0)->recordBody +Raw opens: 1 CompletionSupport.place holder +Package opens Pervasives.JsxModules.place holder +Resolved opens 2 pervasives CompletionSupport.res +ContextPath CArgument Value[hook]($0) +ContextPath Value[hook] +Path hook +[{ + "label": "operator", + "kind": 5, + "tags": [], + "detail": "[#\"and\" | #or]", + "documentation": {"kind": "markdown", "value": "```rescript\noperator?: [#\"and\" | #or]\n```\n\n```rescript\ntype config = {includeName: bool, operator: option<[#\"and\" | #or]>, showMore: bool}\n```"} + }] + +Complete src/CompletionExpressions.res 382:8 +posCursor:[382:8] posNoWhite:[382:7] Found expr:[380:13->386:2] +Pexp_apply ...[380:13->380:17] (...[380:18->386:1]) +Completable: Cexpression CArgument Value[hook]($0)=ope->recordBody +Raw opens: 1 CompletionSupport.place holder +Package opens Pervasives.JsxModules.place holder +Resolved opens 2 pervasives CompletionSupport.res +ContextPath CArgument Value[hook]($0) +ContextPath Value[hook] +Path hook +[{ + "label": "operator", + "kind": 5, + "tags": [], + "detail": "[#\"and\" | #or]", + "documentation": {"kind": "markdown", "value": "```rescript\noperator?: [#\"and\" | #or]\n```\n\n```rescript\ntype config = {includeName: bool, operator: option<[#\"and\" | #or]>, showMore: bool}\n```"} + }] +