From 72fe7b01df46044bcf02a1b47a354086403477c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Sun, 25 May 2025 21:21:51 +0100 Subject: [PATCH 1/2] Don't produce duplicate type definitions for recursive types --- analysis/src/Hover.ml | 13 +++++++++++++ tests/analysis_tests/tests/src/Hover.res | 7 +++++++ .../tests/src/expected/Hover.res.txt | 19 +++++++++++-------- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/analysis/src/Hover.ml b/analysis/src/Hover.ml index d7b27ebb28..c66ac8f787 100644 --- a/analysis/src/Hover.ml +++ b/analysis/src/Hover.ml @@ -1,5 +1,7 @@ open SharedTypes +module StringSet = Set.Make (String) + let showModuleTopLevel ~docstring ~isType ~name (topLevel : Module.item list) = let contents = topLevel @@ -115,7 +117,18 @@ let expandTypes ~file ~package ~supportsMarkdownLinks typ = ], `InlineType ) | all -> + let typesSeen = ref StringSet.empty in + let typeId ~(env : QueryEnv.t) ~name = + env.file.moduleName :: List.rev (name :: env.pathRev) |> String.concat "." + in ( all + (* Don't produce duplicate type definitions for recursive types *) + |> List.filter (fun {env; name} -> + let typeId = typeId ~env ~name in + if StringSet.mem typeId !typesSeen then false + else ( + typesSeen := StringSet.add typeId !typesSeen; + true)) |> List.map (fun {decl; env; loc; path} -> let linkToTypeDefinitionStr = if diff --git a/tests/analysis_tests/tests/src/Hover.res b/tests/analysis_tests/tests/src/Hover.res index e245419438..bd40d4e712 100644 --- a/tests/analysis_tests/tests/src/Hover.res +++ b/tests/analysis_tests/tests/src/Hover.res @@ -262,6 +262,13 @@ let payloadVariant = InlineRecord({field1: 1, field2: true}) let payloadVariant2 = Args(1, true) // ^hov +module RecursiveVariants = { + type rec t = Action1(int) | Action2(float) | Batch(array) +} + +let recursiveVariant = RecursiveVariants.Action1(1) +// ^hov + // Hover on unsaved // let fff = "hello"; fff // ^hov diff --git a/tests/analysis_tests/tests/src/expected/Hover.res.txt b/tests/analysis_tests/tests/src/expected/Hover.res.txt index b31b9aa14c..a68c232167 100644 --- a/tests/analysis_tests/tests/src/expected/Hover.res.txt +++ b/tests/analysis_tests/tests/src/expected/Hover.res.txt @@ -309,10 +309,13 @@ Hover src/Hover.res 258:22 Hover src/Hover.res 261:23 {"contents": {"kind": "markdown", "value": "```rescript\npayloadVariants\nArgs(int, bool)\n```\n\n---\n\n```\n \n```\n```rescript\ntype payloadVariants =\n | InlineRecord({field1: int, field2: bool})\n | Args(int, bool)\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C256%2C0%5D)\n"}} -Hover src/Hover.res 265:23 +Hover src/Hover.res 268:42 +{"contents": {"kind": "markdown", "value": "```rescript\nRecursiveVariants.t\nAction1(int)\n```\n\n---\n\n```\n \n```\n```rescript\ntype RecursiveVariants.t =\n | Action1(int)\n | Action2(float)\n | Batch(array)\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C265%2C2%5D)\n"}} + +Hover src/Hover.res 272:23 Nothing at that position. Now trying to use completion. -posCursor:[265:23] posNoWhite:[265:22] Found expr:[265:22->265:25] -Pexp_ident fff:[265:22->265:25] +posCursor:[272:23] posNoWhite:[272:22] Found expr:[272:22->272:25] +Pexp_ident fff:[272:22->272:25] Completable: Cpath Value[fff] Package opens Stdlib.place holder Pervasives.JsxModules.place holder Resolved opens 1 Stdlib @@ -323,10 +326,10 @@ Resolved opens 1 Stdlib ContextPath string {"contents": {"kind": "markdown", "value": "```rescript\nstring\n```"}} -Hover src/Hover.res 268:33 +Hover src/Hover.res 275:33 Nothing at that position. Now trying to use completion. -posCursor:[268:33] posNoWhite:[268:32] Found expr:[268:31->268:40] -Pexp_ident someField:[268:31->268:40] +posCursor:[275:33] posNoWhite:[275:32] Found expr:[275:31->275:40] +Pexp_ident someField:[275:31->275:40] Completable: Cpath Value[someField] Package opens Stdlib.place holder Pervasives.JsxModules.place holder Resolved opens 1 Stdlib @@ -339,9 +342,9 @@ ContextPath Value[x] Path x {"contents": {"kind": "markdown", "value": "```rescript\nbool\n```"}} -Hover src/Hover.res 271:8 +Hover src/Hover.res 278:8 {"contents": {"kind": "markdown", "value": "\n [`Belt.Array`]()\n\n **mutable array**: Utilities functions\n\n```rescript\nmodule Array: {\n module Id\n module Array\n module SortArray\n module MutableQueue\n module MutableStack\n module List\n module Range\n module Set\n module Map\n module MutableSet\n module MutableMap\n module HashSet\n module HashMap\n module Option\n module Result\n module Int\n module Float\n}\n```"}} -Hover src/Hover.res 274:6 +Hover src/Hover.res 281:6 {"contents": {"kind": "markdown", "value": "```rescript\ntype aliased = variant\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"}} From d91d592a4295ae94001e5753f8ffe99afdfc3bdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Mon, 26 May 2025 13:24:29 +0100 Subject: [PATCH 2/2] Add CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1b7e8126f..43a73c34ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ - `rescript-tools doc` no longer includes shadowed bindings in its output. https://github.com/rescript-lang/rescript/pull/7497 - Treat `throw` like `raise` in analysis. https://github.com/rescript-lang/rescript/pull/7521 - Fix `index out of bounds` exception thrown in rare cases by `rescript-editor-analysis.exe codeAction` command. https://github.com/rescript-lang/rescript/pull/7523 +- Don't produce duplicate type definitions for recursive types on hover. https://github.com/rescript-lang/rescript/pull/7524 #### :nail_care: Polish