Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show docstrings from more sources #694

Merged
merged 6 commits into from
Jan 12, 2023
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Prefer opened `Belt` modules in autocomplete when `-open Belt` is detected in `bsconfig`. https://github.com/rescript-lang/rescript-vscode/pull/673
- Improve precision in signature help. You now do not need to type anything into the argument for it to highlight. https://github.com/rescript-lang/rescript-vscode/pull/675
- Remove redundant function name in signature help, to clean up what's shown to the user some. https://github.com/rescript-lang/rescript-vscode/pull/678
- Show docstrings in hover for record fields and variant constructors. https://github.com/rescript-lang/rescript-vscode/pull/694

#### :bug: Bug Fix

Expand Down
67 changes: 29 additions & 38 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ let completionForExporteds iterExported getDeclared ~prefix ~exact ~env
Hashtbl.add namesUsed declared.name.txt ();
res :=
{
(Completion.create ~name:declared.name.txt ~env
(Completion.create declared.name.txt ~env
~kind:(transformContents declared.item))
with
deprecated = declared.deprecated;
Expand Down Expand Up @@ -597,7 +597,7 @@ let completionsForExportedConstructors ~(env : QueryEnv.t) ~prefix ~exact
if not (Hashtbl.mem namesUsed name) then
let () = Hashtbl.add namesUsed name () in
Some
(Completion.create ~name ~env
(Completion.create name ~env ~docstring:c.docstring
~kind:
(Completion.Constructor
(c, t.item.decl |> Shared.declToString t.name.txt)))
Expand All @@ -619,7 +619,7 @@ let completionForExportedFields ~(env : QueryEnv.t) ~prefix ~exact ~namesUsed =
if not (Hashtbl.mem namesUsed name) then
let () = Hashtbl.add namesUsed name () in
Some
(Completion.create ~name ~env
(Completion.create name ~env ~docstring:f.docstring
~kind:
(Completion.Field
(f, t.item.decl |> Shared.declToString t.name.txt)))
Expand Down Expand Up @@ -800,8 +800,7 @@ let processLocalValue name loc ~prefix ~exact ~env
Hashtbl.add localTables.namesUsed name ();
localTables.resultRev <-
{
(Completion.create ~name:declared.name.txt ~env
~kind:(Value declared.item))
(Completion.create declared.name.txt ~env ~kind:(Value declared.item))
with
deprecated = declared.deprecated;
docstring = declared.docstring;
Expand All @@ -812,7 +811,7 @@ let processLocalValue name loc ~prefix ~exact ~env
(Printf.sprintf "Completion Value Not Found %s loc:%s\n" name
(Loc.toString loc));
localTables.resultRev <-
Completion.create ~name ~env
Completion.create name ~env
~kind:
(Value
(Ctype.newconstr
Expand All @@ -831,7 +830,7 @@ let processLocalConstructor name loc ~prefix ~exact ~env
Hashtbl.add localTables.namesUsed name ();
localTables.resultRev <-
{
(Completion.create ~name:declared.name.txt ~env
(Completion.create declared.name.txt ~env
~kind:
(Constructor
( declared.item,
Expand All @@ -856,8 +855,7 @@ let processLocalType name loc ~prefix ~exact ~env ~(localTables : LocalTables.t)
Hashtbl.add localTables.namesUsed name ();
localTables.resultRev <-
{
(Completion.create ~name:declared.name.txt ~env
~kind:(Type declared.item))
(Completion.create declared.name.txt ~env ~kind:(Type declared.item))
with
deprecated = declared.deprecated;
docstring = declared.docstring;
Expand All @@ -877,7 +875,7 @@ let processLocalModule name loc ~prefix ~exact ~env
Hashtbl.add localTables.namesUsed name ();
localTables.resultRev <-
{
(Completion.create ~name:declared.name.txt ~env
(Completion.create declared.name.txt ~env
~kind:(Module declared.item))
with
deprecated = declared.deprecated;
Expand Down Expand Up @@ -1139,7 +1137,7 @@ let getComplementaryCompletionsForTypedValue ~opens ~allFiles ~scope ~env prefix
(String.contains name '-')
then
Some
(Completion.create ~name ~env ~kind:(Completion.FileModule name))
(Completion.create name ~env ~kind:(Completion.FileModule name))
else None)
in
localCompletionsWithOpens @ fileModules
Expand All @@ -1163,8 +1161,7 @@ let getCompletionsForPath ~package ~opens ~allFiles ~pos ~exact ~scope
(String.contains name '-')
then
Some
(Completion.create ~name ~env
~kind:(Completion.FileModule name))
(Completion.create name ~env ~kind:(Completion.FileModule name))
else None)
in
localCompletionsWithOpens @ fileModules
Expand Down Expand Up @@ -1350,28 +1347,28 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
match contextPath with
| CPString ->
[
Completion.create ~name:"string" ~env
Completion.create "string" ~env
~kind:
(Completion.Value
(Ctype.newconstr (Path.Pident (Ident.create "string")) []));
]
| CPInt ->
[
Completion.create ~name:"int" ~env
Completion.create "int" ~env
~kind:
(Completion.Value
(Ctype.newconstr (Path.Pident (Ident.create "int")) []));
]
| CPFloat ->
[
Completion.create ~name:"float" ~env
Completion.create "float" ~env
~kind:
(Completion.Value
(Ctype.newconstr (Path.Pident (Ident.create "float")) []));
]
| CPArray ->
[
Completion.create ~name:"array" ~env
Completion.create "array" ~env
~kind:
(Completion.Value
(Ctype.newconstr (Path.Pident (Ident.create "array")) []));
Expand Down Expand Up @@ -1418,7 +1415,7 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
| args, tRet when args <> [] ->
let args = processApply args labels in
let retType = reconstructFunctionType args tRet in
[Completion.create ~name:"dummy" ~env ~kind:(Completion.Value retType)]
[Completion.create "dummy" ~env ~kind:(Completion.Value retType)]
| _ -> [])
| None -> [])
| CPField (CPId (path, Module), fieldName) ->
Expand All @@ -1440,7 +1437,8 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
|> Utils.filterMap (fun field ->
if checkName field.fname.txt ~prefix:fieldName ~exact then
Some
(Completion.create ~name:field.fname.txt ~env
(Completion.create field.fname.txt ~env
~docstring:field.docstring
~kind:
(Completion.Field
( field,
Expand Down Expand Up @@ -1472,8 +1470,7 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
|> Utils.filterMap (fun (field, typ) ->
if checkName field ~prefix:label ~exact then
Some
(Completion.create ~name:field ~env
~kind:(Completion.ObjLabel typ))
(Completion.create field ~env ~kind:(Completion.ObjLabel typ))
else None)
| None -> [])
| None -> [])
Expand Down Expand Up @@ -1655,7 +1652,7 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
in
if List.length ctxPaths = List.length typeExrps then
[
Completion.create ~name:"dummy" ~env
Completion.create "dummy" ~env
~kind:(Completion.Value (Ctype.newty (Ttuple typeExrps)));
]
else []
Expand All @@ -1674,7 +1671,7 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
| None -> []
| Some (_, typ, env) ->
[
Completion.create ~name:"dummy" ~env
Completion.create "dummy" ~env
~kind:(Completion.Value (Utils.unwrapIfOption typ));
])
| CArgument {functionContextPath; argumentLabel} -> (
Expand Down Expand Up @@ -1707,7 +1704,7 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
| None -> []
| Some (_, typ) ->
[
Completion.create ~name:"dummy" ~env
Completion.create "dummy" ~env
~kind:
(Completion.Value
(if expandOption then Utils.unwrapIfOption typ else typ));
Expand Down Expand Up @@ -1801,12 +1798,8 @@ let rec completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
match t |> extractType ~env ~package:full.package with
| Some (Tbool env) ->
[
Completion.create ~name:"true"
~kind:(Label (t |> Shared.typeToString))
~env;
Completion.create ~name:"false"
~kind:(Label (t |> Shared.typeToString))
~env;
Completion.create "true" ~kind:(Label (t |> Shared.typeToString)) ~env;
Completion.create "false" ~kind:(Label (t |> Shared.typeToString)) ~env;
]
|> filterItems ~prefix
| Some (Tvariant {env; constructors; variantDecl; variantName}) ->
Expand Down Expand Up @@ -1865,9 +1858,7 @@ let rec completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
})
in
[
Completion.create ~name:"None"
~kind:(Label (t |> Shared.typeToString))
~env;
Completion.create "None" ~kind:(Label (t |> Shared.typeToString)) ~env;
Completion.createWithSnippet ~name:"Some(_)"
~kind:(Label (t |> Shared.typeToString))
~env ~insertText:"Some(${1:_})" ();
Expand All @@ -1892,7 +1883,7 @@ let rec completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
|> List.filter (fun (field : field) ->
List.mem field.fname.txt seenFields = false)
|> List.map (fun (field : field) ->
Completion.create ~name:field.fname.txt
Completion.create field.fname.txt
~kind:(Field (field, typeExpr |> Shared.typeToString))
~env)
|> filterItems ~prefix
Expand Down Expand Up @@ -1998,7 +1989,7 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
| Cjsx ([id], prefix, identsSeen) when String.uncapitalize_ascii id = id ->
(* Lowercase JSX tag means builtin *)
let mkLabel (name, typString) =
Completion.create ~name ~kind:(Label typString) ~env
Completion.create name ~kind:(Label typString) ~env
in
let keyLabels =
if Utils.startsWith "key" prefix then [mkLabel ("key", "string")] else []
Expand All @@ -2012,7 +2003,7 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
| Cjsx (componentPath, prefix, identsSeen) ->
let labels = getJsxLabels ~componentPath ~findTypeOfValue ~package in
let mkLabel_ name typString =
Completion.create ~name ~kind:(Label typString) ~env
Completion.create name ~kind:(Label typString) ~env
in
let mkLabel (name, typ, _env) =
mkLabel_ name (typ |> Shared.typeToString)
Expand All @@ -2031,7 +2022,7 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
@ keyLabels
| Cdecorator prefix ->
let mkDecorator (name, docstring) =
{(Completion.create ~name ~kind:(Label "") ~env) with docstring}
{(Completion.create name ~kind:(Label "") ~env) with docstring}
in
[
( "as",
Expand Down Expand Up @@ -2288,7 +2279,7 @@ Note: The `@react.component` decorator requires the react-jsx config to be set i
| None -> []
in
let mkLabel (name, typ) =
Completion.create ~name ~kind:(Label (typ |> Shared.typeToString)) ~env
Completion.create name ~kind:(Label (typ |> Shared.typeToString)) ~env
in
labels
|> List.filter (fun (name, _t) ->
Expand Down
27 changes: 17 additions & 10 deletions analysis/src/Hover.ml
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ let findRelevantTypesFromType ~file ~package typ =
constructors |> List.filter_map (fromConstructorPath ~env:envToSearch)

(* Produces a hover with relevant types expanded in the main type being hovered. *)
let hoverWithExpandedTypes ~docstring ~file ~package ~supportsMarkdownLinks typ
=
let hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ =
let typeString = Markdown.codeBlock (typ |> Shared.typeToString) in
let types = findRelevantTypesFromType typ ~file ~package in
let typeDefinitions =
Expand All @@ -114,7 +113,7 @@ let hoverWithExpandedTypes ~docstring ~file ~package ~supportsMarkdownLinks typ
(SharedTypes.pathIdentToString path))
^ linkToTypeDefinitionStr ^ "\n")
in
(typeString :: typeDefinitions |> String.concat "\n", docstring)
typeString :: typeDefinitions |> String.concat "\n"

(* Leverages autocomplete functionality to produce a hover for a position. This
makes it (most often) work with unsaved content. *)
Expand Down Expand Up @@ -151,12 +150,20 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
@ docstring
in
Some (Protocol.stringifyHover (String.concat "\n\n" parts))
| {kind = Field _; docstring} :: _ -> (
match CompletionBackEnd.completionsGetTypeEnv completions with
| Some (typ, _env) ->
let typeString =
hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ
in
let parts = typeString :: docstring in
Some (Protocol.stringifyHover (String.concat "\n\n" parts))
| None -> None)
| _ -> (
match CompletionBackEnd.completionsGetTypeEnv completions with
| Some (typ, _env) ->
let typeString, _docstring =
hoverWithExpandedTypes ~docstring:"" ~file ~package
~supportsMarkdownLinks typ
let typeString =
hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ
in
Some (Protocol.stringifyHover typeString)
| None -> None))))
Expand Down Expand Up @@ -221,8 +228,8 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
| Const_nativeint _ -> "int"))
| Typed (_, t, locKind) ->
let fromType ~docstring typ =
hoverWithExpandedTypes ~docstring ~file ~package ~supportsMarkdownLinks
typ
( hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ,
docstring )
in
let parts =
match References.definedForLoc ~file ~package locKind with
Expand All @@ -234,7 +241,7 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
| `Declared ->
let typeString, docstring = t |> fromType ~docstring in
typeString :: docstring
| `Constructor {cname = {txt}; args} ->
| `Constructor {cname = {txt}; args; docstring} ->
let typeString, docstring = t |> fromType ~docstring in
let argsString =
match args with
Expand All @@ -244,7 +251,7 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
|> List.map (fun (t, _) -> Shared.typeToString t)
|> String.concat ", " |> Printf.sprintf "(%s)"
in
typeString :: Markdown.codeBlock (txt ^ argsString) :: docstring
(Markdown.codeBlock (txt ^ argsString) :: docstring) @ [typeString]
| `Field ->
let typeString, docstring = t |> fromType ~docstring in
typeString :: docstring)
Expand Down
Loading