Skip to content

Commit fdfc485

Browse files
committed
WIP, capture current module path via AST
1 parent 6f799bc commit fdfc485

36 files changed

+577
-1232
lines changed

analysis/src/Commands.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let completion ~debug ~path ~pos ~currentFile =
44
Completions.getCompletions ~debug ~path ~pos ~currentFile ~forHover:false
55
with
66
| None -> []
7-
| Some (completions, full, _) ->
7+
| Some (completions, full, _, _) ->
88
completions
99
|> List.map (CompletionBackEnd.completionToItem ~full)
1010
|> List.map Protocol.stringifyCompletionItem

analysis/src/CompletionBackEnd.ml

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ let completionsGetCompletionType ~full completions =
774774
| _ -> None
775775

776776
let rec completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~pos
777-
completions =
777+
~cursorPath completions =
778778
let firstNonSyntheticCompletion =
779779
List.find_opt (fun c -> not c.Completion.synthetic) completions
780780
in
@@ -787,8 +787,9 @@ let rec completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~pos
787787
| Some {Completion.kind = FollowContextPath (ctxPath, scope); env} ->
788788
ctxPath
789789
|> getCompletionsForContextPath ~debug ~full ~env ~exact:true ~opens
790-
~rawOpens ~pos ~scope
790+
~rawOpens ~pos ~scope ~cursorPath
791791
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~pos
792+
~cursorPath
792793
| Some {Completion.kind = Type typ; env} -> (
793794
match TypeUtils.extractTypeFromResolvedType typ ~env ~full with
794795
| None -> None
@@ -798,7 +799,7 @@ let rec completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~pos
798799
| _ -> None
799800

800801
and completionsGetTypeEnv2 ~debug (completions : Completion.t list) ~full ~opens
801-
~rawOpens ~pos =
802+
~rawOpens ~pos ~cursorPath =
802803
let firstNonSyntheticCompletion =
803804
List.find_opt (fun c -> not c.Completion.synthetic) completions
804805
in
@@ -809,12 +810,12 @@ and completionsGetTypeEnv2 ~debug (completions : Completion.t list) ~full ~opens
809810
| Some {Completion.kind = FollowContextPath (ctxPath, scope); env} ->
810811
ctxPath
811812
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
812-
~exact:true ~scope
813-
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos
813+
~exact:true ~scope ~cursorPath
814+
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~cursorPath
814815
| _ -> None
815816

816817
and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
817-
~scope ?(mode = Regular) contextPath =
818+
~scope ?(mode = Regular) ~cursorPath contextPath =
818819
let envCompletionIsMadeFrom = env in
819820
if debug then
820821
Printf.printf "ContextPath %s\n"
@@ -847,7 +848,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
847848
match
848849
cp
849850
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
850-
~exact:true ~scope
851+
~exact:true ~scope ~cursorPath
851852
|> completionsGetCompletionType ~full
852853
with
853854
| None -> []
@@ -869,7 +870,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
869870
match
870871
cp
871872
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
872-
~exact:true ~scope
873+
~exact:true ~scope ~cursorPath
873874
|> completionsGetCompletionType ~full
874875
with
875876
| None -> []
@@ -884,7 +885,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
884885
match
885886
cp
886887
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
887-
~exact:true ~scope
888+
~exact:true ~scope ~cursorPath
888889
|> completionsGetCompletionType ~full
889890
with
890891
| Some (Tpromise (env, typ), _env) ->
@@ -934,8 +935,9 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
934935
match
935936
cp
936937
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
937-
~exact:true ~scope
938+
~exact:true ~scope ~cursorPath
938939
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~pos
940+
~cursorPath
939941
with
940942
| Some ((TypeExpr typ | ExtractedType (Tfunction {typ})), env) -> (
941943
let rec reconstructFunctionType args tRet =
@@ -985,11 +987,11 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
985987
let completionsFromCtxPath =
986988
cp
987989
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
988-
~exact:true ~scope
990+
~exact:true ~scope ~cursorPath
989991
in
990992
let mainTypeCompletionEnv =
991993
completionsFromCtxPath
992-
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos
994+
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~cursorPath
993995
in
994996
match mainTypeCompletionEnv with
995997
| None ->
@@ -1021,7 +1023,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
10211023
let pipeCompletions =
10221024
cpAsPipeCompletion
10231025
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos
1024-
~env:envCompletionIsMadeFrom ~exact ~scope
1026+
~env:envCompletionIsMadeFrom ~exact ~scope ~cursorPath
10251027
|> List.filter_map (fun c ->
10261028
TypeUtils.transformCompletionToPipeCompletion ~synthetic:true
10271029
~env ?posOfDot c)
@@ -1033,8 +1035,8 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
10331035
match
10341036
cp
10351037
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
1036-
~exact:true ~scope
1037-
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos
1038+
~exact:true ~scope ~cursorPath
1039+
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~cursorPath
10381040
with
10391041
| Some (typ, env) -> (
10401042
match typ |> TypeUtils.extractObjectType ~env ~package with
@@ -1052,8 +1054,8 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
10521054
match
10531055
cp
10541056
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
1055-
~exact:true ~scope ~mode:Pipe
1056-
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos
1057+
~exact:true ~scope ~mode:Pipe ~cursorPath
1058+
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~cursorPath
10571059
with
10581060
| None ->
10591061
if Debug.verbose () then
@@ -1180,8 +1182,18 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
11801182
|> TypeUtils.filterPipeableFunctions ~synthetic:true ~env ~full
11811183
~targetTypeId:mainTypeId
11821184
in
1185+
(* Add completions from current fully qualified path module *)
1186+
let currentFullyQualifiedPathModuleCompletions =
1187+
[]
1188+
(* completionsForPipeFromCompletionPath ~envCompletionIsMadeFrom
1189+
~opens:[] ~pos ~scope ~debug ~prefix ~env ~rawOpens ~full cursorPath
1190+
1191+
|> TypeUtils.filterPipeableFunctions ~synthetic:true ~env ~full
1192+
~targetTypeId:mainTypeId *)
1193+
in
11831194
jsxCompletions @ pipeCompletions @ extraCompletions
1184-
@ currentModuleCompletions @ globallyConfiguredCompletions))
1195+
@ currentModuleCompletions @ globallyConfiguredCompletions
1196+
@ currentFullyQualifiedPathModuleCompletions))
11851197
| CTuple ctxPaths ->
11861198
if Debug.verbose () then print_endline "[ctx_path]--> CTuple";
11871199
(* Turn a list of context paths into a list of type expressions. *)
@@ -1190,7 +1202,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
11901202
|> List.map (fun contextPath ->
11911203
contextPath
11921204
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos
1193-
~env ~exact:true ~scope)
1205+
~env ~exact:true ~scope ~cursorPath)
11941206
|> List.filter_map (fun completionItems ->
11951207
match completionItems with
11961208
| {Completion.kind = Value typ} :: _ -> Some typ
@@ -1208,7 +1220,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
12081220
path
12091221
|> getCompletionsForPath ~debug ~completionContext:Value ~exact:true
12101222
~opens ~full ~pos ~env ~scope
1211-
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos
1223+
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~cursorPath
12121224
in
12131225
let lowercaseComponent =
12141226
match pathToComponent with
@@ -1283,8 +1295,9 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
12831295
match
12841296
functionContextPath
12851297
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
1286-
~exact:true ~scope
1298+
~exact:true ~scope ~cursorPath
12871299
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~pos
1300+
~cursorPath
12881301
with
12891302
| Some ((TypeExpr typ | ExtractedType (Tfunction {typ})), env) ->
12901303
if Debug.verbose () then print_endline "--> found function type";
@@ -1330,8 +1343,9 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
13301343
match
13311344
rootCtxPath
13321345
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
1333-
~exact:true ~scope
1346+
~exact:true ~scope ~cursorPath
13341347
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~pos
1348+
~cursorPath
13351349
with
13361350
| Some (typ, env) -> (
13371351
match typ |> TypeUtils.resolveNestedPatternPath ~env ~full ~nested with
@@ -1846,7 +1860,8 @@ let rec completeTypedValue ?(typeArgContext : typeArgContext option) ~rawOpens
18461860

18471861
module StringSet = Set.Make (String)
18481862

1849-
let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
1863+
let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover ~cursorPath
1864+
completable =
18501865
if debug then
18511866
Printf.printf "Completable: %s\n" (Completable.toString completable);
18521867
let package = full.package in
@@ -1857,14 +1872,14 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
18571872
path
18581873
|> getCompletionsForPath ~debug ~completionContext:Value ~exact:true ~opens
18591874
~full ~pos ~env ~scope
1860-
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos
1875+
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~cursorPath
18611876
in
18621877
match completable with
18631878
| Cnone -> []
18641879
| Cpath contextPath ->
18651880
contextPath
18661881
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
1867-
~exact:forHover ~scope
1882+
~exact:forHover ~scope ~cursorPath
18681883
| Cjsx ([id], prefix, identsSeen) when String.uncapitalize_ascii id = id -> (
18691884
(* Lowercase JSX tag means builtin *)
18701885
let mkLabel (name, typString) =
@@ -2115,8 +2130,8 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
21152130
match
21162131
cp
21172132
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
2118-
~exact:true ~scope
2119-
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos
2133+
~exact:true ~scope ~cursorPath
2134+
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~cursorPath
21202135
with
21212136
| Some (typ, _env) ->
21222137
if debug then
@@ -2144,15 +2159,17 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
21442159
let fallbackOrEmpty ?items () =
21452160
match (fallback, items) with
21462161
| Some fallback, (None | Some []) ->
2147-
fallback |> processCompletable ~debug ~full ~scope ~env ~pos ~forHover
2162+
fallback
2163+
|> processCompletable ~debug ~full ~scope ~env ~pos ~forHover
2164+
~cursorPath
21482165
| _, Some items -> items
21492166
| None, None -> []
21502167
in
21512168
match
21522169
contextPath
21532170
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
2154-
~exact:true ~scope
2155-
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos
2171+
~exact:true ~scope ~cursorPath
2172+
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~cursorPath
21562173
with
21572174
| Some (typ, env) -> (
21582175
match
@@ -2200,7 +2217,7 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
22002217
match
22012218
contextPath
22022219
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
2203-
~exact:true ~scope
2220+
~exact:true ~scope ~cursorPath
22042221
|> completionsGetCompletionType ~full
22052222
with
22062223
| None ->
@@ -2301,7 +2318,7 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
23012318
let completionsForContextPath =
23022319
contextPath
23032320
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
2304-
~exact:forHover ~scope
2321+
~exact:forHover ~scope ~cursorPath
23052322
in
23062323
completionsForContextPath
23072324
|> List.map (fun (c : Completion.t) ->

analysis/src/CompletionFrontEnd.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ let completePipeChain ~(inJsxContext : bool) (exp : Parsetree.expression) =
357357

358358
let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
359359
?findThisExprLoc text =
360+
let cursorPath = Stack.create () in
360361
let offsetNoWhite = Utils.skipWhite text (offset - 1) in
361362
let posNoWhite =
362363
let line, col = posCursor in
@@ -737,8 +738,10 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
737738
if recFlag = Nonrecursive then decls |> List.iter scopeTypeDeclaration;
738739
processed := true
739740
| Pstr_module mb ->
741+
if Loc.hasPos mb.pmb_loc ~pos:posCursor then Stack.push mb.pmb_name.txt cursorPath;
740742
iterator.module_binding iterator mb;
741743
scopeModuleBinding mb;
744+
(* if Loc.hasPos mb.pmb_loc ~pos:posCursor then Stack.pop cursorPath |> ignore; *)
742745
processed := true
743746
| Pstr_recmodule mbs ->
744747
mbs |> List.iter scopeModuleBinding;
@@ -1737,7 +1740,8 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
17371740
(Cpath
17381741
(CPId {loc = Location.none; path = [""]; completionContext = Value})));
17391742
if !found = false then if debug then Printf.printf "XXX Not found!\n";
1740-
!result)
1743+
let cursorPath = Stack.to_seq cursorPath |> List.of_seq |> List.rev in
1744+
!result |> Option.map (fun (c, s) -> (c, s, cursorPath)))
17411745
else if Filename.check_suffix path ".resi" then (
17421746
let parser = Res_driver.parsing_engine.parse_interface ~for_printer:false in
17431747
let {Res_driver.parsetree = signature} = parser ~filename:currentFile in
@@ -1748,7 +1752,8 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
17481752
(Cpath
17491753
(CPId {loc = Location.none; path = [""]; completionContext = Type})));
17501754
if !found = false then if debug then Printf.printf "XXX Not found!\n";
1751-
!result)
1755+
let cursorPath = Stack.to_seq cursorPath |> List.of_seq |> List.rev in
1756+
!result |> Option.map (fun (c, s) -> (c, s, cursorPath)))
17521757
else None
17531758

