Skip to content

Commit

Permalink
fix case when cursor is at start of record body but in no field
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed May 29, 2024
1 parent 85e9e86 commit 92de66d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
13 changes: 11 additions & 2 deletions analysis/src/CompletionExpressions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions analysis/tests/src/CompletionExpressions.res
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ let hook = (config: config) => {
}

let {name} = hook({
//
// ^com
// ope
// ^com
includeName: true,
Expand Down
24 changes: 21 additions & 3 deletions analysis/tests/src/expected/CompletionExpressions.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 92de66d

Please sign in to comment.