Skip to content

Commit 0cae74b

Browse files
cristianoczth
authored andcommitted
Add path to QueryEnv and remove it from Completion.
1 parent d56ced2 commit 0cae74b

File tree

5 files changed

+77
-80
lines changed

5 files changed

+77
-80
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,6 @@ let completionForExporteds iterExported getDeclared ~prefix ~exact ~env
562562
with
563563
deprecated = declared.deprecated;
564564
docstring = declared.docstring;
565-
modulePath = declared.modulePath;
566565
}
567566
:: !res
568567
| _ -> ());
@@ -1153,12 +1152,9 @@ let completionToItem {Completion.name; deprecated; docstring; kind} =
11531152
~deprecated ~detail:(detail name kind) ~docstring
11541153

11551154
let completionsGetTypeEnv = function
1156-
| {Completion.kind = Value typ; env; modulePath} :: _ ->
1157-
Some (typ, env, modulePath)
1158-
| {Completion.kind = ObjLabel typ; env; modulePath} :: _ ->
1159-
Some (typ, env, modulePath)
1160-
| {Completion.kind = Field ({typ}, _); env; modulePath} :: _ ->
1161-
Some (typ, env, modulePath)
1155+
| {Completion.kind = Value typ; env} :: _ -> Some (typ, env)
1156+
| {Completion.kind = ObjLabel typ; env} :: _ -> Some (typ, env)
1157+
| {Completion.kind = Field ({typ}, _); env} :: _ -> Some (typ, env)
11621158
| _ -> None
11631159

11641160
let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
@@ -1189,7 +1185,7 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
11891185
~env ~exact:true ~scope
11901186
|> completionsGetTypeEnv
11911187
with
1192-
| Some (typ, env, modulePath) -> (
1188+
| Some (typ, env) -> (
11931189
let rec reconstructFunctionType args tRet =
11941190
match args with
11951191
| [] -> tRet
@@ -1220,10 +1216,7 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
12201216
| args, tRet when args <> [] ->
12211217
let args = processApply args labels in
12221218
let retType = reconstructFunctionType args tRet in
1223-
[
1224-
Completion.createWithModulePath ~name:"dummy" ~env
1225-
~kind:(Completion.Value retType) ~modulePath;
1226-
]
1219+
[Completion.create ~name:"dummy" ~env ~kind:(Completion.Value retType)]
12271220
| _ -> [])
12281221
| None -> [])
12291222
| CPField (CPId (path, Module), fieldName) ->
@@ -1238,20 +1231,19 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
12381231
~env ~exact:true ~scope
12391232
|> completionsGetTypeEnv
12401233
with
1241-
| Some (typ, env, modulePath) -> (
1234+
| Some (typ, env) -> (
12421235
match typ |> extractRecordType ~env ~package with
12431236
| Some (env, fields, typDecl) ->
12441237
fields
12451238
|> Utils.filterMap (fun field ->
12461239
if checkName field.fname.txt ~prefix:fieldName ~exact then
12471240
Some
1248-
(Completion.createWithModulePath ~name:field.fname.txt ~env
1241+
(Completion.create ~name:field.fname.txt ~env
12491242
~kind:
12501243
(Completion.Field
12511244
( field,
12521245
typDecl.item.decl
1253-
|> Shared.declToString typDecl.name.txt ))
1254-
~modulePath)
1246+
|> Shared.declToString typDecl.name.txt )))
12551247
else None)
12561248
| None -> [])
12571249
| None -> [])
@@ -1262,7 +1254,7 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
12621254
~env ~exact:true ~scope
12631255
|> completionsGetTypeEnv
12641256
with
1265-
| Some (typ, env, modulePath) -> (
1257+
| Some (typ, env) -> (
12661258
match typ |> extractObjectType ~env ~package with
12671259
| Some (env, tObj) ->
12681260
let rec getFields (texp : Types.type_expr) =
@@ -1278,8 +1270,8 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
12781270
|> Utils.filterMap (fun (field, typ) ->
12791271
if checkName field ~prefix:label ~exact then
12801272
Some
1281-
(Completion.createWithModulePath ~name:field ~env
1282-
~kind:(Completion.ObjLabel typ) ~modulePath)
1273+
(Completion.create ~name:field ~env
1274+
~kind:(Completion.ObjLabel typ))
12831275
else None)
12841276
| None -> [])
12851277
| None -> [])
@@ -1290,7 +1282,7 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
12901282
~env ~exact:true ~scope
12911283
|> completionsGetTypeEnv
12921284
with
1293-
| Some (typ, _envFromCompletionItem, completionItemModulePath) -> (
1285+
| Some (typ, envFromCompletionItem) -> (
12941286
let {
12951287
arrayModulePath;
12961288
optionModulePath;
@@ -1360,15 +1352,15 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
13601352
(* Assume a non-empty type path is coming from the compiler and
13611353
can be used as-is. *)
13621354
Some (List.rev rest)
1363-
| _ -> (
1364-
(* Module paths coming directly from a completion item is prefixed with
1365-
file module name it was found in. We pluck that off here if the env
1366-
we're in is the same as the completion item was found in. This ensures
1367-
that a correct qualified path can be produced. *)
1368-
match ModulePath.toFullPath completionItemModulePath with
1369-
| topModule :: rest when topModule = env.file.moduleName ->
1370-
Some rest
1371-
| path -> Some path)))
1355+
| _ ->
1356+
(* Get the path from the comletion environment *)
1357+
let pathFromEnv =
1358+
let path = envFromCompletionItem.path in
1359+
if env.file.moduleName = envFromCompletionItem.file.moduleName
1360+
then path
1361+
else envFromCompletionItem.file.moduleName :: path
1362+
in
1363+
Some pathFromEnv))
13721364
| None -> None
13731365
in
13741366
match completionPath with
@@ -1457,7 +1449,7 @@ let processCompletable ~debug ~package ~scope ~env ~pos ~forHover
14571449
| Cjsx (componentPath, prefix, identsSeen) ->
14581450
let labels =
14591451
match componentPath @ ["make"] |> findTypeOfValue with
1460-
| Some (typ, make_env, _) ->
1452+
| Some (typ, make_env) ->
14611453
let rec getFieldsV3 (texp : Types.type_expr) =
14621454
match texp.desc with
14631455
| Tfield (name, _, t1, t2) ->
@@ -1793,7 +1785,7 @@ Note: The `@react.component` decorator requires the react-jsx config to be set i
17931785
~env ~exact:true ~scope
17941786
|> completionsGetTypeEnv
17951787
with
1796-
| Some (typ, _env, _) ->
1788+
| Some (typ, _env) ->
17971789
if debug then
17981790
Printf.printf "Found type for function %s\n"
17991791
(typ |> Shared.typeToString);

