From cb6b1a04c91ffe447985a409d94b3221f98551d9 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 11 Mar 2023 09:30:18 +0100 Subject: [PATCH 1/3] leverage new unsaved code completion features to provide hover on (some) unsaved code --- analysis/src/Commands.ml | 2 +- analysis/src/Completions.ml | 2 +- analysis/src/Hover.ml | 25 +++++++++--- analysis/tests/src/Hover.res | 10 ++++- .../tests/src/expected/Completion.res.txt | 1 + analysis/tests/src/expected/Hover.res.txt | 38 +++++++++++++++---- 6 files changed, 60 insertions(+), 18 deletions(-) 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..cf7cdd889 100644 --- a/analysis/tests/src/Hover.res +++ b/analysis/tests/src/Hover.res @@ -129,7 +129,7 @@ let arity0a = (. ()) => { f } -let arity0b = (. (), . ()) => 3 +let arity0b = (. ()) => (. ()) => 3 // ^hov let arity0c = (. (), ()) => 3 @@ -211,7 +211,6 @@ let usr: useR = { // let f = usr // ^hov - module NotShadowed = { /** Stuff */ let xx_ = 10 @@ -253,3 +252,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..e3ca5c780 100644 --- a/analysis/tests/src/expected/Hover.res.txt +++ b/analysis/tests/src/expected/Hover.res.txt @@ -191,25 +191,47 @@ ContextPath Value[usr] Path usr {"contents": {"kind": "markdown", "value": "```rescript\nuseR\n```\n\n---\n\n```\n \n```\n```rescript\ntype useR = {x: int, y: list>>}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C200%2C0%5D)\n\n\n---\n\n```\n \n```\n```rescript\ntype r<'a> = {i: 'a, f: float}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C101%2C0%5D)\n"}} -Hover src/Hover.res 230:20 +Hover src/Hover.res 229:20 {"contents": {"kind": "markdown", "value": "```rescript\nint\n```\n\n More Stuff "}} -Hover src/Hover.res 233:17 +Hover src/Hover.res 232:17 {"contents": {"kind": "markdown", "value": "```rescript\nint\n```\n\n More Stuff "}} -Hover src/Hover.res 245:6 +Hover src/Hover.res 244:6 Nothing at that position. Now trying to use completion. -posCursor:[245:6] posNoWhite:[245:5] Found expr:[245:3->245:14] -Pexp_field [245:3->245:4] someField:[245:5->245:14] +posCursor:[244:6] posNoWhite:[244:5] Found expr:[244:3->244:14] +Pexp_field [244:3->244:4] someField:[244:5->244:14] Completable: Cpath Value[x].someField ContextPath Value[x].someField ContextPath Value[x] Path x {"contents": {"kind": "markdown", "value": "```rescript\nbool\n```\n\n Mighty fine field here. "}} -Hover src/Hover.res 248:19 +Hover src/Hover.res 247:19 {"contents": {"kind": "markdown", "value": "```rescript\nbool\n```\n\n Mighty fine field here. "}} -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 252: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%2C250%2C0%5D)\n"}} + +Hover src/Hover.res 256:23 +Nothing at that position. Now trying to use completion. +posCursor:[256:23] posNoWhite:[256:22] Found expr:[256:22->256:25] +Pexp_ident fff:[256:22->256:25] +Completable: Cpath Value[fff] +ContextPath Value[fff] +Path fff +ContextPath string +{"contents": {"kind": "markdown", "value": "```rescript\nstring\n```"}} + +Hover src/Hover.res 259:33 +Nothing at that position. Now trying to use completion. +posCursor:[259:33] posNoWhite:[259:32] Found expr:[259:31->259:40] +Pexp_ident someField:[259:31->259: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```"}} From 75299b738938b0cf94cd2cf59a33cf40d2427712 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 11 Mar 2023 09:50:53 +0100 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 1 + analysis/tests/src/Hover.res | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) 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/tests/src/Hover.res b/analysis/tests/src/Hover.res index cf7cdd889..b66c4e68e 100644 --- a/analysis/tests/src/Hover.res +++ b/analysis/tests/src/Hover.res @@ -129,7 +129,7 @@ let arity0a = (. ()) => { f } -let arity0b = (. ()) => (. ()) => 3 +let arity0b = (. (), . ()) => 3 // ^hov let arity0c = (. (), ()) => 3 From 3828bfcec11ffc3871fa4078b985a0dee5e0ffcf Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 11 Mar 2023 09:51:39 +0100 Subject: [PATCH 3/3] revert accidental change --- analysis/tests/src/Hover.res | 1 + analysis/tests/src/expected/Hover.res.txt | 28 +++++++++++------------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/analysis/tests/src/Hover.res b/analysis/tests/src/Hover.res index b66c4e68e..0657759a4 100644 --- a/analysis/tests/src/Hover.res +++ b/analysis/tests/src/Hover.res @@ -211,6 +211,7 @@ let usr: useR = { // let f = usr // ^hov + module NotShadowed = { /** Stuff */ let xx_ = 10 diff --git a/analysis/tests/src/expected/Hover.res.txt b/analysis/tests/src/expected/Hover.res.txt index e3ca5c780..95c655a27 100644 --- a/analysis/tests/src/expected/Hover.res.txt +++ b/analysis/tests/src/expected/Hover.res.txt @@ -191,42 +191,42 @@ ContextPath Value[usr] Path usr {"contents": {"kind": "markdown", "value": "```rescript\nuseR\n```\n\n---\n\n```\n \n```\n```rescript\ntype useR = {x: int, y: list>>}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C200%2C0%5D)\n\n\n---\n\n```\n \n```\n```rescript\ntype r<'a> = {i: 'a, f: float}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C101%2C0%5D)\n"}} -Hover src/Hover.res 229:20 +Hover src/Hover.res 230:20 {"contents": {"kind": "markdown", "value": "```rescript\nint\n```\n\n More Stuff "}} -Hover src/Hover.res 232:17 +Hover src/Hover.res 233:17 {"contents": {"kind": "markdown", "value": "```rescript\nint\n```\n\n More Stuff "}} -Hover src/Hover.res 244:6 +Hover src/Hover.res 245:6 Nothing at that position. Now trying to use completion. -posCursor:[244:6] posNoWhite:[244:5] Found expr:[244:3->244:14] -Pexp_field [244:3->244:4] someField:[244:5->244:14] +posCursor:[245:6] posNoWhite:[245:5] Found expr:[245:3->245:14] +Pexp_field [245:3->245:4] someField:[245:5->245:14] Completable: Cpath Value[x].someField ContextPath Value[x].someField ContextPath Value[x] Path x {"contents": {"kind": "markdown", "value": "```rescript\nbool\n```\n\n Mighty fine field here. "}} -Hover src/Hover.res 247:19 +Hover src/Hover.res 248:19 {"contents": {"kind": "markdown", "value": "```rescript\nbool\n```\n\n Mighty fine field here. "}} -Hover src/Hover.res 252: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%2C250%2C0%5D)\n"}} +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 256:23 +Hover src/Hover.res 257:23 Nothing at that position. Now trying to use completion. -posCursor:[256:23] posNoWhite:[256:22] Found expr:[256:22->256:25] -Pexp_ident fff:[256:22->256:25] +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 259:33 +Hover src/Hover.res 260:33 Nothing at that position. Now trying to use completion. -posCursor:[259:33] posNoWhite:[259:32] Found expr:[259:31->259:40] -Pexp_ident someField:[259:31->259:40] +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