17541759
let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =

analysis/src/Completions.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ let getCompletions ~debug ~path ~pos ~currentFile ~forHover =
88
~currentFile ~text
99
with
1010
| None -> None
11-
| Some (completable, scope) -> (
11+
| Some (completable, scope, cursorPath) -> (
1212
(* Only perform expensive ast operations if there are completables *)
1313
match Cmt.loadFullCmtFromPath ~path with
1414
| None -> None
1515
| Some full ->
1616
let env = SharedTypes.QueryEnv.fromFile full.file in
17+
let cursorPath = full.file.moduleName :: cursorPath in
1718
let completables =
1819
completable
1920
|> CompletionBackEnd.processCompletable ~debug ~full ~pos ~scope ~env
20-
~forHover
21+
~forHover ~cursorPath
2122
in
22-
Some (completables, full, scope)))
23+
Some (completables, full, scope, cursorPath)))

analysis/src/Hover.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
152152
~supportsMarkdownLinks =
153153
match Completions.getCompletions ~debug ~path ~pos ~currentFile ~forHover with
154154
| None -> None
155-
| Some (completions, ({file; package} as full), scope) -> (
155+
| Some (completions, ({file; package} as full), scope, cursorPath) -> (
156156
let rawOpens = Scope.getRawOpens scope in
157157
match completions with
158158
| {kind = Label typString; docstring} :: _ ->
@@ -166,7 +166,7 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
166166
let opens = CompletionBackEnd.getOpens ~debug ~rawOpens ~package ~env in
167167
match
168168
CompletionBackEnd.completionsGetTypeEnv2 ~debug ~full ~rawOpens ~opens
169-
~pos completions
169+
~pos completions ~cursorPath
170170
with
171171
| Some (typ, _env) ->
172172
let typeString =
@@ -179,7 +179,7 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
179179
let opens = CompletionBackEnd.getOpens ~debug ~rawOpens ~package ~env in
180180
match
181181
CompletionBackEnd.completionsGetTypeEnv2 ~debug ~full ~rawOpens ~opens
182-
~pos completions
182+
~pos completions ~cursorPath
183183
with
184184
| Some (typ, _env) ->
185185
let typeString =

analysis/src/SignatureHelp.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ let findFunctionType ~currentFile ~debug ~path ~pos =
8484
~currentFile ~text
8585
with
8686
| None -> None
87-
| Some (completable, scope) ->
87+
| Some (completable, scope, cursorPath) ->
8888
Some
8989
( completable
9090
|> CompletionBackEnd.processCompletable ~debug ~full ~pos ~scope
91-
~env ~forHover:true,
91+
~env ~forHover:true ~cursorPath,
9292
env,
9393
package,
9494
file ))

analysis/src/Xform.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ let extractTypeFromExpr expr ~debug ~path ~currentFile ~full ~pos =
88
|> CompletionFrontEnd.findTypeOfExpressionAtLoc ~debug ~path ~currentFile
99
~posCursor:(Pos.ofLexing expr.Parsetree.pexp_loc.loc_start)
1010
with
11-
| Some (completable, scope) -> (
11+
| Some (completable, scope, cursorPath) -> (
1212
let env = SharedTypes.QueryEnv.fromFile full.SharedTypes.file in
1313
let completions =
1414
completable
1515
|> CompletionBackEnd.processCompletable ~debug ~full ~pos ~scope ~env
16-
~forHover:true
16+
~forHover:true ~cursorPath
1717
in
1818
let rawOpens = Scope.getRawOpens scope in
1919
match completions with
@@ -23,7 +23,7 @@ let extractTypeFromExpr expr ~debug ~path ~currentFile ~full ~pos =
2323
in
2424
match
2525
CompletionBackEnd.completionsGetCompletionType2 ~debug ~full ~rawOpens
26-
~opens ~pos completions
26+
~opens ~pos completions ~cursorPath
2727
with
2828
| Some (typ, _env) ->
2929
let extractedType =

0 commit comments

Comments
 (0)