Skip to content

Commit 13ff18e

Browse files
committed
Find multiple references to the same file.
1 parent e283a4e commit 13ff18e

File tree

9 files changed

+47
-28
lines changed

9 files changed

+47
-28
lines changed

analysis/.depend

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ src/ProcessAttributes.cmx : src/SharedTypes.cmx src/PrepareUtils.cmx
2626
src/ProcessCmt.cmx : src/Utils.cmx src/Uri2.cmx src/SharedTypes.cmx \
2727
src/Shared.cmx src/ProcessAttributes.cmx src/Packages.cmx src/Log.cmx \
2828
src/FindFiles.cmx src/Files.cmx src/BuildSystem.cmx
29-
src/Protocol.cmx : src/Uri2.cmx src/vendor/Json.cmx
29+
src/Protocol.cmx : src/vendor/Json.cmx
3030
src/References.cmx : src/Utils.cmx src/Uri2.cmx src/SharedTypes.cmx \
3131
src/ProcessCmt.cmx src/Log.cmx src/Infix.cmx
3232
src/Shared.cmx : src/PrintType.cmx src/Log.cmx src/Files.cmx

analysis/src/Commands.ml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,14 @@ let references ~path ~line ~col =
130130
allReferences
131131
|> List.fold_left
132132
(fun acc (uri2, references) ->
133-
if references = [] then [Protocol.stringifyFileLocation uri2]
134-
else
135-
(references
136-
|> List.map (fun loc ->
137-
Protocol.stringifyLocation
138-
{
139-
uri = Uri2.toString uri2;
140-
range = Utils.cmtLocToRange loc;
141-
}))
142-
@ acc)
133+
(references
134+
|> List.map (fun loc ->
135+
Protocol.stringifyLocation
136+
{
137+
uri = Uri2.toString uri2;
138+
range = Utils.cmtLocToRange loc;
139+
}))
140+
@ acc)
143141
[]
144142
in
145143
"[\n" ^ (allLocs |> String.concat ",\n") ^ "\n]")

analysis/src/ProcessCmt.ml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,14 @@ struct
733733
Hashtbl.find extra.externalReferences moduleName
734734
else []))
735735

736+
let addFileReference moduleName loc =
737+
Hashtbl.replace extra.fileReferences moduleName
738+
(loc
739+
::
740+
(if Hashtbl.mem extra.fileReferences moduleName then
741+
Hashtbl.find extra.fileReferences moduleName
742+
else []))
743+
736744
let env = QueryEnv.fromFile Collector.file
737745

