Skip to content

Commit 6cced21

Browse files
committed
Use module path instead of simply the id to find references across res/resi.
1 parent 0f55813 commit 6cced21

11 files changed

+96
-83
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
- Fix issue with references from implementation files which also happen to have interface files https://github.com/rescript-lang/rescript-vscode/issues/645
4545

46+
- Fix issue where jump to definition would go to the wrong place when there are aliased identifiers in submodules https://github.com/rescript-lang/rescript-vscode/pull/653
47+
4648
## v1.8.2
4749

4850
#### :rocket: New Feature

analysis/src/Hover.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ let hoverWithExpandedTypes ~docstring ~file ~package ~supportsMarkdownLinks typ
106106
Markdown.goToDefinitionText ~env ~pos:loc.Warnings.loc_start
107107
else ""
108108
in
109-
Markdown.divider ^ (if supportsMarkdownLinks then Markdown.spacing else "")
109+
Markdown.divider
110+
^ (if supportsMarkdownLinks then Markdown.spacing else "")
110111
^ Markdown.codeBlock
111112
(decl
112113
|> Shared.declToString ~printNameAsIs:true
@@ -186,7 +187,7 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
186187
match ResolvePath.resolvePath ~env ~path ~package with
187188
| None -> None
188189
| Some (env, name) -> (
189-
match References.exportedForTip ~env name tip with
190+
match References.exportedForTip ~env ~name tip with
190191
| None -> None
191192
| Some stamp -> (
192193
match Stamps.findModule file.stamps stamp with
@@ -250,4 +251,4 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
250251
let typeString, docstring = t |> fromType ~docstring in
251252
typeString :: docstring)
252253
in
253-
Some (String.concat "\n\n" parts)
254+
Some (String.concat "\n\n" parts)

analysis/src/ProcessCmt.ml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,17 @@ let rec forSignatureItem ~env ~(exported : Exported.t)
249249
decl |> forTypeDeclaration ~env ~exported ~recStatus)
250250
| Tsig_module
251251
{md_id; md_attributes; md_loc; md_name = name; md_type = {mty_type}} ->
252-
let item = forTypeModule env mty_type in
252+
let item =
253+
let env =
254+
{
255+
env with
256+
modulePath =
257+
ExportedModule
258+
{name = name.txt; modulePath = env.modulePath; isType = false};
259+
}
260+
in
261+
forTypeModule env mty_type
262+
in
253263
let declared =
254264
addDeclared ~item ~name ~extent:md_loc ~stamp:(Ident.binding_time md_id)
255265
~env md_attributes

analysis/src/References.ml

Lines changed: 46 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ let getConstructor (file : File.t) stamp name =
145145
| Some const -> Some const)
146146
| _ -> None)
147147

148-
let exportedForTip ~(env : QueryEnv.t) name (tip : Tip.t) =
148+
let exportedForTip ~(env : QueryEnv.t) ~name (tip : Tip.t) =
149149
match tip with
150150
| Value -> Exported.find env.exported Exported.Value name
151151
| Field _ | Constructor _ | Type ->
@@ -185,7 +185,7 @@ let definedForLoc ~file ~package locKind =
185185
Log.log ("Cannot resolve path " ^ pathToString path);
186186
None
187187
| Some (env, name) -> (
188-
match exportedForTip ~env name tip with
188+
match exportedForTip ~env ~name tip with
189189
| None ->
190190
Log.log
191191
("Exported not found for tip " ^ name ^ " > " ^ Tip.toString tip);
@@ -200,23 +200,6 @@ let definedForLoc ~file ~package locKind =
200200
maybeLog "Yes!! got it";
201201
Some res))))
202202

