From fe98bb0fe8e43a9c1eb56ac7732698dcb245445a Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Thu, 19 Nov 2026 17:04:38 +0000 Subject: [PATCH] Read signature declarations by name in editor analysis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Why: Editor analysis reads the declarations in module types and signature constraints from their types. A type only knows where a whole item is, not where its name is. So rename replaces a value's whole declaration instead of its name, module types nested in a module type are not registered, and modules nested in a signature constraint lose their name in the module path, which hides their uses in other files. Module types and signature constraints written in source are now read from the typed tree, which has the location of each name. Nested modules keep their name in the module path. Rename modified (a value declared in a module type): Example: module type MT = { let inMt: int } Before (text): renaming inMt to renamed `let inMt: int` ⇝ `renamed` After (text): renaming inMt to renamed `inMt` ⇝ `renamed` References modified (a value of a module type nested in a module type): Example: 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 Before (text): references of sigValue in the last line none After (text): references of sigValue in the last line `sigValue` in `let sigValue: int` of WithSig.S `sigValue` in `Sealed.Nested.sigValue` References modified (a module nested in a signature constraint): Example: // SignatureItems.res module M: { module Inner: { let v: int } } = { module Inner = { let v = 1 } } // SignatureItemsUse.res let v = SignatureItems.M.Inner.v Before (text): references of v in M's signature `let v: int` (the whole declaration) After (text): references of v in M's signature `v` in `let v: int` `v` in `SignatureItems.M.Inner.v` in SignatureItemsUse.res Review: Which existing snapshots change? References, DefinitionWithInterface.res and three `rescript-tools doc` snapshots (DocExtraction2, DocExtractionRes): their locations now point at a value's name, or at its `let` line instead of the doc comment above it. Review: Which signatures are still read from types? Signatures that have no source: those of `include`, `module type of`, `with` constraints and `.cmi` files. Their values still get the whole item as location; the types have nothing better. Review: What does not change? Module types copied by an `include` stay unregistered, and renaming a value in a module type still does not rename the modules that implement it. Fixes: #9324 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gGbYDy2DBe5YmGdHTL Signed-off-by: Christoph Knittel --- analysis/src/process_cmt.ml | 99 ++++++++++++------- changes/editor-signature-declarations.fix.md | 1 + .../tests/src/SignatureItems.res | 49 +++++++++ .../tests/src/SignatureItemsIntf.res | 6 ++ .../tests/src/SignatureItemsIntf.resi | 5 + .../tests/src/SignatureItemsUse.res | 8 ++ .../expected/DefinitionWithInterface.res.txt | 4 +- .../tests/src/expected/References.res.txt | 4 +- .../tests/src/expected/SignatureItems.res.txt | 59 +++++++++++ .../src/expected/SignatureItemsIntf.res.txt | 0 .../src/expected/SignatureItemsIntf.resi.txt | 18 ++++ .../src/expected/SignatureItemsUse.res.txt | 45 +++++++++ .../src/expected/DocExtraction2.res.json | 2 +- .../src/expected/DocExtraction2.resi.json | 2 +- .../src/expected/DocExtractionRes.res.json | 4 +- 15 files changed, 262 insertions(+), 44 deletions(-) create mode 100644 changes/editor-signature-declarations.fix.md create mode 100644 tests/analysis_tests/tests/src/SignatureItems.res create mode 100644 tests/analysis_tests/tests/src/SignatureItemsIntf.res create mode 100644 tests/analysis_tests/tests/src/SignatureItemsIntf.resi create mode 100644 tests/analysis_tests/tests/src/SignatureItemsUse.res create mode 100644 tests/analysis_tests/tests/src/expected/SignatureItems.res.txt create mode 100644 tests/analysis_tests/tests/src/expected/SignatureItemsIntf.res.txt create mode 100644 tests/analysis_tests/tests/src/expected/SignatureItemsIntf.resi.txt create mode 100644 tests/analysis_tests/tests/src/expected/SignatureItemsUse.res.txt diff --git a/analysis/src/process_cmt.ml b/analysis/src/process_cmt.ml index d6adf720370..1b4f41e290e 100644 --- a/analysis/src/process_cmt.ml +++ b/analysis/src/process_cmt.ml @@ -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) @@ -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 @@ -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) @@ -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 = @@ -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 @@ -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) (* diff --git a/changes/editor-signature-declarations.fix.md b/changes/editor-signature-declarations.fix.md new file mode 100644 index 00000000000..929d25bb208 --- /dev/null +++ b/changes/editor-signature-declarations.fix.md @@ -0,0 +1 @@ +- Fix editor rename, references and go to definition for values declared in module types, module signatures and `.resi` files. diff --git a/tests/analysis_tests/tests/src/SignatureItems.res b/tests/analysis_tests/tests/src/SignatureItems.res new file mode 100644 index 00000000000..b823733dad8 --- /dev/null +++ b/tests/analysis_tests/tests/src/SignatureItems.res @@ -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 + } +} diff --git a/tests/analysis_tests/tests/src/SignatureItemsIntf.res b/tests/analysis_tests/tests/src/SignatureItemsIntf.res new file mode 100644 index 00000000000..185d2ade54a --- /dev/null +++ b/tests/analysis_tests/tests/src/SignatureItemsIntf.res @@ -0,0 +1,6 @@ +module type S = { + let s: int +} +module N = { + let s = 1 +} diff --git a/tests/analysis_tests/tests/src/SignatureItemsIntf.resi b/tests/analysis_tests/tests/src/SignatureItemsIntf.resi new file mode 100644 index 00000000000..4346cd9f3d6 --- /dev/null +++ b/tests/analysis_tests/tests/src/SignatureItemsIntf.resi @@ -0,0 +1,5 @@ +module type S = { + let s: int + // ^ref +} +module N: S diff --git a/tests/analysis_tests/tests/src/SignatureItemsUse.res b/tests/analysis_tests/tests/src/SignatureItemsUse.res new file mode 100644 index 00000000000..050e6e7fa3b --- /dev/null +++ b/tests/analysis_tests/tests/src/SignatureItemsUse.res @@ -0,0 +1,8 @@ +let v = SignatureItems.M.Inner.v +// ^ref + +let s = SignatureItemsIntf.N.s +// ^def + +let w = SignatureItems.W.Inner.w +// ^ref diff --git a/tests/analysis_tests/tests/src/expected/DefinitionWithInterface.res.txt b/tests/analysis_tests/tests/src/expected/DefinitionWithInterface.res.txt index fa6c727e3db..7c82562c7a4 100644 --- a/tests/analysis_tests/tests/src/expected/DefinitionWithInterface.res.txt +++ b/tests/analysis_tests/tests/src/expected/DefinitionWithInterface.res.txt @@ -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" } diff --git a/tests/analysis_tests/tests/src/expected/References.res.txt b/tests/analysis_tests/tests/src/expected/References.res.txt index dd867386771..ca7b48c5989 100644 --- a/tests/analysis_tests/tests/src/expected/References.res.txt +++ b/tests/analysis_tests/tests/src/expected/References.res.txt @@ -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" }, diff --git a/tests/analysis_tests/tests/src/expected/SignatureItems.res.txt b/tests/analysis_tests/tests/src/expected/SignatureItems.res.txt new file mode 100644 index 00000000000..1e2c99b24b1 --- /dev/null +++ b/tests/analysis_tests/tests/src/expected/SignatureItems.res.txt @@ -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" + } +] + diff --git a/tests/analysis_tests/tests/src/expected/SignatureItemsIntf.res.txt b/tests/analysis_tests/tests/src/expected/SignatureItemsIntf.res.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/analysis_tests/tests/src/expected/SignatureItemsIntf.resi.txt b/tests/analysis_tests/tests/src/expected/SignatureItemsIntf.resi.txt new file mode 100644 index 00000000000..38f778289e4 --- /dev/null +++ b/tests/analysis_tests/tests/src/expected/SignatureItemsIntf.resi.txt @@ -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" + } +] + diff --git a/tests/analysis_tests/tests/src/expected/SignatureItemsUse.res.txt b/tests/analysis_tests/tests/src/expected/SignatureItemsUse.res.txt new file mode 100644 index 00000000000..3132b064350 --- /dev/null +++ b/tests/analysis_tests/tests/src/expected/SignatureItemsUse.res.txt @@ -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" + } +] + diff --git a/tests/tools_tests/src/expected/DocExtraction2.res.json b/tests/tools_tests/src/expected/DocExtraction2.res.json index 1221c0b1b39..b51e0796c81 100644 --- a/tests/tools_tests/src/expected/DocExtraction2.res.json +++ b/tests/tools_tests/src/expected/DocExtraction2.res.json @@ -78,7 +78,7 @@ "docstrings": [ "Maker of tea." ], "source": { "filepath": "src/DocExtraction2.resi", - "line": 15, + "line": 16, "col": 3 }, "detail": { diff --git a/tests/tools_tests/src/expected/DocExtraction2.resi.json b/tests/tools_tests/src/expected/DocExtraction2.resi.json index 1221c0b1b39..b51e0796c81 100644 --- a/tests/tools_tests/src/expected/DocExtraction2.resi.json +++ b/tests/tools_tests/src/expected/DocExtraction2.resi.json @@ -78,7 +78,7 @@ "docstrings": [ "Maker of tea." ], "source": { "filepath": "src/DocExtraction2.resi", - "line": 15, + "line": 16, "col": 3 }, "detail": { diff --git a/tests/tools_tests/src/expected/DocExtractionRes.res.json b/tests/tools_tests/src/expected/DocExtractionRes.res.json index 1f43a055c2d..7f27304c447 100644 --- a/tests/tools_tests/src/expected/DocExtractionRes.res.json +++ b/tests/tools_tests/src/expected/DocExtractionRes.res.json @@ -332,7 +332,7 @@ "docstrings": [ "The maker of stuff!" ], "source": { "filepath": "src/DocExtractionRes.res", - "line": 78, + "line": 79, "col": 3 }, "detail": { @@ -389,7 +389,7 @@ "docstrings": [ "function from t to t" ], "source": { "filepath": "src/DocExtractionRes.res", - "line": 116, + "line": 119, "col": 3 }, "detail": {