Skip to content

Commit f477af4

Browse files
committed
completing for functions in patterns does not make sense
1 parent 6162f86 commit f477af4

File tree

3 files changed

+61
-39
lines changed

3 files changed

+61
-39
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -972,8 +972,10 @@ let printConstructorArgs argsLen ~asSnippet =
972972
if List.length !args > 0 then "(" ^ (!args |> String.concat ", ") ^ ")"
973973
else ""
974974

975+
type completionMode = Pattern | Expression
976+
975977
let rec completeTypedValue (t : SharedTypes.completionType) ~env ~full ~prefix
976-
~completionContext =
978+
~completionContext ~mode =
977979
let extractedType =
978980
match t with
979981
| TypeExpr t -> t |> TypeUtils.extractType ~env ~package:full.package
@@ -1030,7 +1032,7 @@ let rec completeTypedValue (t : SharedTypes.completionType) ~env ~full ~prefix
10301032
let innerType = Utils.unwrapIfOption t in
10311033
let expandedCompletions =
10321034
TypeExpr innerType
1033-
|> completeTypedValue ~env ~full ~prefix ~completionContext
1035+
|> completeTypedValue ~env ~full ~prefix ~completionContext ~mode
10341036
|> List.map (fun (c : Completion.t) ->
10351037
{
10361038
c with
@@ -1118,41 +1120,44 @@ let rec completeTypedValue (t : SharedTypes.completionType) ~env ~full ~prefix
11181120
]
11191121
else []
11201122
| Some (Tfunction {env; typ; args}) ->
1121-
let mkFnBody ~asSnippet =
1122-
match args with
1123-
| [(Nolabel, argTyp)] when TypeUtils.typeIsUnit argTyp -> "()"
1124-
| [(Nolabel, _)] -> if asSnippet then "${1:v1}" else "v1"
1125-
| _ ->
1126-
let currentUnlabelledIndex = ref 0 in
1127-
let argsText =
1128-
args
1129-
|> List.map (fun ((label, typ) : typedFnArg) ->
1130-
match label with
1131-
| Optional name -> "~" ^ name ^ "=?"
1132-
| Labelled name -> "~" ^ name
1133-
| Nolabel ->
1134-
if TypeUtils.typeIsUnit typ then "()"
1135-
else (
1136-
currentUnlabelledIndex := !currentUnlabelledIndex + 1;
1137-
let num = !currentUnlabelledIndex in
1138-
if asSnippet then
1139-
"${" ^ string_of_int num ^ ":v" ^ string_of_int num ^ "}"
1140-
else "v" ^ string_of_int num))
1141-
|> String.concat ", "
1142-
in
1143-
"(" ^ argsText ^ ")"
1144-
in
1145-
if prefix = "" then
1146-
[
1147-
Completion.createWithSnippet
1148-
~name:(mkFnBody ~asSnippet:false ^ " => {}")
1149-
~insertText:
1150-
(mkFnBody ~asSnippet:!Cfg.supportsSnippets
1151-
^ " => "
1152-
^ if !Cfg.supportsSnippets then "${0:{\\}}" else "{}")
1153-
~sortText:"A" ~kind:(Value typ) ~env ();
1154-
]
1155-
else []
1123+
if mode = Pattern then []
1124+
else
1125+
let mkFnBody ~asSnippet =
1126+
match args with
1127+
| [(Nolabel, argTyp)] when TypeUtils.typeIsUnit argTyp -> "()"
1128+
| [(Nolabel, _)] -> if asSnippet then "${1:v1}" else "v1"
1129+
| _ ->
1130+
let currentUnlabelledIndex = ref 0 in
1131+
let argsText =
1132+
args
1133+
|> List.map (fun ((label, typ) : typedFnArg) ->
1134+
match label with
1135+
| Optional name -> "~" ^ name ^ "=?"
1136+
| Labelled name -> "~" ^ name
1137+
| Nolabel ->
1138+
if TypeUtils.typeIsUnit typ then "()"
1139+
else (
1140+
currentUnlabelledIndex := !currentUnlabelledIndex + 1;
1141+
let num = !currentUnlabelledIndex in
1142+
if asSnippet then
1143+
"${" ^ string_of_int num ^ ":v" ^ string_of_int num
1144+
^ "}"
1145+
else "v" ^ string_of_int num))
1146+
|> String.concat ", "
1147+
in
1148+
"(" ^ argsText ^ ")"
1149+
in
1150+
if prefix = "" then
1151+
[
1152+
Completion.createWithSnippet
1153+
~name:(mkFnBody ~asSnippet:false ^ " => {}")
1154+
~insertText:
1155+
(mkFnBody ~asSnippet:!Cfg.supportsSnippets
1156+
^ " => "
1157+
^ if !Cfg.supportsSnippets then "${0:{\\}}" else "{}")
1158+
~sortText:"A" ~kind:(Value typ) ~env ();
1159+
]
1160+
else []
11561161
| _ -> []
11571162

11581163
let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
@@ -1277,7 +1282,9 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
12771282
| None -> fallbackOrEmpty ()
12781283
| Some (typ, env, completionContext) ->
12791284
let items =
1280-
typ |> completeTypedValue ~env ~full ~prefix ~completionContext
1285+
typ
1286+
|> completeTypedValue ~mode:Pattern ~env ~full ~prefix
1287+
~completionContext
12811288
in
12821289
fallbackOrEmpty ~items ())
12831290
| None -> fallbackOrEmpty ())
@@ -1297,7 +1304,9 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
12971304
| None -> []
12981305
| Some (typ, env, completionContext) -> (
12991306
let items =
1300-
typ |> completeTypedValue ~env ~full ~prefix ~completionContext
1307+
typ
1308+
|> completeTypedValue ~mode:Expression ~env ~full ~prefix
1309+
~completionContext
13011310
in
13021311
match (prefix, completionContext) with
13031312
| "", _ -> items

analysis/tests/src/CompletionPattern.res

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,10 @@ let s = (true, Some(true), [false])
194194

195195
// switch s { | (true, _, []) }
196196
// ^com
197+
198+
type recordWithFn = {someFn: unit => unit}
199+
200+
let ff: recordWithFn = {someFn: () => ()}
201+
202+
// switch ff { | {someFn: }}
203+
// ^com

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,3 +904,9 @@ Completable: Cpattern Value[s]->tuple($1)
904904
"documentation": null
905905
}]
906906

907+
Complete src/CompletionPattern.res 201:25
908+
posCursor:[201:25] posNoWhite:[201:24] Found expr:[201:3->201:28]
909+
posCursor:[201:25] posNoWhite:[201:24] Found pattern:[201:17->201:28]
910+
Completable: Cpattern Value[ff]->recordField(someFn)
911+
[]
912+

0 commit comments

Comments
 (0)