Skip to content
Open
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
89 changes: 70 additions & 19 deletions analysis/src/references.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions changes/references-other-module-paths.fix.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@tests/sourcedirs-namespaced-dependency-lib",
"private": true,
"scripts": {
"build": "rescript build",
"clean": "rescript clean"
},
"dependencies": {
"rescript": "workspace:^"
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Plain = {
let plain = 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"dependencies": {
"@tests/sourcedirs-dependency-lib": "workspace:*",
"@tests/sourcedirs-namespaced-dependency-lib": "workspace:*",
"rescript": "workspace:^"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let p = NsDep.NsUtils.Plain.plain
// ^ref

module P = NsDep.NsUtils.Plain
let q = P.plain
Original file line number Diff line number Diff line change
@@ -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"
}
]

28 changes: 28 additions & 0 deletions tests/analysis_tests/tests/src/PathRefs.res
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions tests/analysis_tests/tests/src/PathRefsFar.res
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions tests/analysis_tests/tests/src/PathRefsIntf.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module type S = {
let s: int
}
module N = {
let s = 1
// ^ref
}
module M = {
let s = 2
}
5 changes: 5 additions & 0 deletions tests/analysis_tests/tests/src/PathRefsIntf.resi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module type S = {
let s: int
}
module N: S
module M: S
2 changes: 2 additions & 0 deletions tests/analysis_tests/tests/src/PathRefsIntfUse.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let n = PathRefsIntf.N.s
let m = PathRefsIntf.M.s
10 changes: 10 additions & 0 deletions tests/analysis_tests/tests/src/PathRefsUse.res
Original file line number Diff line number Diff line change
@@ -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
118 changes: 118 additions & 0 deletions tests/analysis_tests/tests/src/expected/PathRefs.res.txt
Original file line number Diff line number Diff line change
@@ -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"
}
]

Loading
Loading