analysis/src/Hover.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
152152
Some (Protocol.stringifyHover (String.concat "\n\n" parts))
153153
| _ -> (
154154
match CompletionBackEnd.completionsGetTypeEnv completions with
155-
| Some (typ, _env, _) ->
155+
| Some (typ, _env) ->
156156
let typeString, _docstring =
157157
hoverWithExpandedTypes ~docstring:"" ~file ~package
158158
~supportsMarkdownLinks typ

analysis/src/ProcessCmt.ml

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -104,33 +104,34 @@ let rec forTypeSignatureItem ~(env : SharedTypes.Env.t) ~(exported : Exported.t)
104104
in
105105
[{Module.kind = Type (declared.item, recStatus); name = declared.name.txt}]
106106
| Sig_module (ident, {md_type; md_attributes; md_loc}, _) ->
107+
let name = Ident.name ident in
107108
let declared =
108109
addDeclared ~extent:md_loc
109-
~item:(forTypeModule env md_type)
110-
~name:(Location.mkloc (Ident.name ident) md_loc)
110+
~item:(forTypeModule ~name ~env md_type)
111+
~name:(Location.mkloc name md_loc)
111112
~stamp:(Ident.binding_time ident) ~env md_attributes
112113
(Exported.add exported Exported.Module)
113114
Stamps.addModule
114115
in
115116
[{Module.kind = Module declared.item; name = declared.name.txt}]
116117
| _ -> []
117118

118-
and forTypeSignature env signature =
119+
and forTypeSignature ~name ~env signature =
119120
let exported = Exported.init () in
120121
let items =
121122
List.fold_right
122123
(fun item items -> forTypeSignatureItem ~env ~exported item @ items)
123124
signature []
124125
in
125-
{Module.docstring = []; exported; items}
126+
{Module.name; docstring = []; exported; items}
126127

127-
and forTypeModule env moduleType =
128+
and forTypeModule ~name ~env moduleType =
128129
match moduleType with
129130
| Types.Mty_ident path -> Ident path
130131
| Mty_alias (_ (* 402 *), path) -> Ident path
131-
| Mty_signature signature -> Structure (forTypeSignature env signature)
132+
| Mty_signature signature -> Structure (forTypeSignature ~name ~env signature)
132133
| Mty_functor (_argIdent, _argType, resultType) ->
133-
forTypeModule env resultType
134+
forTypeModule ~name ~env resultType
134135

135136
let getModuleTypePath mod_desc =
136137
match mod_desc with
@@ -249,7 +250,11 @@ let rec forSignatureItem ~env ~(exported : Exported.t)
249250
decl |> forTypeDeclaration ~env ~exported ~recStatus)
250251
| Tsig_module
251252
{md_id; md_attributes; md_loc; md_name = name; md_type = {mty_type}} ->
252-
let item = forTypeModule (env |> Env.addModule ~name:name.txt) mty_type in
253+
let item =
254+
forTypeModule ~name:name.txt
255+
~env:(env |> Env.addModule ~name:name.txt)
256+
mty_type
257+
in
253258
let declared =
254259
addDeclared ~item ~name ~extent:md_loc ~stamp:(Ident.binding_time md_id)
255260
~env md_attributes
@@ -279,7 +284,7 @@ let rec forSignatureItem ~env ~(exported : Exported.t)
279284
(* TODO: process other things here *)
280285
| _ -> []
281286

