From 85e9e868c2568c8291f322a37be6753cd1211a57 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Wed, 29 May 2024 13:24:23 +0200 Subject: [PATCH] fix case in expr record field completion where the record body context was not picked up --- analysis/src/CompletionExpressions.ml | 4 ++++ analysis/tests/src/CompletionExpressions.res | 22 +++++++++++++++++++ .../expected/CompletionExpressions.res.txt | 18 +++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/analysis/src/CompletionExpressions.ml b/analysis/src/CompletionExpressions.ml index d000294da..f68e38212 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) diff --git a/analysis/tests/src/CompletionExpressions.res b/analysis/tests/src/CompletionExpressions.res index 78186f18d..807121432 100644 --- a/analysis/tests/src/CompletionExpressions.res +++ b/analysis/tests/src/CompletionExpressions.res @@ -362,3 +362,25 @@ 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({ + // 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..8e2ab69f0 100644 --- a/analysis/tests/src/expected/CompletionExpressions.res.txt +++ b/analysis/tests/src/expected/CompletionExpressions.res.txt @@ -1423,3 +1423,21 @@ Path someTyp "documentation": {"kind": "markdown", "value": "```rescript\ntest: bool\n```\n\n```rescript\ntype someTyp = {test: bool}\n```"} }] +Complete src/CompletionExpressions.res 381:8 +posCursor:[381:8] posNoWhite:[381:7] Found expr:[380:13->385:2] +Pexp_apply ...[380:13->380:17] (...[380:18->385: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```"} + }] +