Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.

Add support for doc strings when hovering on modules. #49

Merged
merged 1 commit into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## master
- Add support for doc strings when hovering on modules.

## Release 1.0.3 of rescript-vscode
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).
Expand Down
13 changes: 13 additions & 0 deletions examples/example-project/src/ModuleWithDocComment.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@@ocaml.doc("This comment is for the **toplevel** module.")

@ocaml.doc("This comment is for the first **nested** module.")
module Nested = {
let x = "123"

@ocaml.doc("This comment is for the inner **nested-again** module.")
module NestedAgain = {
let y = 123
}
}

module M = Nested.NestedAgain
4 changes: 3 additions & 1 deletion examples/example-project/src/ZZ.res
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ let testRecordFields = (gr: gr) => {
@ocaml.doc("vr docstring")
type vr = | V1 | V2

let v1 = V1
let v1 = V1

module DoubleNested = ModuleWithDocComment.Nested.NestedAgain
41 changes: 28 additions & 13 deletions src/rescript-editor-support/Hover.re
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ let codeBlock = code => {
};

let showModuleTopLevel =
(~name, topLevel: list(SharedTypes.declared(SharedTypes.moduleItem))) => {
(
~docstring,
~name,
topLevel: list(SharedTypes.declared(SharedTypes.moduleItem)),
) => {
let contents =
topLevel
|> List.map(item =>
Expand All @@ -32,19 +36,25 @@ let showModuleTopLevel =
)
|> String.concat("\n");
let full = "module " ++ name ++ " = {" ++ "\n" ++ contents ++ "\n}";
Some(codeBlock(full));
let doc =
switch (docstring) {
| None => ""
| Some(s) => "\n" ++ s ++ "\n"
};
Some(doc ++ codeBlock(full));
};

let showModule =
(
~docstring,
~file: SharedTypes.file,
~name,
declared: option(SharedTypes.declared(SharedTypes.moduleKind)),
) => {
switch (declared) {
| None => showModuleTopLevel(~name, file.contents.topLevel)
| None => showModuleTopLevel(~docstring, ~name, file.contents.topLevel)
| Some({item: Structure({topLevel})}) =>
showModuleTopLevel(~name, topLevel)
showModuleTopLevel(~docstring, ~name, topLevel)
| Some({item: Ident(_)}) => Some("Unable to resolve module reference")
};
};
Expand All @@ -60,12 +70,12 @@ let newHover = (~rootUri, ~file: SharedTypes.file, ~getModule, loc) => {
let%opt md = Hashtbl.find_opt(file.stamps.modules, stamp);
let%opt (file, declared) =
References.resolveModuleReference(~file, ~getModule, md);
let name =
let (name, docstring) =
switch (declared) {
| Some(d) => d.name.txt
| None => file.moduleName
| Some(d) => (d.name.txt, d.docstring)
| None => (file.moduleName, file.contents.docstring)
};
showModule(~name, ~file, declared);
showModule(~docstring, ~name, ~file, declared);
| LModule(GlobalReference(moduleName, path, tip)) =>
let%opt file = getModule(moduleName);
let env = {Query.file, exported: file.contents.exported};
Expand All @@ -74,16 +84,21 @@ let newHover = (~rootUri, ~file: SharedTypes.file, ~getModule, loc) => {
let%opt md = Hashtbl.find_opt(file.stamps.modules, stamp);
let%opt (file, declared) =
References.resolveModuleReference(~file, ~getModule, md);
let name =
let (name, docstring) =
switch (declared) {
| Some(d) => d.name.txt
| None => file.moduleName
| Some(d) => (d.name.txt, d.docstring)
| None => (file.moduleName, file.contents.docstring)
};
showModule(~name, ~file, declared);
showModule(~docstring, ~name, ~file, declared);
| LModule(NotFound) => None
| TopLevelModule(name) =>
let%opt file = getModule(name);
showModule(~name=file.moduleName, ~file, None);
showModule(
~docstring=file.contents.docstring,
~name=file.moduleName,
~file,
None,
);
| Typed(_, Definition(_, Field(_) | Constructor(_))) => None
| Constant(t) =>
Some(
Expand Down
20 changes: 17 additions & 3 deletions src/rescript-editor-support/ProcessCmt.re
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ and forSignatureType = (env, signature) => {
signature,
[],
);
{exported, topLevel};
{docstring: None, exported, topLevel};
}
and forModuleType = (env, moduleType) =>
switch (moduleType) {
Expand Down Expand Up @@ -370,7 +370,14 @@ let forSignature = (~env, items) => {
let exported = initExported();
let topLevel =
items |> List.map(forSignatureItem(~env, ~exported)) |> List.flatten;
{exported, topLevel};
let attributes =
switch (items) {
| [{sig_desc: Tsig_attribute(attribute)}, ..._] => [attribute]
| _ => []
};
let docstring =
ProcessAttributes.findDocAttribute(attributes) |?>> env.processDoc;
{docstring, exported, topLevel};
};

let forTreeModuleType = (~env, {mty_desc}) =>
Expand Down Expand Up @@ -554,7 +561,14 @@ and forStructure = (~env, items) => {
items,
[],
);
{exported, topLevel};
let attributes =
switch (items) {
| [{str_desc: Tstr_attribute(attribute)}, ..._] => [attribute]
| _ => []
};
let docstring =
ProcessAttributes.findDocAttribute(attributes) |?>> env.processDoc;
{docstring, exported, topLevel};
};

let forCmt =
Expand Down
4 changes: 3 additions & 1 deletion src/rescript-editor-support/SharedTypes.re
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ type moduleItem =
| MType(Type.t, Types.rec_status)
| Module(moduleKind)
and moduleContents = {
docstring: option(string),
exported,
mutable topLevel: list(declared(moduleItem)),
topLevel: list(declared(moduleItem)),
}
and moduleKind =
| Ident(Path.t)
Expand Down Expand Up @@ -176,6 +177,7 @@ let emptyFile = (moduleName, uri) => {
stamps: initStamps(),
moduleName,
contents: {
docstring: None,
exported: initExported(),
topLevel: [],
},
Expand Down