Skip to content

Commit

Permalink
Fix expr record field completion in existing records (#997)
Browse files Browse the repository at this point in the history
* fix case in expr record field completion where the record body context was not picked up

* fix case when cursor is at start of record body but in no field

* changelog

* dont use empty line
  • Loading branch information
zth authored May 29, 2024
1 parent e0a1815 commit 4b555e5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 15 additions & 2 deletions analysis/src/CompletionExpressions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ let rec traverseExpr (exp : Parsetree.expression) ~exprPath ~pos
(* An expression hole means for example `{someField: <com>}`. 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)
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions analysis/tests/src/CompletionExpressions.res
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
36 changes: 36 additions & 0 deletions analysis/tests/src/expected/CompletionExpressions.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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```"}
}]

0 comments on commit 4b555e5

Please sign in to comment.