Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prettify optional fields in completion detail #737

Merged
merged 2 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

- Signature Help is now considered stable, and enabled for all users. Can still be turned off in settings.
- Show whether record fields and variant constructors are deprecated when completing. https://github.com/rescript-lang/rescript-vscode/pull/731
- Prettify how optional record fields are printed in the completion item detail. https://github.com/rescript-lang/rescript-vscode/pull/737

## 1.12.0

Expand Down
10 changes: 9 additions & 1 deletion analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,15 @@ let kindToDetail name (kind : Completion.kind) =
| Label typString -> typString
| Module _ -> "module"
| FileModule _ -> "file module"
| Field ({typ}, s) -> name ^ ": " ^ (typ |> Shared.typeToString) ^ "\n\n" ^ s
| Field ({typ; optional}, s) ->
(* Handle optional fields. Checking for "?" is because sometimes optional
fields are prefixed with "?" when completing, and at that point we don't
need to _also_ add a "?" after the field name, as that looks weird. *)
if optional && Utils.startsWith name "?" = false then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: little code duplication in the if and else branch, perhaps better to unify, perhaps not

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was being goal oriented 😉

name ^ "?: "
^ (typ |> Utils.unwrapIfOption |> Shared.typeToString)
^ "\n\n" ^ s
else name ^ ": " ^ (typ |> Shared.typeToString) ^ "\n\n" ^ s
| Constructor (c, s) -> showConstructor c ^ "\n\n" ^ s
| PolyvariantConstructor ({name; args}, s) ->
"#" ^ name
Expand Down
10 changes: 10 additions & 0 deletions analysis/tests/src/CompletionExpressions.res
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,13 @@ let something = {
Js.log(s)
// ^com
}

let fff: recordWithOptionalField = {
someField: 123,
someOptField: true,
}

ignore(fff)

// fff.someOpt
// ^com
12 changes: 12 additions & 0 deletions analysis/tests/src/expected/CompletionExpressions.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,15 @@ Completable: Cexpression CArgument Value[Js, log]($0)=s
"documentation": null
}]

Complete src/CompletionExpressions.res 196:14
posCursor:[196:14] posNoWhite:[196:13] Found expr:[196:3->196:14]
Pexp_field [196:3->196:6] someOpt:[196:7->196:14]
Completable: Cpath Value[fff].someOpt
[{
"label": "someOptField",
"kind": 5,
"tags": [],
"detail": "someOptField?: bool\n\ntype recordWithOptionalField = {\n someField: int,\n someOptField?: bool,\n}",
"documentation": null
}]