Skip to content

Commit 6992133

Browse files
committed
Towards better treatment of modules and module types.
Fixes #178
1 parent d818310 commit 6992133

File tree

8 files changed

+33
-5
lines changed

8 files changed

+33
-5
lines changed

analysis/src/Commands.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ let documentSymbol ~path =
170170
| MValue v -> (v |> SharedTypes.variableKind, [])
171171
| MType (t, _) -> (t.decl |> SharedTypes.declarationKind, [])
172172
| Module (Structure contents) -> (Module, getItems contents)
173+
| Module (Constraint (_, Structure contents)) ->
174+
(Module, getItems contents)
175+
| Module (Constraint _) -> (Module, [])
173176
| Module (Ident _) -> (Module, [])
174177
in
175178
if extentLoc.loc_ghost then siblings

analysis/src/Hover.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ let showModule ~docstring ~(file : SharedTypes.file) ~name
4545
| None -> showModuleTopLevel ~docstring ~name file.contents.topLevel
4646
| Some {item = Structure {topLevel}} ->
4747
showModuleTopLevel ~docstring ~name topLevel
48-
| Some {item = Ident _} -> Some "Unable to resolve module reference"
48+
| Some {item = Constraint(Structure {topLevel}, _)} ->
49+
showModuleTopLevel ~docstring ~name topLevel
50+
| Some _ -> Some "Unable to resolve module reference"
4951

5052
let newHover ~(file : SharedTypes.file) ~package locItem =
5153
match locItem.SharedTypes.locType with

analysis/src/ProcessCmt.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ let rec forItem ~env ~(exported : exported) item =
375375
| _ -> Types.Trec_next
376376
in
377377
decl |> forTypeDeclaration ~env ~exported ~recStatus)
378+
| Tstr_modtype _ -> []
378379
| _ -> []
379380

380381
and forModule env mod_desc moduleName =
@@ -422,12 +423,14 @@ and forModule env mod_desc moduleName =
422423
(* e.g. when the same id is defined twice (e.g. make with @react.component) *)
423424
(* skip the constraint and use the original module definition *)
424425
forModule env expr.mod_desc moduleName
425-
| Tmod_constraint (_expr, typ, _constraint, _coercion) ->
426+
| Tmod_constraint (expr, typ, _constraint, _coercion) ->
426427
(* TODO do this better I think *)
428+
let forMod = forModule env expr.mod_desc moduleName in
427429
let env =
428430
{env with modulePath = ExportedModule (moduleName, env.modulePath)}
429431
in
430-
forModuleType env typ
432+
let forModTyp = forModuleType env typ in
433+
Constraint (forMod, forModTyp)
431434

432435
and forStructure ~env items =
433436
let exported = initExported () in
@@ -636,6 +639,7 @@ and findInModule ~env kind path =
636639
match kind with
637640
| Structure {exported} ->
638641
resolvePathInner ~env:{env with qExported = exported} ~path
642+
| Constraint (_, k) -> findInModule ~env k path
639643
| Ident modulePath -> (
640644
let stamp, moduleName, fullPath = joinPaths modulePath path in
641645
if stamp = 0 then Some (`Global (moduleName, fullPath))

analysis/src/References.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ let alternateDeclared ~file ~package declared tip =
197197
let resolveModuleReference ~file ~package (declared : moduleKind declared) =
198198
match declared.item with
199199
| Structure _ -> Some (file, Some declared)
200-
| Ident path -> (
200+
| Constraint ((Structure _ | Constraint _), _) -> Some (file, Some declared)
201+
| Ident path | Constraint (Ident path, _) -> (
201202
let env = ProcessCmt.fileEnv file in
202203
match ProcessCmt.fromCompilerPath ~env path with
203204
| `Not_found -> None

analysis/src/SharedTypes.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ and moduleContents = {
117117
topLevel : moduleItem declared list;
118118
}
119119

120-
and moduleKind = Ident of Path.t | Structure of moduleContents
120+
and moduleKind =
121+
| Ident of Path.t
122+
| Structure of moduleContents
123+
| Constraint of moduleKind * moduleKind
121124

122125
type 't stampMap = (int, 't) Hashtbl.t
123126

analysis/tests/src/Hover.res

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,12 @@ let make2 = (~name:string) => React.string(name)
4444

4545
let num = 34
4646
// ^hov
47+
48+
module type Logger = {
49+
let log: string => unit
50+
}
51+
52+
module JsLogger: Logger = {
53+
// ^hov
54+
let log = (msg: string) => Js.log(msg)
55+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hover tests/src/A.res 4:7
2+
{"contents": "```rescript\nmodule JsLogger = {\n let log: string => unit\n}\n```"}
3+

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ Hover tests/src/Hover.res 41:13
2828
Hover tests/src/Hover.res 44:10
2929
{"contents": "```rescript\nint\n```"}
3030

31+
Hover tests/src/Hover.res 51:7
32+
{"contents": "```rescript\nmodule JsLogger = {\n let log: string => unit\n}\n```"}
33+

0 commit comments

Comments
 (0)