Skip to content

Commit 2e3d668

Browse files
committed
prettify optional fields in completion detail
1 parent da0841c commit 2e3d668

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

analysis/src/CompletionBackEnd.ml

+9-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,15 @@ let kindToDetail name (kind : Completion.kind) =
221221
| Label typString -> typString
222222
| Module _ -> "module"
223223
| FileModule _ -> "file module"
224-
| Field ({typ}, s) -> name ^ ": " ^ (typ |> Shared.typeToString) ^ "\n\n" ^ s
224+
| Field ({typ; optional}, s) ->
225+
(* Handle optional fields. Checking for "?" is because sometimes optional
226+
fields are prefixed with "?" when completing, and at that point we don't
227+
need to _also_ add a "?" after the field name, as that looks weird. *)
228+
if optional && Utils.startsWith name "?" = false then
229+
name ^ "?: "
230+
^ (typ |> Utils.unwrapIfOption |> Shared.typeToString)
231+
^ "\n\n" ^ s
232+
else name ^ ": " ^ (typ |> Shared.typeToString) ^ "\n\n" ^ s
225233
| Constructor (c, s) -> showConstructor c ^ "\n\n" ^ s
226234
| PolyvariantConstructor ({name; args}, s) ->
227235
"#" ^ name

analysis/tests/src/CompletionExpressions.res

+10
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,13 @@ let something = {
186186
Js.log(s)
187187
// ^com
188188
}
189+
190+
let fff: recordWithOptionalField = {
191+
someField: 123,
192+
someOptField: true,
193+
}
194+
195+
ignore(fff)
196+
197+
// fff.someOpt
198+
// ^com

analysis/tests/src/expected/CompletionExpressions.res.txt

+12
Original file line numberDiff line numberDiff line change
@@ -747,3 +747,15 @@ Completable: Cexpression CArgument Value[Js, log]($0)=s
747747
"documentation": null
748748
}]
749749

750+
Complete src/CompletionExpressions.res 196:14
751+
posCursor:[196:14] posNoWhite:[196:13] Found expr:[196:3->196:14]
752+
Pexp_field [196:3->196:6] someOpt:[196:7->196:14]
753+
Completable: Cpath Value[fff].someOpt
754+
[{
755+
"label": "someOptField",
756+
"kind": 5,
757+
"tags": [],
758+
"detail": "someOptField?: bool\n\ntype recordWithOptionalField = {\n someField: int,\n someOptField?: bool,\n}",
759+
"documentation": null
760+
}]
761+

0 commit comments

Comments
 (0)