diff --git a/CHANGELOG.md b/CHANGELOG.md index 50c1dd84d..6bc2e93c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ #### :rocket: New Feature - Greatly extend completion abilities for unsaved code. WARNING: Might be a bit unstable initially. Report any issues you see. https://github.com/rescript-lang/rescript-vscode/pull/712 +- Provide hovers for more unsaved code via the new completion features. https://github.com/rescript-lang/rescript-vscode/pull/749 ## 1.14.0 diff --git a/analysis/src/Commands.ml b/analysis/src/Commands.ml index aa4b766ad..4341c4f82 100644 --- a/analysis/src/Commands.ml +++ b/analysis/src/Commands.ml @@ -4,7 +4,7 @@ let completion ~debug ~path ~pos ~currentFile = Completions.getCompletions ~debug ~path ~pos ~currentFile ~forHover:false with | None -> [] - | Some (completions, _) -> completions + | Some (completions, _, _) -> completions in print_endline (completions diff --git a/analysis/src/Completions.ml b/analysis/src/Completions.ml index 3b455817b..42176bb3b 100644 --- a/analysis/src/Completions.ml +++ b/analysis/src/Completions.ml @@ -19,4 +19,4 @@ let getCompletions ~debug ~path ~pos ~currentFile ~forHover = |> CompletionBackEnd.processCompletable ~debug ~full ~pos ~scope ~env ~forHover in - Some (completables, full))) + Some (completables, full, scope))) diff --git a/analysis/src/Hover.ml b/analysis/src/Hover.ml index 3ab1a0c74..01ef5de94 100644 --- a/analysis/src/Hover.ml +++ b/analysis/src/Hover.ml @@ -125,7 +125,11 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover ~supportsMarkdownLinks = match Completions.getCompletions ~debug ~path ~pos ~currentFile ~forHover with | None -> None - | Some (completions, {file; package}) -> ( + | Some (completions, ({file; package} as full), scope) -> ( + let rawOpens = Scope.getRawOpens scope in + let allFiles = + FileSet.union package.projectFiles package.dependenciesFiles + in match completions with | {kind = Label typString; docstring} :: _ -> let parts = @@ -133,8 +137,12 @@ 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 + | {kind = Field _; env; docstring} :: _ -> ( + let opens = CompletionBackEnd.getOpens ~debug ~rawOpens ~package ~env in + match + CompletionBackEnd.completionsGetTypeEnv2 ~debug ~full ~rawOpens ~opens + ~allFiles ~pos ~scope completions + with | Some (typ, _env) -> let typeString = hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ @@ -142,14 +150,19 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover let parts = typeString :: docstring in Some (Protocol.stringifyHover (String.concat "\n\n" parts)) | None -> None) - | _ -> ( - match CompletionBackEnd.completionsGetTypeEnv completions with + | {env} :: _ -> ( + let opens = CompletionBackEnd.getOpens ~debug ~rawOpens ~package ~env in + match + CompletionBackEnd.completionsGetTypeEnv2 ~debug ~full ~rawOpens ~opens + ~allFiles ~pos ~scope completions + with | Some (typ, _env) -> let typeString = hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ in Some (Protocol.stringifyHover typeString) - | None -> None)) + | None -> None) + | _ -> None) let newHover ~full:{file; package} ~supportsMarkdownLinks locItem = match locItem.locType with diff --git a/analysis/tests/src/Hover.res b/analysis/tests/src/Hover.res index 739482f9a..0657759a4 100644 --- a/analysis/tests/src/Hover.res +++ b/analysis/tests/src/Hover.res @@ -253,3 +253,10 @@ type variant = | /** Cool variant! */ CoolVariant | /** Other cool variant */ Ot let coolVariant = CoolVariant // ^hov + +// Hover on unsaved +// let fff = "hello"; fff +// ^hov + +// switch x { | {someField} => someField } +// ^hov diff --git a/analysis/tests/src/expected/Completion.res.txt b/analysis/tests/src/expected/Completion.res.txt index 409d7b7b8..de4ca7659 100644 --- a/analysis/tests/src/expected/Completion.res.txt +++ b/analysis/tests/src/expected/Completion.res.txt @@ -1593,6 +1593,7 @@ Raw opens: 2 Shadow.B.place holder ... Shadow.A.place holder Resolved opens 2 Completion.res Completion.res ContextPath Value[FAO, forAutoObject] Path FAO.forAutoObject +Raw opens: 2 Shadow.B.place holder ... Shadow.A.place holder {"contents": {"kind": "markdown", "value": "```rescript\n{\"age\": int, \"forAutoLabel\": FAR.forAutoRecord}\n```"}} Hover src/Completion.res 352:17 diff --git a/analysis/tests/src/expected/Hover.res.txt b/analysis/tests/src/expected/Hover.res.txt index 423f460f9..95c655a27 100644 --- a/analysis/tests/src/expected/Hover.res.txt +++ b/analysis/tests/src/expected/Hover.res.txt @@ -213,3 +213,25 @@ Hover src/Hover.res 248:19 Hover src/Hover.res 253:20 {"contents": {"kind": "markdown", "value": "```rescript\nCoolVariant\n```\n\n Cool variant! \n\n```rescript\nvariant\n```\n\n---\n\n```\n \n```\n```rescript\ntype variant = CoolVariant | OtherCoolVariant\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C251%2C0%5D)\n"}} +Hover src/Hover.res 257:23 +Nothing at that position. Now trying to use completion. +posCursor:[257:23] posNoWhite:[257:22] Found expr:[257:22->257:25] +Pexp_ident fff:[257:22->257:25] +Completable: Cpath Value[fff] +ContextPath Value[fff] +Path fff +ContextPath string +{"contents": {"kind": "markdown", "value": "```rescript\nstring\n```"}} + +Hover src/Hover.res 260:33 +Nothing at that position. Now trying to use completion. +posCursor:[260:33] posNoWhite:[260:32] Found expr:[260:31->260:40] +Pexp_ident someField:[260:31->260:40] +Completable: Cpath Value[someField] +ContextPath Value[someField] +Path someField +ContextPath CPatternPath(Value[x])->recordField(someField) +ContextPath Value[x] +Path x +{"contents": {"kind": "markdown", "value": "```rescript\nbool\n```"}} +