Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 198a2eb

Browse files
committedJan 8, 2021
Add support for doc strings when hovering on modules.
Fixes rescript-lang/rescript-vscode#58
1 parent a25c883 commit 198a2eb

File tree

6 files changed

+65
-18
lines changed

6 files changed

+65
-18
lines changed
 

‎Changes.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## master
2+
- Add support for doc strings when hovering on modules.
23

34
## Release 1.0.3 of rescript-vscode
45
This [commit](https://github.com/rescript-lang/rescript-editor-support/commit/214d220d8573f9f0c8d54e623c163e01617bf124) is vendored in [rescript-vscode 1.0.3](https://github.com/rescript-lang/rescript-vscode/releases/tag/1.0.3).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@@ocaml.doc("This comment is for the **toplevel** module.")
2+
3+
@ocaml.doc("This comment is for the first **nested** module.")
4+
module Nested = {
5+
let x = "123"
6+
7+
@ocaml.doc("This comment is for the inner **nested-again** module.")
8+
module NestedAgain = {
9+
let y = 123
10+
}
11+
}
12+
13+
module M = Nested.NestedAgain

‎examples/example-project/src/ZZ.res

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,6 @@ let testRecordFields = (gr: gr) => {
8181
@ocaml.doc("vr docstring")
8282
type vr = | V1 | V2
8383

84-
let v1 = V1
84+
let v1 = V1
85+
86+
module DoubleNested = ModuleWithDocComment.Nested.NestedAgain

‎src/rescript-editor-support/Hover.re

+28-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ let codeBlock = code => {
1717
};
1818

1919
let showModuleTopLevel =
20-
(~name, topLevel: list(SharedTypes.declared(SharedTypes.moduleItem))) => {
20+
(
21+
~docstring,
22+
~name,
23+
topLevel: list(SharedTypes.declared(SharedTypes.moduleItem)),
24+
) => {
2125
let contents =
2226
topLevel
2327
|> List.map(item =>
@@ -32,19 +36,25 @@ let showModuleTopLevel =
3236
)
3337
|> String.concat("\n");
3438
let full = "module " ++ name ++ " = {" ++ "\n" ++ contents ++ "\n}";
35-
Some(codeBlock(full));
39+
let doc =
40+
switch (docstring) {
41+
| None => ""
42+
| Some(s) => "\n" ++ s ++ "\n"
43+
};
44+
Some(doc ++ codeBlock(full));
3645
};
3746

3847
let showModule =
3948
(
49+
~docstring,
4050
~file: SharedTypes.file,
4151
~name,
4252
declared: option(SharedTypes.declared(SharedTypes.moduleKind)),
4353
) => {
4454
switch (declared) {
45-
| None => showModuleTopLevel(~name, file.contents.topLevel)
55+
| None => showModuleTopLevel(~docstring, ~name, file.contents.topLevel)
4656
| Some({item: Structure({topLevel})}) =>
47-
showModuleTopLevel(~name, topLevel)
57+
showModuleTopLevel(~docstring, ~name, topLevel)
4858
| Some({item: Ident(_)}) => Some("Unable to resolve module reference")
4959
};
5060
};
@@ -60,12 +70,12 @@ let newHover = (~rootUri, ~file: SharedTypes.file, ~getModule, loc) => {
6070
let%opt md = Hashtbl.find_opt(file.stamps.modules, stamp);
6171
let%opt (file, declared) =
6272
References.resolveModuleReference(~file, ~getModule, md);
63-
let name =
73+
let (name, docstring) =
6474
switch (declared) {
65-
| Some(d) => d.name.txt
66-
| None => file.moduleName
75+
| Some(d) => (d.name.txt, d.docstring)
76+
| None => (file.moduleName, file.contents.docstring)
6777
};
68-
showModule(~name, ~file, declared);
78+
showModule(~docstring, ~name, ~file, declared);
6979
| LModule(GlobalReference(moduleName, path, tip)) =>
7080
let%opt file = getModule(moduleName);
7181
let env = {Query.file, exported: file.contents.exported};
@@ -74,16 +84,21 @@ let newHover = (~rootUri, ~file: SharedTypes.file, ~getModule, loc) => {
7484
let%opt md = Hashtbl.find_opt(file.stamps.modules, stamp);
7585
let%opt (file, declared) =
7686
References.resolveModuleReference(~file, ~getModule, md);
77-
let name =
87+
let (name, docstring) =
7888
switch (declared) {
79-
| Some(d) => d.name.txt
80-
| None => file.moduleName
89+
| Some(d) => (d.name.txt, d.docstring)
90+
| None => (file.moduleName, file.contents.docstring)
8191
};
82-
showModule(~name, ~file, declared);
92+
showModule(~docstring, ~name, ~file, declared);
8393
| LModule(NotFound) => None
8494
| TopLevelModule(name) =>
8595
let%opt file = getModule(name);
86-
showModule(~name=file.moduleName, ~file, None);
96+
showModule(
97+
~docstring=file.contents.docstring,
98+
~name=file.moduleName,
99+
~file,
100+
None,
101+
);
87102
| Typed(_, Definition(_, Field(_) | Constructor(_))) => None
88103
| Constant(t) =>
89104
Some(

‎src/rescript-editor-support/ProcessCmt.re

+17-3
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ and forSignatureType = (env, signature) => {
203203
signature,
204204
[],
205205
);
206-
{exported, topLevel};
206+
{docstring: None, exported, topLevel};
207207
}
208208
and forModuleType = (env, moduleType) =>
209209
switch (moduleType) {
@@ -370,7 +370,14 @@ let forSignature = (~env, items) => {
370370
let exported = initExported();
371371
let topLevel =
372372
items |> List.map(forSignatureItem(~env, ~exported)) |> List.flatten;
373-
{exported, topLevel};
373+
let attributes =
374+
switch (items) {
375+
| [{sig_desc: Tsig_attribute(attribute)}, ..._] => [attribute]
376+
| _ => []
377+
};
378+
let docstring =
379+
ProcessAttributes.findDocAttribute(attributes) |?>> env.processDoc;
380+
{docstring, exported, topLevel};
374381
};
375382

376383
let forTreeModuleType = (~env, {mty_desc}) =>
@@ -554,7 +561,14 @@ and forStructure = (~env, items) => {
554561
items,
555562
[],
556563
);
557-
{exported, topLevel};
564+
let attributes =
565+
switch (items) {
566+
| [{str_desc: Tstr_attribute(attribute)}, ..._] => [attribute]
567+
| _ => []
568+
};
569+
let docstring =
570+
ProcessAttributes.findDocAttribute(attributes) |?>> env.processDoc;
571+
{docstring, exported, topLevel};
558572
};
559573

560574
let forCmt =

‎src/rescript-editor-support/SharedTypes.re

+3-1
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ type moduleItem =
141141
| MType(Type.t, Types.rec_status)
142142
| Module(moduleKind)
143143
and moduleContents = {
144+
docstring: option(string),
144145
exported,
145-
mutable topLevel: list(declared(moduleItem)),
146+
topLevel: list(declared(moduleItem)),
146147
}
147148
and moduleKind =
148149
| Ident(Path.t)
@@ -176,6 +177,7 @@ let emptyFile = (moduleName, uri) => {
176177
stamps: initStamps(),
177178
moduleName,
178179
contents: {
180+
docstring: None,
179181
exported: initExported(),
180182
topLevel: [],
181183
},

0 commit comments

Comments
 (0)
This repository has been archived.