282-
let forSignature ~env sigItems =
287+
let forSignature ~name ~env sigItems =
283288
let exported = Exported.init () in
284289
let items =
285290
sigItems |> List.map (forSignatureItem ~env ~exported) |> List.flatten
@@ -294,13 +299,13 @@ let forSignature ~env sigItems =
294299
| None -> []
295300
| Some d -> [d]
296301
in
297-
{Module.docstring; exported; items}
302+
{Module.name; docstring; exported; items}
298303

299-
let forTreeModuleType ~env {Typedtree.mty_desc} =
304+
let forTreeModuleType ~name ~env {Typedtree.mty_desc} =
300305
match mty_desc with
301306
| Tmty_ident _ -> None
302307
| Tmty_signature {sig_items} ->
303-
let contents = forSignature ~env sig_items in
308+
let contents = forSignature ~name ~env sig_items in
304309
Some (Module.Structure contents)
305310
| _ -> None
306311

@@ -352,7 +357,7 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
352357
(String.length name.txt >= 6
353358
&& (String.sub name.txt 0 6 = "local_") [@doesNotRaise])
354359
(* %%private generates a dummy module called local_... *) ->
355-
let item = forModule env mod_desc name.txt in
360+
let item = forModule ~env mod_desc name.txt in
356361
let declared =
357362
addDeclared ~item ~name ~extent:mb_loc ~stamp:(Ident.binding_time mb_id)
358363
~env mb_attributes
@@ -375,7 +380,7 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
375380
mtd_loc;
376381
} ->
377382
let env = env |> Env.addModuleType ~name:name.txt in
378-
let modTypeItem = forTypeModule env modType in
383+
let modTypeItem = forTypeModule ~name:name.txt ~env modType in
379384
let declared =
380385
addDeclared ~item:modTypeItem ~name ~extent:mtd_loc
381386
~stamp:(Ident.binding_time mtd_id)
@@ -418,18 +423,18 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
418423
decl |> forTypeDeclaration ~env ~exported ~recStatus)
419424
| _ -> []
420425