738746
let addForPath path lident loc typ tip =
@@ -767,9 +775,9 @@ struct
767775
let addForPathParent path loc =
768776
let locType =
769777
match fromCompilerPath ~env path with
770-
| `GlobalMod name ->
771-
(* TODO track external references to filenames to handle renames well *)
772-
TopLevelModule name
778+
| `GlobalMod moduleName ->
779+
addFileReference moduleName loc;
780+
TopLevelModule moduleName
773781
| `Stamp stamp ->
774782
addReference stamp loc;
775783
LModule (LocalReference (stamp, Module))

analysis/src/Protocol.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ let stringifyLocation h =
5656
Printf.sprintf {|{"uri": "%s", "range": %s}|} (Json.escape h.uri)
5757
(stringifyRange h.range)
5858

59-
let stringifyFileLocation uri =
60-
Printf.sprintf {|{"uri": "%s"}|} (Json.escape (Uri2.toString uri))
61-
6259
let stringifyDocumentSymbolItem i =
6360
Printf.sprintf
6461
{|{

analysis/src/References.ml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -467,15 +467,16 @@ let forLocalStamp ~full:{file; extra; package} stamp tip =
467467

468468
let allReferencesForLocItem ~full:({file; package} as full) locItem =
469469
match locItem.locType with
470-
| TopLevelModule moduleName -> (
471-
match Hashtbl.find_opt package.pathsForModule moduleName with
472-
| None -> []
473-
| Some paths -> (
474-
match getSrc paths with
470+
| TopLevelModule moduleName ->
471+
let locs =
472+
match Hashtbl.find_opt full.extra.fileReferences moduleName with
475473
| None -> []
476-
| Some src ->
477-
let uri, loc = (Uri2.fromPath src, Utils.topLoc src) in
478-
[(uri, [])]))
474+
| Some locs ->
475+
locs
476+
|> List.map (fun loc ->
477+
(Uri2.fromPath loc.Location.loc_start.pos_fname, [loc]))
478+
in
479+
locs
479480
| Typed (_, _, NotFound) | LModule NotFound | Constant _ -> []
480481
| TypeDefinition (_, _, stamp) -> forLocalStamp ~full stamp Type
481482
| Typed (_, _, (LocalReference (stamp, tip) | Definition (stamp, tip)))

analysis/src/SharedTypes.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ type openTracker = {
193193
type extra = {
194194
internalReferences : (int, Location.t list) Hashtbl.t;
195195
externalReferences : (string, (path * tip * Location.t) list) Hashtbl.t;
196+
fileReferences : (string, Location.t list) Hashtbl.t;
196197
mutable locItems : locItem list;
197198
(* This is the "open location", like the location...
198199
or maybe the >> location of the open ident maybe *)
@@ -218,6 +219,7 @@ let initExtra () =
218219
{
219220
internalReferences = Hashtbl.create 10;
220221
externalReferences = Hashtbl.create 10;
222+
fileReferences = Hashtbl.create 10;
221223
locItems = [];
222224
opens = Hashtbl.create 10;
223225
}
@@ -246,7 +248,8 @@ let locKindToString = function
246248

247249
let locTypeToString = function
248250
| Typed (name, e, locKind) ->
249-
"Typed " ^ name ^ " " ^ Shared.typeToString e ^ " " ^ locKindToString locKind
251+
"Typed " ^ name ^ " " ^ Shared.typeToString e ^ " "
252+
^ locKindToString locKind
250253
| Constant _ -> "Constant"
251254
| LModule locKind -> "LModule " ^ locKindToString locKind
252255
| TopLevelModule _ -> "TopLevelModule"

analysis/tests/src/Cross.res

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
let crossRef = References.x
2-
// ^ref
2+
// ^ref
3+
4+
let crossRef2 = References.x
5+
6+
7+
module Ref = References
8+
9+
let crossRef3 = References.x
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
References tests/src/Cross.res 0:17
22
[
3-
{"uri": "References.res"}
3+
{"uri": "Cross.res", "range": {"start": {"line": 0, "character": 15}, "end": {"line": 0, "character": 25}}},
4+
{"uri": "Cross.res", "range": {"start": {"line": 3, "character": 16}, "end": {"line": 3, "character": 26}}},
5+
{"uri": "Cross.res", "range": {"start": {"line": 6, "character": 13}, "end": {"line": 6, "character": 23}}},
6+
{"uri": "Cross.res", "range": {"start": {"line": 8, "character": 16}, "end": {"line": 8, "character": 26}}}
47
]
58

analysis/tests/src/expected/References.res.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
References tests/src/References.res 0:4
22
[
3+
{"uri": "Cross.res", "range": {"start": {"line": 8, "character": 27}, "end": {"line": 8, "character": 28}}},
4+
{"uri": "Cross.res", "range": {"start": {"line": 3, "character": 27}, "end": {"line": 3, "character": 28}}},
35
{"uri": "Cross.res", "range": {"start": {"line": 0, "character": 26}, "end": {"line": 0, "character": 27}}},
46
{"uri": "References.res", "range": {"start": {"line": 7, "character": 8}, "end": {"line": 7, "character": 9}}},
57
{"uri": "References.res", "range": {"start": {"line": 3, "character": 8}, "end": {"line": 3, "character": 9}}},

0 commit comments

Comments
 (0)