@@ -635,6 +635,30 @@ let getCompletionsForPath ~debug ~opens ~full ~pos ~exact ~scope
635
635
findAllCompletions ~env ~prefix ~exact ~names Used ~completion Context
636
636
| None -> [] )
637
637
638
+ (* * Completions intended for piping, from a completion path. *)
639
+ let completionsForPipeFromCompletionPath ~opens ~pos ~scope ~debug ~prefix ~env
640
+ ~rawOpens ~full completionPath =
641
+ let completionPathMinusOpens =
642
+ TypeUtils. removeOpensFromCompletionPath ~raw Opens ~package: full.package
643
+ completionPath
644
+ |> String. concat " ."
645
+ in
646
+ let completionName name =
647
+ if completionPathMinusOpens = " " then name
648
+ else completionPathMinusOpens ^ " ." ^ name
649
+ in
650
+ let completions =
651
+ completionPath @ [prefix]
652
+ |> getCompletionsForPath ~debug ~completion Context:Value ~exact: false ~opens
653
+ ~full ~pos ~env ~scope
654
+ in
655
+ let completions =
656
+ completions
657
+ |> List. map (fun (completion : Completion.t ) ->
658
+ {completion with name = completionName completion.name; env})
659
+ in
660
+ completions
661
+
638
662
let rec digToRecordFieldsForCompletion ~debug ~package ~opens ~full ~pos ~env
639
663
~scope path =
640
664
match
@@ -1028,62 +1052,10 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
1028
1052
let pipeCompletionsForModule =
1029
1053
match pipeCompletion with
1030
1054
| Some (path , completionPath ) ->
1031
- (* Most of this is copied from the pipe completion code. Should probably be unified. *)
1032
- let completions =
1033
- completionPath @ [fieldName]
1034
- |> getCompletionsForPath ~debug ~completion Context:Value
1035
- ~exact: false ~opens ~full ~pos ~env ~scope
1036
- in
1037
- let completionPathMinusOpens =
1038
- TypeUtils. removeOpensFromCompletionPath ~raw Opens ~package
1039
- completionPath
1040
- |> String. concat " ."
1041
- in
1042
- let completionName name =
1043
- if completionPathMinusOpens = " " then name
1044
- else completionPathMinusOpens ^ " ." ^ name
1045
- in
1046
- (* Find all functions in the module that takes type t *)
1047
- let rec fnTakesTypeT t =
1048
- match t.Types. desc with
1049
- | Tlink t1
1050
- | Tsubst t1
1051
- | Tpoly (t1, [] )
1052
- | Tconstr (Pident {name = "function$" } , [t1 ; _ ], _ ) ->
1053
- fnTakesTypeT t1
1054
- | Tarrow _ -> (
1055
- match
1056
- TypeUtils. extractFunctionType ~env ~package: full.package t
1057
- with
1058
- | (Nolabel, {desc = Tconstr (p , _ , _ )} ) :: _ , _ ->
1059
- Path. same p path || Path. name p = " t"
1060
- | _ -> false )
1061
- | _ -> false
1062
- in
1063
- completions
1064
- |> List. filter_map (fun (completion : Completion.t ) ->
1065
- match completion.kind with
1066
- | Value t when fnTakesTypeT t ->
1067
- let name = completionName completion.name in
1068
- let nameWithPipe = " ->" ^ name in
1069
- (* TODO: We need to add support for setting the text insertion location explicitly,
1070
- so we can account for the dot that triggered the completion, but that we want
1071
- removed when inserting the pipe. This means we also need to track the loc of
1072
- the dot + the identifier that we're filtering with (fieldName here).
1073
- That should be easy to do by extending CPField. *)
1074
- Some
1075
- {
1076
- completion with
1077
- name = nameWithPipe;
1078
- sortText =
1079
- Some
1080
- (name |> String. split_on_char '.' |> List. rev
1081
- |> List. hd);
1082
- insertText = Some nameWithPipe;
1083
- env;
1084
- range = Some fieldNameLoc;
1085
- }
1086
- | _ -> None )
1055
+ completionsForPipeFromCompletionPath ~opens ~pos ~scope ~debug
1056
+ ~prefix: fieldName ~env ~raw Opens ~full completionPath
1057
+ |> TypeUtils. filterPipeableFunctions ~env ~full ~path
1058
+ ~replace Range:fieldNameLoc
1087
1059
| None -> []
1088
1060
in
1089
1061
pipeCompletionsForModule
@@ -1134,6 +1106,10 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
1134
1106
with
1135
1107
| None -> []
1136
1108
| Some (typ , envFromCompletionItem ) -> (
1109
+ (* Extract any module to draw extra completions from for the identified type. *)
1110
+ let extraModuleToCompleteFrom =
1111
+ TypeUtils. getExtraModuleToCompleteFromForType typ ~env ~full
1112
+ in
1137
1113
let env, typ =
1138
1114
typ
1139
1115
|> TypeUtils. resolveTypeForPipeCompletion ~env ~package ~full ~lhs Loc
@@ -1184,32 +1160,20 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
1184
1160
~env FromItem:envFromCompletionItem (Utils. expandPath path)
1185
1161
| _ -> None )
1186
1162
in
1163
+ let completionsFromExtraModule =
1164
+ match extraModuleToCompleteFrom with
1165
+ | None -> []
1166
+ | Some completionPath ->
1167
+ completionsForPipeFromCompletionPath ~opens ~pos ~scope ~debug
1168
+ ~prefix: funNamePrefix ~env ~raw Opens ~full completionPath
1169
+ in
1187
1170
match completionPath with
1188
1171
| Some completionPath -> (
1189
- let completionPathMinusOpens =
1190
- TypeUtils. removeOpensFromCompletionPath ~raw Opens ~package
1191
- completionPath
1192
- |> String. concat " ."
1193
- in
1194
- let completionName name =
1195
- if completionPathMinusOpens = " " then name
1196
- else completionPathMinusOpens ^ " ." ^ name
1197
- in
1198
- let completions =
1199
- completionPath @ [funNamePrefix]
1200
- |> getCompletionsForPath ~debug ~completion Context:Value ~exact: false
1201
- ~opens ~full ~pos ~env ~scope
1202
- in
1203
- let completions =
1204
- completions
1205
- |> List. map (fun (completion : Completion.t ) ->
1206
- {
1207
- completion with
1208
- name = completionName completion.name;
1209
- env
1210
- (* Restore original env for the completion after x->foo()... *) ;
1211
- })
1172
+ let completionsFromMainFn =
1173
+ completionsForPipeFromCompletionPath ~opens ~pos ~scope ~debug
1174
+ ~prefix: funNamePrefix ~env ~raw Opens ~full completionPath
1212
1175
in
1176
+ let completions = completionsFromMainFn @ completionsFromExtraModule in
1213
1177
(* We add React element functions to the completion if we're in a JSX context *)
1214
1178
let forJsxCompletion =
1215
1179
if inJsx then
@@ -1246,7 +1210,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
1246
1210
]
1247
1211
@ completions
1248
1212
| _ -> completions)
1249
- | None -> [] ))
1213
+ | None -> completionsFromExtraModule ))
1250
1214
| CTuple ctxPaths ->
1251
1215
if Debug. verbose () then print_endline " [ctx_path]--> CTuple" ;
1252
1216
(* Turn a list of context paths into a list of type expressions. *)
0 commit comments