421-
and forModule env mod_desc moduleName =
426+
and forModule ~env mod_desc moduleName =
422427
match mod_desc with
423428
| Tmod_ident (path, _lident) -> Ident path
424429
| Tmod_structure structure ->
425430
let env = env |> Env.addModule ~name:moduleName in
426-
let contents = forStructure ~env structure.str_items in
431+
let contents = forStructure ~name:moduleName ~env structure.str_items in
427432
Structure contents
428433
| Tmod_functor (ident, argName, maybeType, resultExpr) ->
429434
(match maybeType with
430435
| None -> ()
431436
| Some t -> (
432-
match forTreeModuleType ~env t with
437+
match forTreeModuleType ~name:argName.txt ~env t with
433438
| None -> ()
434439
| Some kind ->
435440
let stamp = Ident.binding_time ident in
@@ -438,20 +443,20 @@ and forModule env mod_desc moduleName =
438443
~extent:t.Typedtree.mty_loc ~stamp ~modulePath:NotVisible false []
439444
in
440445
Stamps.addModule env.stamps stamp declared));
441-
forModule env resultExpr.mod_desc moduleName
446+
forModule ~env resultExpr.mod_desc moduleName
442447
| Tmod_apply (functor_, _arg, _coercion) ->
443-
forModule env functor_.mod_desc moduleName
448+
forModule ~env functor_.mod_desc moduleName
444449
| Tmod_unpack (_expr, moduleType) ->
445450
let env = env |> Env.addModule ~name:moduleName in
446-
forTypeModule env moduleType
451+
forTypeModule ~name:moduleName ~env moduleType
447452
| Tmod_constraint (expr, typ, _constraint, _coercion) ->
448453
(* TODO do this better I think *)
449-
let modKind = forModule env expr.mod_desc moduleName in
454+
let modKind = forModule ~env expr.mod_desc moduleName in
450455
let env = env |> Env.addModule ~name:moduleName in
451-
let modTypeKind = forTypeModule env typ in
456+
let modTypeKind = forTypeModule ~name:moduleName ~env typ in
452457
Constraint (modKind, modTypeKind)
453458

454-
and forStructure ~env strItems =
459+
and forStructure ~name ~env strItems =
455460
let exported = Exported.init () in
456461
let items =
457462
List.fold_right
@@ -468,7 +473,7 @@ and forStructure ~env strItems =
468473
| None -> []
469474
| Some d -> [d]
470475
in
471-
{docstring; exported; items}
476+
{Module.name; docstring; exported; items}
472477

473478
let fileForCmtInfos ~moduleName ~uri
474479
({cmt_modname; cmt_annots} : Cmt_format.cmt_infos) =
@@ -486,7 +491,7 @@ let fileForCmtInfos ~moduleName ~uri
486491
| _ -> None)
487492
|> List.concat
488493
in
489-
let structure = forStructure ~env items in
494+
let structure = forStructure ~name:moduleName ~env items in
490495
{File.uri; moduleName = cmt_modname; stamps = env.stamps; structure}
491496
| Partial_interface parts ->
492497
let items =
@@ -498,13 +503,13 @@ let fileForCmtInfos ~moduleName ~uri
498503
| _ -> None)
499504
|> List.concat
500505
in
501-
let structure = forSignature ~env items in
506+
let structure = forSignature ~name:moduleName ~env items in
502507
{uri; moduleName = cmt_modname; stamps = env.stamps; structure}
503508
| Implementation structure ->
504-
let structure = forStructure ~env structure.str_items in
509+
let structure = forStructure ~name:moduleName ~env structure.str_items in
505510
{uri; moduleName = cmt_modname; stamps = env.stamps; structure}
506511
| Interface signature ->
507-
let structure = forSignature ~env signature.sig_items in
512+
let structure = forSignature ~name:moduleName ~env signature.sig_items in
508513
{uri; moduleName = cmt_modname; stamps = env.stamps; structure}
509514
| _ -> File.create moduleName uri
510515

analysis/src/ResolvePath.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ and resolvePathInner ~(env : QueryEnv.t) ~path =
4646

4747
and findInModule ~(env : QueryEnv.t) module_ path =
4848
match module_ with
49-
| Structure {exported} -> resolvePathInner ~env:{env with exported} ~path
49+
| Structure structure ->
50+
resolvePathInner ~env:(QueryEnv.enterStructure env structure) ~path
5051
| Constraint (_, module1) -> findInModule ~env module1 path
5152
| Ident modulePath -> (
5253
let stamp, moduleName, fullPath = joinPaths modulePath path in

0 commit comments

Comments
 (0)