diff --git a/analysis/src/references.ml b/analysis/src/references.ml index 7d238853e3a..a1c9c9f66d3 100644 --- a/analysis/src/references.ml +++ b/analysis/src/references.ml @@ -440,8 +440,11 @@ let for_local_stamp ~state ~full:{file; extra; package} stamp (tip : Tip.t) = | None -> [] | Some declared -> if is_visible declared then ( + let alternate = + alternate_declared ~state ~package ~file declared tip + in let alternative_references = - match alternate_declared ~state ~package ~file declared tip with + match alternate with | None -> [] | Some (file, extra, {stamp}) -> ( match @@ -475,28 +478,76 @@ let for_local_stamp ~state ~full:{file; extra; package} stamp (tip : Tip.t) = normalize_external_reference_key ~namespace:package.namespace ~module_name:this_module_name ~path in + (* A use in another file is recorded under the path it is written + with, e.g. Use.S.add for a functor application S in Use.res, or + Dep.Utils.x for a module of the namespaced dependency Dep. + Such a path names the same declaration when it resolves to the + declaration's stamp, or to the stamp of its .res/.resi + counterpart at the same path. A counterpart at another path, + such as S.s for N.s in `module N: S`, is shared with other + modules, so uses that resolve to it are not uses of this + declaration. Only paths ending in the declaration's name can + resolve to the declaration; their resolutions are cached per + key. *) + let targets = + (Uri.is_interface file.uri, stamp) + :: + (match alternate with + | Some (alternate_file, _extra, alternate_declared) + when Module_path.to_path alternate_declared.module_path + alternate_declared.name.txt + = path -> + [ + (Uri.is_interface alternate_file.uri, alternate_declared.stamp); + ] + | _ -> []) + in + let resolutions = Hashtbl.create 16 in + let resolves_to_declaration module_name path = + match Hashtbl.find_opt resolutions (module_name, path) with + | Some result -> result + | None -> + let result = + match + Process_cmt.file_for_module ~state ~package module_name + with + | None -> false + | Some module_file -> ( + let env = Query_env.from_file module_file in + match exported_for_tip ~state ~env ~path ~package ~tip with + | None -> false + | Some (env, _name, resolved_stamp) -> + env.file.module_name = file.module_name + && List.mem + (Uri.is_interface env.file.uri, resolved_stamp) + targets) + in + Hashtbl.replace resolutions (module_name, path) result; + result + in + let refers_to_declaration module_name path = + (module_name = normalized_module_name && path = normalized_path) + || + match List.rev path with + | name :: _ when name = declared.name.txt -> + resolves_to_declaration module_name path + | _ -> false + in let externals = package.project_files |> File_set.elements |> List.filter (fun name -> name <> file.module_name) - |> List.map (fun module_name -> + |> List.concat_map (fun module_name -> Cmt.fulls_from_module ~package ~module_name - |> List.map (fun {file; extra} -> - match - Hashtbl.find_opt extra.external_references - normalized_module_name - with - | None -> [] - | Some refs -> - let locs = - refs - |> List.filter_map (fun (p, t, locs) -> - if p = normalized_path && t = tip then Some locs - else None) - in - locs - |> List.map (fun loc -> - {uri = file.uri; loc_opt = Some loc}))) - |> List.concat |> List.concat + |> List.concat_map (fun {file; extra} -> + Hashtbl.fold + (fun module_name refs acc -> + (refs + |> List.filter_map (fun (p, t, loc) -> + if t = tip && refers_to_declaration module_name p + then Some {uri = file.uri; loc_opt = Some loc} + else None)) + @ acc) + extra.external_references [])) in alternative_references @ externals) else ( diff --git a/changes/references-other-module-paths.fix.md b/changes/references-other-module-paths.fix.md new file mode 100644 index 00000000000..31bc9d1a7b1 --- /dev/null +++ b/changes/references-other-module-paths.fix.md @@ -0,0 +1 @@ +- Find References and Rename in the editor now also find uses written through another module path, such as a functor application in another file, a local alias of a functor, or a namespaced dependency. diff --git a/tests/analysis_tests/tests-sourcedirs-dependency/namespaced-dependency/package.json b/tests/analysis_tests/tests-sourcedirs-dependency/namespaced-dependency/package.json new file mode 100644 index 00000000000..2e1997a08a6 --- /dev/null +++ b/tests/analysis_tests/tests-sourcedirs-dependency/namespaced-dependency/package.json @@ -0,0 +1,11 @@ +{ + "name": "@tests/sourcedirs-namespaced-dependency-lib", + "private": true, + "scripts": { + "build": "rescript build", + "clean": "rescript clean" + }, + "dependencies": { + "rescript": "workspace:^" + } +} diff --git a/tests/analysis_tests/tests-sourcedirs-dependency/namespaced-dependency/rescript.json b/tests/analysis_tests/tests-sourcedirs-dependency/namespaced-dependency/rescript.json new file mode 100644 index 00000000000..56e6d940ac1 --- /dev/null +++ b/tests/analysis_tests/tests-sourcedirs-dependency/namespaced-dependency/rescript.json @@ -0,0 +1,12 @@ +{ + "name": "@tests/sourcedirs-namespaced-dependency-lib", + "namespace": "NsDep", + "sources": [ + { + "dir": "src", + "subdirs": true + } + ], + "package-specs": [{ "module": "commonjs", "in-source": false }], + "suffix": ".res.js" +} diff --git a/tests/analysis_tests/tests-sourcedirs-dependency/namespaced-dependency/src/NsUtils.res b/tests/analysis_tests/tests-sourcedirs-dependency/namespaced-dependency/src/NsUtils.res new file mode 100644 index 00000000000..e642a124556 --- /dev/null +++ b/tests/analysis_tests/tests-sourcedirs-dependency/namespaced-dependency/src/NsUtils.res @@ -0,0 +1,3 @@ +module Plain = { + let plain = 1 +} diff --git a/tests/analysis_tests/tests-sourcedirs-dependency/package.json b/tests/analysis_tests/tests-sourcedirs-dependency/package.json index c0a27837683..1d3b87857cf 100644 --- a/tests/analysis_tests/tests-sourcedirs-dependency/package.json +++ b/tests/analysis_tests/tests-sourcedirs-dependency/package.json @@ -7,6 +7,7 @@ }, "dependencies": { "@tests/sourcedirs-dependency-lib": "workspace:*", + "@tests/sourcedirs-namespaced-dependency-lib": "workspace:*", "rescript": "workspace:^" } } diff --git a/tests/analysis_tests/tests-sourcedirs-dependency/rescript.json b/tests/analysis_tests/tests-sourcedirs-dependency/rescript.json index 1339c0fc2c2..75c63efe703 100644 --- a/tests/analysis_tests/tests-sourcedirs-dependency/rescript.json +++ b/tests/analysis_tests/tests-sourcedirs-dependency/rescript.json @@ -6,7 +6,10 @@ "subdirs": true } ], - "dependencies": ["@tests/sourcedirs-dependency-lib"], + "dependencies": [ + "@tests/sourcedirs-dependency-lib", + "@tests/sourcedirs-namespaced-dependency-lib" + ], "package-specs": [{ "module": "commonjs", "in-source": false }], "suffix": ".res.js" } diff --git a/tests/analysis_tests/tests-sourcedirs-dependency/src/NamespacedDependency.res b/tests/analysis_tests/tests-sourcedirs-dependency/src/NamespacedDependency.res new file mode 100644 index 00000000000..14c097d286f --- /dev/null +++ b/tests/analysis_tests/tests-sourcedirs-dependency/src/NamespacedDependency.res @@ -0,0 +1,5 @@ +let p = NsDep.NsUtils.Plain.plain +// ^ref + +module P = NsDep.NsUtils.Plain +let q = P.plain diff --git a/tests/analysis_tests/tests-sourcedirs-dependency/src/expected/NamespacedDependency.res.txt b/tests/analysis_tests/tests-sourcedirs-dependency/src/expected/NamespacedDependency.res.txt new file mode 100644 index 00000000000..7f9fd5191ec --- /dev/null +++ b/tests/analysis_tests/tests-sourcedirs-dependency/src/expected/NamespacedDependency.res.txt @@ -0,0 +1,25 @@ +References src/NamespacedDependency.res 0:28 +[ + { + "range": { + "end": { "character": 33, "line": 0 }, + "start": { "character": 28, "line": 0 } + }, + "uri": "file:///NamespacedDependency.res" + }, + { + "range": { + "end": { "character": 15, "line": 4 }, + "start": { "character": 10, "line": 4 } + }, + "uri": "file:///NamespacedDependency.res" + }, + { + "range": { + "end": { "character": 11, "line": 1 }, + "start": { "character": 6, "line": 1 } + }, + "uri": "file:///NsUtils.res" + } +] + diff --git a/tests/analysis_tests/tests/src/PathRefs.res b/tests/analysis_tests/tests/src/PathRefs.res new file mode 100644 index 00000000000..07d5d738c77 --- /dev/null +++ b/tests/analysis_tests/tests/src/PathRefs.res @@ -0,0 +1,28 @@ +module type Ord = { + type t +} +module IntOrd = { + type t = int +} +module MakeSet = (O: Ord) => { + type sh = Dot | Box(O.t) + let add = (x: O.t) => [x] + // ^ref +} +module MakeTwo = (A: Ord, B: Ord) => { + let pair = 1 + // ^ref +} +module First = MakeTwo(IntOrd) +module Other = { + let add = 2 +} +module Plain = { + type r = { + // ^ref + field: int, + //^ref + } + let value = 1 + // ^ref +} diff --git a/tests/analysis_tests/tests/src/PathRefsFar.res b/tests/analysis_tests/tests/src/PathRefsFar.res new file mode 100644 index 00000000000..25b79fa4ec0 --- /dev/null +++ b/tests/analysis_tests/tests/src/PathRefsFar.res @@ -0,0 +1,16 @@ +let f = PathRefsUse.S.add(2) +// ^ref + +let b = PathRefsUse.S.Box(1) +// ^ref + +let c = PathRefsUse.Aliased.Box(2) + +let g = PathRefsUse.S.add(3) +// ^ren plus + +let o = PathRefsUse.O.add + +let v = PathRefsUse.Al.value +let r: PathRefsUse.Al.r = {field: 1} +let rf = r.field diff --git a/tests/analysis_tests/tests/src/PathRefsIntf.res b/tests/analysis_tests/tests/src/PathRefsIntf.res new file mode 100644 index 00000000000..83aa8128811 --- /dev/null +++ b/tests/analysis_tests/tests/src/PathRefsIntf.res @@ -0,0 +1,10 @@ +module type S = { + let s: int +} +module N = { + let s = 1 + // ^ref +} +module M = { + let s = 2 +} diff --git a/tests/analysis_tests/tests/src/PathRefsIntf.resi b/tests/analysis_tests/tests/src/PathRefsIntf.resi new file mode 100644 index 00000000000..f0a283e9c97 --- /dev/null +++ b/tests/analysis_tests/tests/src/PathRefsIntf.resi @@ -0,0 +1,5 @@ +module type S = { + let s: int +} +module N: S +module M: S diff --git a/tests/analysis_tests/tests/src/PathRefsIntfUse.res b/tests/analysis_tests/tests/src/PathRefsIntfUse.res new file mode 100644 index 00000000000..82628aff075 --- /dev/null +++ b/tests/analysis_tests/tests/src/PathRefsIntfUse.res @@ -0,0 +1,2 @@ +let n = PathRefsIntf.N.s +let m = PathRefsIntf.M.s diff --git a/tests/analysis_tests/tests/src/PathRefsUse.res b/tests/analysis_tests/tests/src/PathRefsUse.res new file mode 100644 index 00000000000..6f99b6f23f4 --- /dev/null +++ b/tests/analysis_tests/tests/src/PathRefsUse.res @@ -0,0 +1,10 @@ +module S = PathRefs.MakeSet(PathRefs.IntOrd) +let s = S.add(1) +module Make = PathRefs.MakeSet +module Aliased = Make(PathRefs.IntOrd) +let a = Aliased.add(1) +module Fin = PathRefs.First(PathRefs.IntOrd) +let p = Fin.pair +module O = PathRefs.Other +let o = O.add +module Al = PathRefs.Plain diff --git a/tests/analysis_tests/tests/src/expected/PathRefs.res.txt b/tests/analysis_tests/tests/src/expected/PathRefs.res.txt new file mode 100644 index 00000000000..29064b3ccb6 --- /dev/null +++ b/tests/analysis_tests/tests/src/expected/PathRefs.res.txt @@ -0,0 +1,118 @@ +References src/PathRefs.res 8:6 +[ + { + "range": { + "end": { "character": 13, "line": 1 }, + "start": { "character": 10, "line": 1 } + }, + "uri": "file:///PathRefsUse.res" + }, + { + "range": { + "end": { "character": 19, "line": 4 }, + "start": { "character": 16, "line": 4 } + }, + "uri": "file:///PathRefsUse.res" + }, + { + "range": { + "end": { "character": 25, "line": 0 }, + "start": { "character": 22, "line": 0 } + }, + "uri": "file:///PathRefsFar.res" + }, + { + "range": { + "end": { "character": 25, "line": 8 }, + "start": { "character": 22, "line": 8 } + }, + "uri": "file:///PathRefsFar.res" + }, + { + "range": { + "end": { "character": 9, "line": 8 }, + "start": { "character": 6, "line": 8 } + }, + "uri": "file:///PathRefs.res" + } +] + +References src/PathRefs.res 12:6 +[ + { + "range": { + "end": { "character": 16, "line": 6 }, + "start": { "character": 12, "line": 6 } + }, + "uri": "file:///PathRefsUse.res" + }, + { + "range": { + "end": { "character": 10, "line": 12 }, + "start": { "character": 6, "line": 12 } + }, + "uri": "file:///PathRefs.res" + } +] + +References src/PathRefs.res 20:7 +[ + { + "range": { + "end": { "character": 23, "line": 14 }, + "start": { "character": 22, "line": 14 } + }, + "uri": "file:///PathRefsFar.res" + }, + { + "range": { + "end": { "character": 8, "line": 20 }, + "start": { "character": 7, "line": 20 } + }, + "uri": "file:///PathRefs.res" + } +] + +References src/PathRefs.res 22:6 +[ + { + "range": { + "end": { "character": 32, "line": 14 }, + "start": { "character": 27, "line": 14 } + }, + "uri": "file:///PathRefsFar.res" + }, + { + "range": { + "end": { "character": 16, "line": 15 }, + "start": { "character": 11, "line": 15 } + }, + "uri": "file:///PathRefsFar.res" + }, + { + "range": { + "end": { "character": 9, "line": 22 }, + "start": { "character": 4, "line": 22 } + }, + "uri": "file:///PathRefs.res" + } +] + +References src/PathRefs.res 25:6 +[ + { + "range": { + "end": { "character": 28, "line": 13 }, + "start": { "character": 23, "line": 13 } + }, + "uri": "file:///PathRefsFar.res" + }, + { + "range": { + "end": { "character": 11, "line": 25 }, + "start": { "character": 6, "line": 25 } + }, + "uri": "file:///PathRefs.res" + } +] + diff --git a/tests/analysis_tests/tests/src/expected/PathRefsFar.res.txt b/tests/analysis_tests/tests/src/expected/PathRefsFar.res.txt new file mode 100644 index 00000000000..87dbfe312ee --- /dev/null +++ b/tests/analysis_tests/tests/src/expected/PathRefsFar.res.txt @@ -0,0 +1,118 @@ +References src/PathRefsFar.res 0:22 +[ + { + "range": { + "end": { "character": 13, "line": 1 }, + "start": { "character": 10, "line": 1 } + }, + "uri": "file:///PathRefsUse.res" + }, + { + "range": { + "end": { "character": 19, "line": 4 }, + "start": { "character": 16, "line": 4 } + }, + "uri": "file:///PathRefsUse.res" + }, + { + "range": { + "end": { "character": 25, "line": 0 }, + "start": { "character": 22, "line": 0 } + }, + "uri": "file:///PathRefsFar.res" + }, + { + "range": { + "end": { "character": 25, "line": 8 }, + "start": { "character": 22, "line": 8 } + }, + "uri": "file:///PathRefsFar.res" + }, + { + "range": { + "end": { "character": 9, "line": 8 }, + "start": { "character": 6, "line": 8 } + }, + "uri": "file:///PathRefs.res" + } +] + +References src/PathRefsFar.res 3:22 +[ + { + "range": { + "end": { "character": 25, "line": 3 }, + "start": { "character": 22, "line": 3 } + }, + "uri": "file:///PathRefsFar.res" + }, + { + "range": { + "end": { "character": 31, "line": 6 }, + "start": { "character": 28, "line": 6 } + }, + "uri": "file:///PathRefsFar.res" + }, + { + "range": { + "end": { "character": 21, "line": 7 }, + "start": { "character": 18, "line": 7 } + }, + "uri": "file:///PathRefs.res" + } +] + +Rename src/PathRefsFar.res 8:22 plus +[ + { + "edits": [ + { + "newText": "plus", + "range": { + "end": { "character": 13, "line": 1 }, + "start": { "character": 10, "line": 1 } + } + }, + { + "newText": "plus", + "range": { + "end": { "character": 19, "line": 4 }, + "start": { "character": 16, "line": 4 } + } + } + ], + "textDocument": { "uri": "file:///PathRefsUse.res", "version": 0 } + }, + { + "edits": [ + { + "newText": "plus", + "range": { + "end": { "character": 25, "line": 0 }, + "start": { "character": 22, "line": 0 } + } + }, + { + "newText": "plus", + "range": { + "end": { "character": 25, "line": 8 }, + "start": { "character": 22, "line": 8 } + } + } + ], + "textDocument": { "uri": "file:///PathRefsFar.res", "version": 0 } + }, + { + "edits": [ + { + "newText": "plus", + "range": { + "end": { "character": 9, "line": 8 }, + "start": { "character": 6, "line": 8 } + } + } + ], + "textDocument": { "uri": "file:///PathRefs.res", "version": 0 } + } +] + diff --git a/tests/analysis_tests/tests/src/expected/PathRefsIntf.res.txt b/tests/analysis_tests/tests/src/expected/PathRefsIntf.res.txt new file mode 100644 index 00000000000..74b92708c64 --- /dev/null +++ b/tests/analysis_tests/tests/src/expected/PathRefsIntf.res.txt @@ -0,0 +1,25 @@ +References src/PathRefsIntf.res 4:6 +[ + { + "range": { + "end": { "character": 24, "line": 0 }, + "start": { "character": 23, "line": 0 } + }, + "uri": "file:///PathRefsIntfUse.res" + }, + { + "range": { + "end": { "character": 7, "line": 1 }, + "start": { "character": 6, "line": 1 } + }, + "uri": "file:///PathRefsIntf.resi" + }, + { + "range": { + "end": { "character": 7, "line": 4 }, + "start": { "character": 6, "line": 4 } + }, + "uri": "file:///PathRefsIntf.res" + } +] + diff --git a/tests/analysis_tests/tests/src/expected/PathRefsIntf.resi.txt b/tests/analysis_tests/tests/src/expected/PathRefsIntf.resi.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/analysis_tests/tests/src/expected/PathRefsIntfUse.res.txt b/tests/analysis_tests/tests/src/expected/PathRefsIntfUse.res.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/analysis_tests/tests/src/expected/PathRefsUse.res.txt b/tests/analysis_tests/tests/src/expected/PathRefsUse.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 index 38f778289e4..3690dc7ff80 100644 --- a/tests/analysis_tests/tests/src/expected/SignatureItemsIntf.resi.txt +++ b/tests/analysis_tests/tests/src/expected/SignatureItemsIntf.resi.txt @@ -1,5 +1,12 @@ References src/SignatureItemsIntf.resi 1:6 [ + { + "range": { + "end": { "character": 30, "line": 3 }, + "start": { "character": 29, "line": 3 } + }, + "uri": "file:///SignatureItemsUse.res" + }, { "range": { "end": { "character": 7, "line": 1 }, diff --git a/yarn.lock b/yarn.lock index 8478c97b249..605df379b27 100644 --- a/yarn.lock +++ b/yarn.lock @@ -694,6 +694,15 @@ __metadata: resolution: "@tests/sourcedirs-dependency@workspace:tests/analysis_tests/tests-sourcedirs-dependency" dependencies: "@tests/sourcedirs-dependency-lib": "workspace:*" + "@tests/sourcedirs-namespaced-dependency-lib": "workspace:*" + rescript: "workspace:^" + languageName: unknown + linkType: soft + +"@tests/sourcedirs-namespaced-dependency-lib@workspace:*, @tests/sourcedirs-namespaced-dependency-lib@workspace:tests/analysis_tests/tests-sourcedirs-dependency/namespaced-dependency": + version: 0.0.0-use.local + resolution: "@tests/sourcedirs-namespaced-dependency-lib@workspace:tests/analysis_tests/tests-sourcedirs-dependency/namespaced-dependency" + dependencies: rescript: "workspace:^" languageName: unknown linkType: soft