Skip to content

Commit 1e1f4ba

Browse files
committed
tweak logic some and break out functionality to own function
1 parent 1799bc4 commit 1e1f4ba

File tree

6 files changed

+115
-42
lines changed

6 files changed

+115
-42
lines changed

Diff for: analysis/src/CompletionBackEnd.ml

+7-36
Original file line numberDiff line numberDiff line change
@@ -1342,45 +1342,13 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
13421342
]
13431343
else []
13441344
| Tfunction {env; typ; args} when prefix = "" && mode = Expression ->
1345-
let prettyPrintArgTyp ?currentIndex (argTyp : Types.type_expr) =
1346-
let indexText =
1347-
match currentIndex with
1348-
| None -> ""
1349-
| Some i -> string_of_int i
1350-
in
1351-
match argTyp |> TypeUtils.pathFromTypeExpr with
1352-
| None -> "v" ^ indexText
1353-
| Some p -> (
1354-
(* Pretty print a few common patterns. *)
1355-
match Path.head p |> Ident.name with
1356-
| "unit" -> "()"
1357-
| "ReactEvent" | "JsxEvent" -> "event"
1358-
| _ ->
1359-
let defaultVarName = "v" ^ indexText in
1360-
let txt =
1361-
match
1362-
TypeUtils.templateVarNameForTyp ~env ~package:full.package argTyp
1363-
with
1364-
| Some txt -> txt
1365-
| None -> (
1366-
match p |> Utils.expandPath |> List.rev |> Utils.lastElements with
1367-
| [] | ["t"] -> defaultVarName
1368-
| [someName; "t"] | [_; someName] | [someName] -> (
1369-
match someName with
1370-
| "string" | "int" | "float" | "array" | "option" | "bool" ->
1371-
defaultVarName
1372-
| someName when String.length someName < 30 ->
1373-
someName |> Utils.lowercaseFirstChar
1374-
| _ -> defaultVarName)
1375-
| _ -> defaultVarName)
1376-
in
1377-
txt)
1378-
in
13791345
let mkFnArgs ~asSnippet =
13801346
match args with
13811347
| [(Nolabel, argTyp)] when TypeUtils.typeIsUnit argTyp -> "()"
13821348
| [(Nolabel, argTyp)] ->
1383-
let varName = prettyPrintArgTyp argTyp in
1349+
let varName =
1350+
CompletionExpressions.prettyPrintFnTemplateArgName ~env ~full argTyp
1351+
in
13841352
if asSnippet then "${1:" ^ varName ^ "}" else varName
13851353
| _ ->
13861354
let currentUnlabelledIndex = ref 0 in
@@ -1395,7 +1363,10 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
13951363
else (
13961364
currentUnlabelledIndex := !currentUnlabelledIndex + 1;
13971365
let num = !currentUnlabelledIndex in
1398-
let varName = prettyPrintArgTyp typ ~currentIndex:num in
1366+
let varName =
1367+
CompletionExpressions.prettyPrintFnTemplateArgName
1368+
~currentIndex:num ~env ~full typ
1369+
in
13991370
if asSnippet then
14001371
"${" ^ string_of_int num ^ ":" ^ varName ^ "}"
14011372
else varName))

Diff for: analysis/src/CompletionExpressions.ml

+36
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,39 @@ and traverseExprTupleItems tupleItems ~nextExprPath ~resultFromFoundItemNum ~pos
216216
if pos >= Loc.start e.Parsetree.pexp_loc then posNum := index);
217217
if !posNum > -1 then Some ("", resultFromFoundItemNum !posNum) else None
218218
| v, _ -> v
219+
220+
let prettyPrintFnTemplateArgName ?currentIndex ~env ~full
221+
(argTyp : Types.type_expr) =
222+
let indexText =
223+
match currentIndex with
224+
| None -> ""
225+
| Some i -> string_of_int i
226+
in
227+
let defaultVarName = "v" ^ indexText in
228+
let argTyp, suffix, env =
229+
TypeUtils.digToRelevantTemplateNameType ~env ~package:full.package argTyp
230+
in
231+
match TypeUtils.templateVarNameForTyp ~env ~package:full.package argTyp with
232+
| Some txt -> txt
233+
| None -> (
234+
match argTyp |> TypeUtils.pathFromTypeExpr with
235+
| None -> defaultVarName
236+
| Some p -> (
237+
match p |> Utils.expandPath |> List.rev |> Utils.lastElements with
238+
| [] | ["t"] -> defaultVarName
239+
| ["unit"] -> "()"
240+
(* Special treatment for JsxEvent, since that's a common enough thing
241+
used in event handlers. *)
242+
| ["JsxEvent"; "synthetic"] -> "event"
243+
| ["synthetic"] when env.file.moduleName = "JsxEvent" -> "event"
244+
(* Ignore `t` types, and go for its module name instead. *)
245+
| [someName; "t"] | [_; someName] | [someName] -> (
246+
match someName with
247+
| "string" | "int" | "float" | "array" | "option" | "bool" ->
248+
defaultVarName
249+
| someName when String.length someName < 30 ->
250+
(* We cap how long the name can be, so we don't end up with super
251+
long type names. *)
252+
(someName |> Utils.lowercaseFirstChar) ^ suffix
253+
| _ -> defaultVarName)
254+
| _ -> defaultVarName))

Diff for: analysis/src/TypeUtils.ml

