Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 63 additions & 36 deletions analysis/src/process_cmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ let rec for_type_signature_item ~(env : Shared_types.Env.t)
let name = Ident.name ident in
let declared =
add_declared ~extent:md_loc
~item:(for_type_module ~name ~env md_type)
~item:(for_type_module ~name ~env:(env |> Env.add_module ~name) md_type)
~name:(Location.mkloc name md_loc)
~stamp:(Ident.binding_time ident) ~env md_attributes
(Exported.add exported Exported.Module)
Expand All @@ -165,7 +165,7 @@ let rec for_type_signature_item ~(env : Shared_types.Env.t)
loc = declared.extent_loc;
};
]
| _ -> []
| Sig_modtype _ | Sig_typext _ -> []

and for_type_signature ~name ~env signature =
let exported = Exported.init () in
Expand Down Expand Up @@ -358,12 +358,11 @@ let rec for_signature_item ~env ~(exported : Exported.t)
| _ -> Types.Trec_next
in
decl |> for_type_declaration ~env ~exported ~rec_status)
| Tsig_module
{md_id; md_attributes; md_loc; md_name = name; md_type = {mty_type}} ->
| Tsig_module {md_id; md_attributes; md_loc; md_name = name; md_type} ->
let item =
for_type_module ~name:name.txt
for_module_type ~name:name.txt
~env:(env |> Env.add_module ~name:name.txt)
mty_type
md_type
in
let declared =
add_declared ~item ~name ~extent:md_loc ~stamp:(Ident.binding_time md_id)
Expand Down Expand Up @@ -401,9 +400,58 @@ let rec for_signature_item ~env ~(exported : Exported.t)
incl_type []
in
top_level
| Tsig_modtype module_type_declaration ->
for_module_type_declaration ~env ~exported module_type_declaration
(* TODO: process other things here *)
| _ -> []

(* A module type written in source: read its signature from the typed tree,
which has the location of each item's name. *)
and for_module_type ~name ~env (module_type : Typedtree.module_type) =
match module_type.mty_desc with
| Tmty_signature signature ->
let exported = Exported.init () in
(* Right to left, as in for_type_signature, so that the last of several
items with the same name is the one exported. *)
let items =
List.fold_right
(fun item items -> for_signature_item ~env ~exported item @ items)
signature.sig_items []
in
Structure {Module.name; docstring = []; exported; items; deprecated = None}
| Tmty_ident _ | Tmty_alias _ | Tmty_functor _ | Tmty_with _ | Tmty_typeof _
->
for_type_module ~name ~env module_type.mty_type

(* A module type declaration written in source. *)
and for_module_type_declaration ~env ~(exported : Exported.t)
({mtd_name = name; mtd_id; mtd_attributes; mtd_type; mtd_loc} :
Typedtree.module_type_declaration) =
match mtd_type with
| None -> []
| Some mod_type ->
let env = env |> Env.add_module_type ~name:name.txt in
let declared =
add_declared
~item:(for_module_type ~name:name.txt ~env mod_type)
~name ~extent:mtd_loc
~stamp:(Ident.binding_time mtd_id)
~env mtd_attributes
(Exported.add exported Exported.Module)
Stamps.add_module
in
[
{
Module.kind =
Module
{type_ = declared.item; is_module_type = is_module_type declared};
name = declared.name.txt;
docstring = declared.docstring;
deprecated = declared.deprecated;
loc = declared.extent_loc;
};
]

let for_signature ~name ~env sig_items =
let exported = Exported.init () in
let items =
Expand Down Expand Up @@ -576,34 +624,8 @@ let rec for_structure_item ~(env : Shared_types.Env.t) ~(exported : Exported.t)
for_structure_item ~env ~exported
{item with str_desc = Tstr_module mod_decl})
|> List.flatten
| Tstr_modtype
{
mtd_name = name;
mtd_id;
mtd_attributes;
mtd_type = Some {mty_type = mod_type};
mtd_loc;
} ->
let env = env |> Env.add_module_type ~name:name.txt in
let mod_type_item = for_type_module ~name:name.txt ~env mod_type in
let declared =
add_declared ~item:mod_type_item ~name ~extent:mtd_loc
~stamp:(Ident.binding_time mtd_id)
~env mtd_attributes
(Exported.add exported Exported.Module)
Stamps.add_module
in
[
{
Module.kind =
Module
{type_ = declared.item; is_module_type = is_module_type declared};
name = declared.name.txt;
docstring = declared.docstring;
deprecated = declared.deprecated;
loc = declared.extent_loc;
};
]
| Tstr_modtype module_type_declaration ->
for_module_type_declaration ~env ~exported module_type_declaration
| Tstr_include {incl_mod; incl_type} ->
let env =
match get_module_path incl_mod.mod_desc with
Expand Down Expand Up @@ -671,11 +693,16 @@ and for_module ~env mod_desc module_name =
| Tmod_unpack (_expr, module_type) ->
let env = env |> Env.add_module ~name:module_name in
for_type_module ~name:module_name ~env module_type
| Tmod_constraint (expr, typ, _constraint, _coercion) ->
| Tmod_constraint (expr, typ, constraint_, _coercion) ->
(* TODO do this better I think *)
let mod_kind = for_module ~env expr.mod_desc module_name in
let env = env |> Env.add_module ~name:module_name in
let mod_type_kind = for_type_module ~name:module_name ~env typ in
let mod_type_kind =
match constraint_ with
| Tmodtype_explicit module_type ->
for_module_type ~name:module_name ~env module_type
| Tmodtype_implicit -> for_type_module ~name:module_name ~env typ
in
Constraint (mod_kind, mod_type_kind)

