Skip to content

Commit 66cd27b

Browse files
committed
Add references command.
1 parent 9790e36 commit 66cd27b

File tree

5 files changed

+83
-19
lines changed

5 files changed

+83
-19
lines changed

analysis/src/Cli.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ Options:
2222

2323
current-platform.exe hover src/Foo.res 10 2
2424

25-
definition: get inferred type for Foo.res at line 10 column 2:
25+
definition: get definition for item in Foo.res at line 10 column 2:
2626

27-
current-platform.exe definition src/Foo.res 10 2|}
27+
current-platform.exe definition src/Foo.res 10 2
28+
29+
references: get references to item in Foo.res at line 10 column 2:
30+
31+
current-platform.exe references src/Foo.res 10 2|}
2832

2933
let main () =
3034
match Array.to_list Sys.argv with
@@ -36,6 +40,9 @@ let main () =
3640
| [_; "definition"; path; line; col] ->
3741
Commands.definition ~path ~line:(int_of_string line)
3842
~col:(int_of_string col)
43+
| [_; "references"; path; line; col] ->
44+
Commands.references ~path ~line:(int_of_string line)
45+
~col:(int_of_string col)
3946
| _ :: "dump" :: files -> Commands.dump files
4047
| [_; "test"; path] -> Commands.test ~path
4148
| args when List.mem "-h" args || List.mem "--help" args -> prerr_endline help

analysis/src/Commands.ml

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,6 @@ let definition state ~file ~line ~col ~extra ~package =
128128
match References.locForPos ~extra:{extra with locations} pos with
129129
| None -> Protocol.null
130130
| Some (_, loc) -> (
131-
let zzzTODO =
132-
References.allReferencesForLoc ~pathsForModule:package.pathsForModule
133-
~file ~extra ~allModules:package.localModules
134-
~getUri:(State.fileForUri state)
135-
~getModule:(State.fileForModule state ~package)
136-
~getExtra:(State.extraForModule state ~package)
137-
loc
138-
in
139-
140131
let locIsModule =
141132
match loc with
142133
| SharedTypes.LModule _ | TopLevelModule _ -> true
@@ -175,6 +166,53 @@ let definition ~path ~line ~col =
175166
in
176167
print_endline result
177168

169+
let references state ~file ~line ~col ~extra ~package =
170+
let open TopTypes in
171+
let locations =
172+
extra.SharedTypes.locations
173+
|> List.filter (fun (l, _) -> not l.Location.loc_ghost)
174+
in
175+
let pos = Utils.protocolLineColToCmtLoc ~line ~col in
176+
177+
match References.locForPos ~extra:{extra with locations} pos with
178+
| None -> Protocol.null
179+
| Some (_, loc) ->
180+
let allReferences =
181+
References.allReferencesForLoc ~pathsForModule:package.pathsForModule
182+
~file ~extra ~allModules:package.localModules
183+
~getUri:(State.fileForUri state)
184+
~getModule:(State.fileForModule state ~package)
185+
~getExtra:(State.extraForModule state ~package)
186+
loc
187+
in
188+
let allLocs =
189+
allReferences
190+
|> List.fold_left
191+
(fun acc (uri2, references) ->
192+
(references
193+
|> List.map (fun loc ->
194+
Protocol.stringifyLocation
195+
{
196+
uri = Uri2.toString uri2;
197+
range = Utils.cmtLocToRange loc;
198+
}))
199+
@ acc)
200+
[]
201+
in
202+
"[\n" ^ (allLocs |> String.concat ",\n") ^ "]"
203+
204+
let references ~path ~line ~col =
205+
let state = TopTypes.empty () in
206+
let filePath = Files.maybeConcat (Unix.getcwd ()) path in
207+
let uri = Uri2.fromPath filePath in
208+
let result =
209+
match State.getFullFromCmt ~state ~uri with
210+
| Error _message -> Protocol.null
211+
| Ok (package, {file; extra}) ->
212+
references state ~file ~line ~col ~extra ~package
213+
in
214+
print_endline result
215+
178216
let test ~path =
179217
Uri2.stripPath := true;
180218
match Files.readFile path with
@@ -202,6 +240,11 @@ let test ~path =
202240
^ string_of_int col);
203241

204242
hover ~path ~line ~col
243+
| "ref" ->
244+
print_endline
245+
("References " ^ path ^ " " ^ string_of_int line ^ ":"
246+
^ string_of_int col);
247+
references ~path ~line ~col
205248
| "com" ->
206249
print_endline
207250
("Complete " ^ path ^ " " ^ string_of_int line ^ ":"

analysis/src/References.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,10 @@ let forLocalStamp ~pathsForModule ~file ~extra ~allModules ~getModule ~getUri
336336
| Field name -> Query.getField file stamp name |?>> fun x -> x.stamp
337337
| _ -> Some stamp
338338
with
339-
| None -> None
339+
| None -> []
340340
| Some localStamp -> (
341341
match Hashtbl.find_opt extra.internalReferences localStamp with
342-
| None -> None
342+
| None -> []
343343
| Some local ->
344344
maybeLog ("Checking externals: " ^ string_of_int stamp);
345345
let externals =
@@ -406,7 +406,7 @@ let forLocalStamp ~pathsForModule ~file ~extra ~allModules ~getModule ~getUri
406406
maybeLog "Not visible";
407407
[])
408408
in
409-
Some ((file.uri, local) :: externals))
409+
(file.uri, local) :: externals)
410410

411411
let allReferencesForLoc ~pathsForModule ~getUri ~file ~extra ~allModules
412412
~getModule ~getExtra loc =
@@ -415,7 +415,7 @@ let allReferencesForLoc ~pathsForModule ~getUri ~file ~extra ~allModules
415415
| Typed (_, NotFound)
416416
| LModule NotFound
417417
| TopLevelModule _ | Constant _ ->
418-
None
418+
[]
419419
| TypeDefinition (_, _, stamp) ->
420420
forLocalStamp ~pathsForModule ~getUri ~file ~extra ~allModules ~getModule
421421
~getExtra stamp Type
@@ -429,17 +429,17 @@ let allReferencesForLoc ~pathsForModule ~getUri ~file ~extra ~allModules
429429
| LModule (GlobalReference (moduleName, path, tip))
430430
| Typed (_, GlobalReference (moduleName, path, tip)) -> (
431431
match getModule moduleName with
432-
| None -> None
432+
| None -> []
433433
| Some file -> (
434434
let env = Query.fileEnv file in
435435
match Query.resolvePath ~env ~path ~getModule with
436-
| None -> None
436+
| None -> []
437437
| Some (env, name) -> (
438438
match Query.exportedForTip ~env name tip with
439-
| None -> None
439+
| None -> []
440440
| Some stamp -> (
441441
match getUri env.file.uri with
442-
| Error _ -> None
442+
| Error _ -> []
443443
| Ok (file, extra) ->
444444
maybeLog
445445
("Finding references for (global) " ^ Uri2.toString env.file.uri

analysis/tests/src/References.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let x = 12
2+
// ^ref
3+
4+
let a = x
5+
6+
let b = a
7+
8+
let c = x
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
References tests/src/References.res 0:4
2+
[
3+
{"uri": "References.res", "range": {"start": {"line": 7, "character": 8}, "end": {"line": 7, "character": 9}}},
4+
{"uri": "References.res", "range": {"start": {"line": 3, "character": 8}, "end": {"line": 3, "character": 9}}},
5+
{"uri": "References.res", "range": {"start": {"line": 0, "character": 4}, "end": {"line": 0, "character": 5}}}]
6+

0 commit comments

Comments
 (0)