203-
let declaredForExportedTip ~(stamps : Stamps.t) ~(exported : Exported.t) name
204-
(tip : Tip.t) =
205-
let bind f x = Option.bind x f in
206-
match tip with
207-
| Value ->
208-
Exported.find exported Exported.Value name
209-
|> bind (fun stamp -> Stamps.findValue stamps stamp)
210-
|> Option.map (fun x -> {x with Declared.item = ()})
211-
| Field _ | Constructor _ | Type ->
212-
Exported.find exported Exported.Type name
213-
|> bind (fun stamp -> Stamps.findType stamps stamp)
214-
|> Option.map (fun x -> {x with Declared.item = ()})
215-
| Module ->
216-
Exported.find exported Exported.Module name
217-
|> bind (fun stamp -> Stamps.findModule stamps stamp)
218-
|> Option.map (fun x -> {x with Declared.item = ()})
219-
220203
(** Find alternative declaration: from res in case of interface, or from resi in case of implementation *)
221204
let alternateDeclared ~(file : File.t) ~package (declared : _ Declared.t) tip =
222205
match Hashtbl.find_opt package.pathsForModule file.moduleName with
@@ -230,10 +213,18 @@ let alternateDeclared ~(file : File.t) ~package (declared : _ Declared.t) tip =
230213
match Cmt.fullFromUri ~uri:(Uri.fromPath alternateUri) with
231214
| None -> None
232215
| Some {file; extra} -> (
233-
match
234-
declaredForExportedTip ~stamps:file.stamps
235-
~exported:file.structure.exported declared.name.txt tip
236-
with
216+
let env = QueryEnv.fromFile file in
217+
let path = ModulePath.toPath declared.modulePath declared.name.txt in
218+
maybeLog ("find declared for path " ^ pathToString path);
219+
let declaredOpt =
220+
match ResolvePath.resolvePath ~env ~path ~package with
221+
| None -> None
222+
| Some (env, name) -> (
223+
match exportedForTip ~env ~name tip with
224+
| None -> None
225+
| Some stamp -> declaredForTip ~stamps:file.stamps stamp tip)
226+
in
227+
match declaredOpt with
237228
| None -> None
238229
| Some declared -> Some (file, extra, declared)))
239230
| _ ->
@@ -386,7 +377,7 @@ let definitionForLocItem ~full:{file; package} locItem =
386377
| None -> None
387378
| Some (env, name) -> (
388379
maybeLog ("resolved path:" ^ name);
389-
match exportedForTip ~env name tip with
380+
match exportedForTip ~env ~name tip with
390381
| None -> None
391382
| Some stamp ->
392383
(* oooh wht do I do if the stamp is inside a pseudo-file? *)
@@ -425,7 +416,7 @@ let typeDefinitionForLocItem ~full:{file; package} locItem =
425416
let isVisible (declared : _ Declared.t) =
426417
declared.isExported
427418
&&
428-
let rec loop v =
419+
let rec loop (v : ModulePath.t) =
429420
match v with
430421
| File _ -> true
431422
| NotVisible -> false
@@ -434,17 +425,6 @@ let isVisible (declared : _ Declared.t) =
434425
in
435426
loop declared.modulePath
436427

437-
let rec pathFromVisibility visibilityPath current =
438-
match visibilityPath with
439-
| File _ -> Some current
440-
| IncludedModule (_, inner) -> pathFromVisibility inner current
441-
| ExportedModule {name; modulePath = inner} ->
442-
pathFromVisibility inner (name :: current)
443-
| NotVisible -> None
444-
445-
let pathFromVisibility visibilityPath tipName =
446-
pathFromVisibility visibilityPath [tipName]
447-
448428
type references = {
449429
uri: Uri.t;
450430
locOpt: Location.t option; (* None: reference to a toplevel module *)
@@ -497,35 +477,35 @@ let forLocalStamp ~full:{file; extra; package} stamp (tip : Tip.t) =
497477
(* if this file has a corresponding interface or implementation file
498478
also find the references in that file *)
499479
in
500-
match pathFromVisibility declared.modulePath declared.name.txt with
501-
| None -> []
502-
| Some path ->
503-
maybeLog ("Now checking path " ^ pathToString path);
504-
let thisModuleName = file.moduleName in
505-
let externals =
506-
package.projectFiles |> FileSet.elements
507-
|> List.filter (fun name -> name <> file.moduleName)
508-
|> List.map (fun moduleName ->
509-
Cmt.fullsFromModule ~package ~moduleName
510-
|> List.map (fun {file; extra} ->
511-
match
512-
Hashtbl.find_opt extra.externalReferences
513-
thisModuleName
514-
with
515-
| None -> []
516-
| Some refs ->
517-
let locs =
518-
refs
519-
|> Utils.filterMap (fun (p, t, locs) ->
520-
if p = path && t = tip then Some locs
521-
else None)
522-
in
523-
locs
524-
|> List.map (fun loc ->
525-
{uri = file.uri; locOpt = Some loc})))
526-
|> List.concat |> List.concat
527-
in
528-
alternativeReferences @ externals)
480+
let path =
481+
ModulePath.toPath declared.modulePath declared.name.txt
482+
in
483+
maybeLog ("Now checking path " ^ pathToString path);
484+
let thisModuleName = file.moduleName in
485+
let externals =
486+
package.projectFiles |> FileSet.elements
487+
|> List.filter (fun name -> name <> file.moduleName)
488+
|> List.map (fun moduleName ->
489+
Cmt.fullsFromModule ~package ~moduleName
490+
|> List.map (fun {file; extra} ->
491+
match
492+
Hashtbl.find_opt extra.externalReferences
493+
thisModuleName
494+
with
495+
| None -> []
496+
| Some refs ->
497+
let locs =
498+
refs
499+
|> Utils.filterMap (fun (p, t, locs) ->
500+
if p = path && t = tip then Some locs
501+
else None)
502+
in
503+
locs
504+
|> List.map (fun loc ->
505+
{uri = file.uri; locOpt = Some loc})))
506+
|> List.concat |> List.concat
507+
in
508+
alternativeReferences @ externals)
529509
else (
530510
maybeLog "Not visible";
531511
[])
@@ -580,7 +560,7 @@ let allReferencesForLocItem ~full:({file; package} as full) locItem =
580560
match ResolvePath.resolvePath ~env ~path ~package with
581561
| None -> []
582562
| Some (env, name) -> (
583-
match exportedForTip ~env name tip with
563+
match exportedForTip ~env ~name tip with
584564
| None -> []
585565
| Some stamp -> (
586566
match Cmt.fullFromUri ~uri:env.file.uri with

analysis/src/ResolvePath.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ let resolveFromCompilerPath ~env ~package path =
133133
| NotFound -> NotFound
134134
| Exported (env, name) -> Exported (env, name)
135135

136-
let rec getSourceUri ~(env : QueryEnv.t) ~package path =
136+
let rec getSourceUri ~(env : QueryEnv.t) ~package (path : ModulePath.t) =
137137
match path with
138138
| File (uri, _moduleName) -> uri
139139
| NotVisible -> env.file.uri

analysis/src/SharedTypes.ml

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,27 @@ let str s = if s = "" then "\"\"" else s
22
let list l = "[" ^ (l |> List.map str |> String.concat ", ") ^ "]"
33
let ident l = l |> List.map str |> String.concat "."
44

5-
type modulePath =
6-
| File of Uri.t * string
7-
| NotVisible
8-
| IncludedModule of Path.t * modulePath
9-
| ExportedModule of {name: string; modulePath: modulePath; isType: bool}
5+
type path = string list
6+
7+
let pathToString (path : path) = path |> String.concat "."
8+
9+
module ModulePath = struct
10+
type t =
11+
| File of Uri.t * string
12+
| NotVisible
13+
| IncludedModule of Path.t * t
14+
| ExportedModule of {name: string; modulePath: t; isType: bool}
15+
16+
let toPath modulePath tipName : path =
17+
let rec loop modulePath current =
18+
match modulePath with
19+
| File _ -> current
20+
| IncludedModule (_, inner) -> loop inner current
21+
| ExportedModule {name; modulePath = inner} -> loop inner (name :: current)
22+
| NotVisible -> current
23+
in
24+
loop modulePath [tipName]
25+
end
1026

1127
type field = {stamp: int; fname: string Location.loc; typ: Types.type_expr}
1228

@@ -102,7 +118,7 @@ module Declared = struct
102118
name: string Location.loc;
103119
extentLoc: Location.t;
104120
stamp: int;
105-
modulePath: modulePath;
121+
modulePath: ModulePath.t;
106122
isExported: bool;
107123
deprecated: string option;
108124
docstring: string list;
@@ -257,7 +273,7 @@ module Completion = struct
257273
end
258274

259275
module Env = struct
260-
type t = {stamps: Stamps.t; modulePath: modulePath}
276+
type t = {stamps: Stamps.t; modulePath: ModulePath.t}
261277
end
262278

263279
type filePath = string
@@ -321,10 +337,6 @@ module Tip = struct
321337
| Module -> "Module"
322338
end
323339

324-
type path = string list
325-
326-
let pathToString (path : path) = path |> String.concat "."
327-
328340
let rec pathIdentToString (p : Path.t) =
329341
match p with
330342
| Pident {name} -> name

analysis/tests/src/DefinitionWithInterface.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ let _ = aabbcc
88

99
module Inner = {
1010
let y = 100
11+
// ^def
1112
}

analysis/tests/src/DefinitionWithInterface.resi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ type t
55

66
module Inner: {
77
let y: int
8+
// ^def
89
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,5 @@ Completable: Cpath Value[DefinitionWithInterface, a]
100100
[]
101101

102102
Definition src/Cross.res 39:39
103-
{"uri": "DefinitionWithInterface.res", "range": {"start": {"line": 0, "character": 4}, "end": {"line": 0, "character": 5}}}
103+
{"uri": "DefinitionWithInterface.res", "range": {"start": {"line": 9, "character": 6}, "end": {"line": 9, "character": 7}}}
104104

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
Definition src/DefinitionWithInterface.res 0:4
22
{"uri": "DefinitionWithInterface.resi", "range": {"start": {"line": 0, "character": 4}, "end": {"line": 0, "character": 5}}}
33

4+
Definition src/DefinitionWithInterface.res 9:6
5+
{"uri": "DefinitionWithInterface.resi", "range": {"start": {"line": 6, "character": 2}, "end": {"line": 6, "character": 12}}}
6+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
Definition src/DefinitionWithInterface.resi 0:4
22
{"uri": "DefinitionWithInterface.res", "range": {"start": {"line": 0, "character": 4}, "end": {"line": 0, "character": 5}}}
33

4+
Definition src/DefinitionWithInterface.resi 6:6
5+
{"uri": "DefinitionWithInterface.res", "range": {"start": {"line": 9, "character": 6}, "end": {"line": 9, "character": 7}}}
6+

0 commit comments

Comments
 (0)