Skip to content

Commit f3f0f91

Browse files
committed
Restore come code on references that could be used to find definitions of an element.
1 parent a66401d commit f3f0f91

File tree

1 file changed

+112
-2
lines changed

1 file changed

+112
-2
lines changed

analysis/src/References.ml

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ let locForPos ~extra pos =
4646
arg has the location range of arg
4747
heuristic for: [Props, arg], give loc of `arg` *)
4848
(* Printf.eprintf "l1 %s\nl2 %s\n"
49-
(SharedTypes.locationToString _l1)
50-
(SharedTypes.locationToString l2); *)
49+
(SharedTypes.locationToString _l1)
50+
(SharedTypes.locationToString l2); *)
5151
Some l2
5252
| [(loc1, _); ((loc2, _) as l); (loc3, _)] when loc1 = loc2 && loc2 = loc3 ->
5353
(* JSX with at most one child
@@ -301,3 +301,113 @@ let definitionForLoc ~pathsForModule ~file ~getUri ~getModule loc =
301301
(* oooh wht do I do if the stamp is inside a pseudo-file? *)
302302
maybeLog ("Got stamp " ^ string_of_int stamp);
303303
definition ~file:env.file ~getModule stamp tip)))
304+
305+
let isVisible (declared : _ SharedTypes.declared) =
306+
declared.exported
307+
&&
308+
let rec loop v =
309+
match v with
310+
| File _ -> true
311+
| NotVisible -> false
312+
| IncludedModule (_, inner) -> loop inner
313+
| ExportedModule (_, inner) -> loop inner
314+
in
315+
loop declared.modulePath
316+
317+
let rec pathFromVisibility visibilityPath current =
318+
match visibilityPath with
319+
| File _ -> Some current
320+
| IncludedModule (_, inner) -> pathFromVisibility inner current
321+
| ExportedModule (name, inner) ->
322+
pathFromVisibility inner (Nested (name, current))
323+
| NotVisible -> None
324+
325+
let pathFromVisibility visibilityPath tipName =
326+
pathFromVisibility visibilityPath (Tip tipName)
327+
328+
let forLocalStamp ~pathsForModule ~file ~extra ~allModules ~getModule ~getUri
329+
~getExtra stamp tip =
330+
let env = Query.fileEnv file in
331+
let open Infix in
332+
match
333+
match tip with
334+
| Constructor name ->
335+
Query.getConstructor file stamp name |?>> fun x -> x.stamp
336+
| Field name -> Query.getField file stamp name |?>> fun x -> x.stamp
337+
| _ -> Some stamp
338+
with
339+
| None -> None
340+
| Some localStamp -> (
341+
match Hashtbl.find_opt extra.internalReferences localStamp with
342+
| None -> None
343+
| Some local ->
344+
maybeLog ("Checking externals: " ^ string_of_int stamp);
345+
let externals =
346+
match Query.declaredForTip ~stamps:env.file.stamps stamp tip with
347+
| None -> []
348+
| Some declared ->
349+
if isVisible declared then (
350+
let alternativeReferences =
351+
match
352+
alternateDeclared ~pathsForModule ~file ~getUri declared tip
353+
with
354+
| None -> []
355+
| Some (file, extra, {stamp}) -> (
356+
match
357+
match tip with
358+
| Constructor name ->
359+
Query.getConstructor file stamp name |?>> fun x -> x.stamp
360+
| Field name ->
361+
Query.getField file stamp name |?>> fun x -> x.stamp
362+
| _ -> Some stamp
363+
with
364+
| None -> []
365+
| Some localStamp -> (
366+
match
367+
Hashtbl.find_opt extra.internalReferences localStamp
368+
with
369+
| None -> []
370+
| Some local -> [(file.uri, local)]))
371+
[@@ocaml.doc
372+
"\n\
373+
\ if this file has a corresponding interface or \
374+
implementation file\n\
375+
\ also find the references in that file.\n\
376+
\ "]
377+
in
378+
match pathFromVisibility declared.modulePath declared.name.txt with
379+
| None -> []
380+
| Some path ->
381+
maybeLog ("Now checking path " ^ pathToString path);
382+
let thisModuleName = file.moduleName in
383+
let externals =
384+
allModules
385+
|> List.filter (fun name -> name <> file.moduleName)
386+
|> Utils.filterMap (fun name ->
387+
match getModule name with
388+
| Error _ -> None
389+
| Ok file -> (
390+
match getExtra name with
391+
| Error _ -> None
392+
| Ok extra -> (
393+
match
394+
Hashtbl.find_opt extra.externalReferences
395+
thisModuleName
396+
with
397+
| None -> None
398+
| Some refs ->
399+
let refs =
400+
refs
401+
|> Utils.filterMap (fun (p, t, l) ->
402+
match p = path && t = tip with
403+
| true -> Some l
404+
| false -> None)
405+
in
406+
Some (file.uri, refs))))
407+
in
408+
alternativeReferences @ externals)
409+
else (
410+
maybeLog "Not visible";
411+
[])
412+
in
413+
Some ((file.uri, local) :: externals))

0 commit comments

Comments
 (0)