From 92de66d2e25ebe6469705f8168a9661783b58b45 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Wed, 29 May 2024 13:33:38 +0200 Subject: [PATCH] fix case when cursor is at start of record body but in no field --- analysis/src/CompletionExpressions.ml | 13 ++++++++-- analysis/tests/src/CompletionExpressions.res | 2 ++ .../expected/CompletionExpressions.res.txt | 24 ++++++++++++++++--- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/analysis/src/CompletionExpressions.ml b/analysis/src/CompletionExpressions.ml index f68e38212..8b5b233e3 100644 --- a/analysis/src/CompletionExpressions.ml +++ b/analysis/src/CompletionExpressions.ml @@ -98,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 807121432..2eb282746 100644 --- a/analysis/tests/src/CompletionExpressions.res +++ b/analysis/tests/src/CompletionExpressions.res @@ -379,6 +379,8 @@ let hook = (config: config) => { } let {name} = hook({ + // + // ^com // ope // ^com includeName: true, diff --git a/analysis/tests/src/expected/CompletionExpressions.res.txt b/analysis/tests/src/expected/CompletionExpressions.res.txt index 8e2ab69f0..9b6962898 100644 --- a/analysis/tests/src/expected/CompletionExpressions.res.txt +++ b/analysis/tests/src/expected/CompletionExpressions.res.txt @@ -1423,9 +1423,27 @@ 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]) +Complete src/CompletionExpressions.res 381:5 +posCursor:[381:5] posNoWhite:[381:-2] Found expr:[380:13->387:2] +Pexp_apply ...[380:13->380:17] (...[380:18->387: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 383:8 +posCursor:[383:8] posNoWhite:[383:7] Found expr:[380:13->387:2] +Pexp_apply ...[380:13->380:17] (...[380:18->387:1]) Completable: Cexpression CArgument Value[hook]($0)=ope->recordBody Raw opens: 1 CompletionSupport.place holder Package opens Pervasives.JsxModules.place holder