Skip to content

Commit 272200f

Browse files
committed
print exotic identifiers properly when completing polyvariants
1 parent 28a25f9 commit 272200f

8 files changed

+59
-9
lines changed

analysis/src/CompletionBackEnd.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ let kindToDetail name (kind : Completion.kind) =
231231
^ "\n\n" ^ s
232232
else name ^ ": " ^ (typ |> Shared.typeToString) ^ "\n\n" ^ s
233233
| Constructor (c, s) -> showConstructor c ^ "\n\n" ^ s
234-
| PolyvariantConstructor ({name; args}, s) ->
235-
"#" ^ name
234+
| PolyvariantConstructor ({displayName; args}, s) ->
235+
"#" ^ displayName
236236
^ (match args with
237237
| [] -> ""
238238
| typeExprs ->
@@ -1223,13 +1223,13 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
12231223
|> List.map (fun (constructor : polyVariantConstructor) ->
12241224
Completion.createWithSnippet
12251225
~name:
1226-
("#" ^ constructor.name
1226+
("#" ^ constructor.displayName
12271227
^ printConstructorArgs
12281228
(List.length constructor.args)
12291229
~asSnippet:false)
12301230
~insertText:
12311231
((if Utils.startsWith prefix "#" then "" else "#")
1232-
^ constructor.name
1232+
^ constructor.displayName
12331233
^ printConstructorArgs
12341234
(List.length constructor.args)
12351235
~asSnippet:true)
@@ -1771,7 +1771,7 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
17711771
~cases:
17721772
(v.constructors
17731773
|> List.map (fun (constructor : polyVariantConstructor) ->
1774-
"#" ^ constructor.name
1774+
"#" ^ constructor.displayName
17751775
^
17761776
match constructor.args with
17771777
| [] -> ""

analysis/src/SharedTypes.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ end = struct
311311
{env with exported = structure.exported; pathRev; parent = Some env}
312312
end
313313

314-
type polyVariantConstructor = {name: string; args: Types.type_expr list}
314+
type polyVariantConstructor = {
315+
name: string;
316+
displayName: string;
317+
args: Types.type_expr list;
318+
}
315319

316320
type innerType = TypeExpr of Types.type_expr | ExtractedType of completionType
317321
and completionType =

analysis/src/TypeUtils.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ let rec extractType ~env ~package (t : Types.type_expr) =
157157
|> List.map (fun (label, field) ->
158158
{
159159
name = label;
160+
displayName = Utils.printMaybeExoticIdent ~allowUident:true label;
160161
args =
161162
(* Multiple arguments are represented as a Ttuple, while a single argument is just the type expression itself. *)
162163
(match field with
@@ -691,7 +692,7 @@ module Codegen = struct
691692
(match c.args with
692693
| [] -> None
693694
| _ -> Some (any ()))
694-
c.name))
695+
c.displayName))
695696
| Toption (_, innerType) ->
696697
let extractedType =
697698
match innerType with

analysis/src/Utils.ml

+17
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,20 @@ let rec flattenAnyNamespaceInPath path =
243243
(* Namespaces are in reverse order, so "URL-RescriptBun" where RescriptBun is the namespace. *)
244244
(parts |> List.rev) @ flattenAnyNamespaceInPath tail
245245
else head :: flattenAnyNamespaceInPath tail
246+
247+
let printMaybeExoticIdent ?(allowUident = false) txt =
248+
let len = String.length txt in
249+
250+
let rec loop i =
251+
if i == len then txt
252+
else if i == 0 then
253+
match String.unsafe_get txt i with
254+
| 'A' .. 'Z' when allowUident -> loop (i + 1)
255+
| 'a' .. 'z' | '_' -> loop (i + 1)
256+
| _ -> "\"" ^ txt ^ "\""
257+
else
258+
match String.unsafe_get txt i with
259+
| 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '\'' | '_' -> loop (i + 1)
260+
| _ -> "\"" ^ txt ^ "\""
261+
in
262+
loop 0

analysis/tests/src/CompletionExpressions.res

+9
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,12 @@ let arr = ["hello"]
262262

263263
// arr->Belt.Array.map()
264264
// ^com
265+
266+
type exoticPolyvariant = [#"some exotic"]
267+
268+
let takesExotic = (e: exoticPolyvariant) => {
269+
ignore(e)
270+
}
271+
272+
// takesExotic()
273+
// ^com

analysis/tests/src/ExhaustiveSwitch.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
type someVariant = One | Two | Three(option<bool>)
2-
type somePolyVariant = [#one | #two | #three(option<bool>)]
2+
type somePolyVariant = [#one | #two | #three(option<bool>) | #"exotic ident"]
33

44
let withSomeVariant = One
55
let withSomePoly: somePolyVariant = #one

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

+19
Original file line numberDiff line numberDiff line change
@@ -1113,3 +1113,22 @@ Path Belt.Array.map
11131113
"insertTextFormat": 2
11141114
}]
11151115

1116+
Complete src/CompletionExpressions.res 271:15
1117+
posCursor:[271:15] posNoWhite:[271:14] Found expr:[271:3->271:16]
1118+
Pexp_apply ...[271:3->271:14] (...[271:15->271:16])
1119+
Completable: Cexpression CArgument Value[takesExotic]($0)
1120+
Package opens Pervasives.JsxModules.place holder
1121+
Resolved opens 1 pervasives
1122+
ContextPath CArgument Value[takesExotic]($0)
1123+
ContextPath Value[takesExotic]
1124+
Path takesExotic
1125+
[{
1126+
"label": "#\"some exotic\"",
1127+
"kind": 4,
1128+
"tags": [],
1129+
"detail": "#\"some exotic\"\n\n[#\"some exotic\"]",
1130+
"documentation": null,
1131+
"insertText": "#\"some exotic\"",
1132+
"insertTextFormat": 2
1133+
}]
1134+

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Path withSomePol
4242
"detail": "insert exhaustive switch for value",
4343
"documentation": null,
4444
"filterText": "withSomePoly",
45-
"insertText": "withSomePoly {\n | #one => ${1:failwith(\"todo\")}\n | #three(_) => ${2:failwith(\"todo\")}\n | #two => ${3:failwith(\"todo\")}\n }",
45+
"insertText": "withSomePoly {\n | #one => ${1:failwith(\"todo\")}\n | #three(_) => ${2:failwith(\"todo\")}\n | #two => ${3:failwith(\"todo\")}\n | #\"exotic ident\" => ${4:failwith(\"todo\")}\n }",
4646
"insertTextFormat": 2
4747
}]
4848

0 commit comments

Comments
 (0)