(*
Expand Down
1 change: 1 addition & 0 deletions changes/editor-signature-declarations.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix editor rename, references and go to definition for values declared in module types, module signatures and `.resi` files.
49 changes: 49 additions & 0 deletions tests/analysis_tests/tests/src/SignatureItems.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module type MT = {
let inMt: int
// ^ren renamed
}
module Impl: MT = {
let inMt = 2
}
let a = Impl.inMt

module type WithSig = {
module type S = {
let sigValue: int
}
module Nested: S
}
module Sealed: WithSig = {
module type S = {
let sigValue: int
}
module Nested = {
let sigValue = 1
}
}
let sv = Sealed.Nested.sigValue
// ^ref

module M: {
module Inner: {
let v: int
// ^ref
}
} = {
module Inner = {
let v = 1
}
}

module type HasInner = {
type t
module Inner: {
let w: int
}
}
module W: HasInner with type t = int = {
type t = int
module Inner = {
let w = 1
}
}
6 changes: 6 additions & 0 deletions tests/analysis_tests/tests/src/SignatureItemsIntf.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module type S = {
let s: int
}
module N = {
let s = 1
}
5 changes: 5 additions & 0 deletions tests/analysis_tests/tests/src/SignatureItemsIntf.resi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module type S = {
let s: int
// ^ref
}
module N: S
8 changes: 8 additions & 0 deletions tests/analysis_tests/tests/src/SignatureItemsUse.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let v = SignatureItems.M.Inner.v
// ^ref

let s = SignatureItemsIntf.N.s
// ^def

let w = SignatureItems.W.Inner.w
// ^ref
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Definition src/DefinitionWithInterface.res 0:4
Definition src/DefinitionWithInterface.res 9:6
{
"range": {
"end": { "character": 12, "line": 6 },
"start": { "character": 2, "line": 6 }
"end": { "character": 7, "line": 6 },
"start": { "character": 6, "line": 6 }
},
"uri": "file:///DefinitionWithInterface.resi"
}
Expand Down
4 changes: 2 additions & 2 deletions tests/analysis_tests/tests/src/expected/References.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ References src/References.res 20:12
[
{
"range": {
"end": { "character": 13, "line": 13 },
"start": { "character": 2, "line": 13 }
"end": { "character": 8, "line": 13 },
"start": { "character": 6, "line": 13 }
},
"uri": "file:///References.res"
},
Expand Down
59 changes: 59 additions & 0 deletions tests/analysis_tests/tests/src/expected/SignatureItems.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Rename src/SignatureItems.res 1:6 renamed
[
{
"edits": [
{
"newText": "renamed",
"range": {
"end": { "character": 10, "line": 1 },
"start": { "character": 6, "line": 1 }
}
},
{
"newText": "renamed",
"range": {
"end": { "character": 17, "line": 7 },
"start": { "character": 13, "line": 7 }
}
}
],
"textDocument": { "uri": "file:///SignatureItems.res", "version": 0 }
}
]

References src/SignatureItems.res 23:23
[
{
"range": {
"end": { "character": 16, "line": 11 },
"start": { "character": 8, "line": 11 }
},
"uri": "file:///SignatureItems.res"
},
{
"range": {
"end": { "character": 31, "line": 23 },
"start": { "character": 23, "line": 23 }
},
"uri": "file:///SignatureItems.res"
}
]

References src/SignatureItems.res 28:8
[
{
"range": {
"end": { "character": 32, "line": 0 },
"start": { "character": 31, "line": 0 }
},
"uri": "file:///SignatureItemsUse.res"
},
{
"range": {
"end": { "character": 9, "line": 28 },
"start": { "character": 8, "line": 28 }
},
"uri": "file:///SignatureItems.res"
}
]

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
References src/SignatureItemsIntf.resi 1:6
[
{
"range": {
"end": { "character": 7, "line": 1 },
"start": { "character": 6, "line": 1 }
},
"uri": "file:///SignatureItemsIntf.res"
},
{
"range": {
"end": { "character": 7, "line": 1 },
"start": { "character": 6, "line": 1 }
},
"uri": "file:///SignatureItemsIntf.resi"
}
]

45 changes: 45 additions & 0 deletions tests/analysis_tests/tests/src/expected/SignatureItemsUse.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
References src/SignatureItemsUse.res 0:31
[
{
"range": {
"end": { "character": 32, "line": 0 },
"start": { "character": 31, "line": 0 }
},
"uri": "file:///SignatureItemsUse.res"
},
{
"range": {
"end": { "character": 9, "line": 28 },
"start": { "character": 8, "line": 28 }
},
"uri": "file:///SignatureItems.res"
}
]

Definition src/SignatureItemsUse.res 3:29
{
"range": {
"end": { "character": 7, "line": 1 },
"start": { "character": 6, "line": 1 }
},
"uri": "file:///SignatureItemsIntf.res"
}

References src/SignatureItemsUse.res 6:31
[
{
"range": {
"end": { "character": 32, "line": 6 },
"start": { "character": 31, "line": 6 }
},
"uri": "file:///SignatureItemsUse.res"
},
{
"range": {
"end": { "character": 14, "line": 40 },
"start": { "character": 4, "line": 40 }
},
"uri": "file:///SignatureItems.res"
}
]

2 changes: 1 addition & 1 deletion tests/tools_tests/src/expected/DocExtraction2.res.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"docstrings": [ "Maker of tea." ],
"source": {
"filepath": "src/DocExtraction2.resi",
"line": 15,
"line": 16,
"col": 3
},
"detail": {
Expand Down
2 changes: 1 addition & 1 deletion tests/tools_tests/src/expected/DocExtraction2.resi.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"docstrings": [ "Maker of tea." ],
"source": {
"filepath": "src/DocExtraction2.resi",
"line": 15,
"line": 16,
"col": 3
},
"detail": {
Expand Down
Loading
Loading