+18-2
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,27 @@ let rec templateVarNameForTyp ~env ~package (t : Types.type_expr) =
208208
templateVarNameForTyp ~env ~package t1
209209
| Tconstr (path, _, _) -> (
210210
match References.digConstructor ~env ~package path with
211-
| Some (_env, {item = {attributes}}) -> (
212-
ProcessAttributes.findTemplateVarNameAttribute attributes)
211+
| Some (_env, {item = {attributes}}) ->
212+
ProcessAttributes.findTemplateVarNameAttribute attributes
213213
| _ -> None)
214214
| _ -> None
215215

216+
let rec digToRelevantTemplateNameType ~env ~package ?(suffix = "")
217+
(t : Types.type_expr) =
218+
match t.desc with
219+
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) ->
220+
digToRelevantTemplateNameType ~suffix ~env ~package t1
221+
| Tconstr (Path.Pident {name = "option"}, [t1], _) ->
222+
digToRelevantTemplateNameType ~suffix ~env ~package t1
223+
| Tconstr (Path.Pident {name = "array"}, [t1], _) ->
224+
digToRelevantTemplateNameType ~suffix:"s" ~env ~package t1
225+
| Tconstr (path, _, _) -> (
226+
match References.digConstructor ~env ~package path with
227+
| Some (env, {item = {decl = {type_manifest = Some typ}}}) ->
228+
digToRelevantTemplateNameType ~suffix ~env ~package typ
229+
| _ -> (t, suffix, env))
230+
| _ -> (t, suffix, env)
231+
216232
let rec resolveTypeForPipeCompletion ~env ~package ~lhsLoc ~full
217233
(t : Types.type_expr) =
218234
let builtin =

Diff for: analysis/src/Utils.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ end
207207

208208
let rec lastElements list =
209209
match list with
210-
| ([_] | [_; _] | []) as res -> res
210+
| ([_; _] | [_] | []) as res -> res
211211
| _ :: tl -> lastElements tl
212212

213213
let lowercaseFirstChar s =

Diff for: analysis/tests/src/CompletionExpressions.res

+14
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,20 @@ let takesCb3 = cb => {
227227
// takesCb3()
228228
// ^com
229229

230+
let takesCb4 = cb => {
231+
cb(Some({hi: true}))
232+
}
233+
234+
// takesCb4()
235+
// ^com
236+
237+
let takesCb5 = cb => {
238+
cb([Some({hi: true})])
239+
}
240+
241+
// takesCb5()
242+
// ^com
243+
230244
module RecordSourceSelectorProxy = {
231245
@editor.templateVariableName("store")
232246
type t

Diff for: analysis/tests/src/expected/CompletionExpressions.res.txt

+39-3
Original file line numberDiff line numberDiff line change
@@ -1005,9 +1005,45 @@ Path takesCb3
10051005
"insertTextFormat": 2
10061006
}]
10071007

1008-
Complete src/CompletionExpressions.res 238:30
1009-
posCursor:[238:30] posNoWhite:[238:29] Found expr:[238:3->238:31]
1010-
Pexp_apply ...[238:3->238:20] (~updater238:22->238:29=...__ghost__[0:-1->0:-1])
1008+
Complete src/CompletionExpressions.res 233:12
1009+
posCursor:[233:12] posNoWhite:[233:11] Found expr:[233:3->233:13]
1010+
Pexp_apply ...[233:3->233:11] (...[233:12->233:13])
1011+
Completable: Cexpression CArgument Value[takesCb4]($0)
1012+
ContextPath CArgument Value[takesCb4]($0)
1013+
ContextPath Value[takesCb4]
1014+
Path takesCb4
1015+
[{
1016+
"label": "apiCallResult => {}",
1017+
"kind": 12,
1018+
"tags": [],
1019+
"detail": "option<apiCallResult> => 'a",
1020+
"documentation": null,
1021+
"sortText": "A",
1022+
"insertText": "${1:apiCallResult} => {$0}",
1023+
"insertTextFormat": 2
1024+
}]
1025+
1026+
Complete src/CompletionExpressions.res 240:12
1027+
posCursor:[240:12] posNoWhite:[240:11] Found expr:[240:3->240:13]
1028+
Pexp_apply ...[240:3->240:11] (...[240:12->240:13])
1029+
Completable: Cexpression CArgument Value[takesCb5]($0)
1030+
ContextPath CArgument Value[takesCb5]($0)
1031+
ContextPath Value[takesCb5]
1032+
Path takesCb5
1033+
[{
1034+
"label": "apiCallResults => {}",
1035+
"kind": 12,
1036+
"tags": [],
1037+
"detail": "array<option<apiCallResult>> => 'a",
1038+
"documentation": null,
1039+
"sortText": "A",
1040+
"insertText": "${1:apiCallResults} => {$0}",
1041+
"insertTextFormat": 2
1042+
}]
1043+
1044+
Complete src/CompletionExpressions.res 252:30
1045+
posCursor:[252:30] posNoWhite:[252:29] Found expr:[252:3->252:31]
1046+
Pexp_apply ...[252:3->252:20] (~updater252:22->252:29=...__ghost__[0:-1->0:-1])
10111047
Completable: Cexpression CArgument Value[commitLocalUpdate](~updater)
10121048
ContextPath CArgument Value[commitLocalUpdate](~updater)
10131049
ContextPath Value[commitLocalUpdate]

0 commit comments

Comments
 (0)