From 2f384594c22676c9fcdf8b046a7f49f69d308d2d Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Thu, 20 Feb 2025 20:21:15 +0000 Subject: [PATCH] Better completion for applications (#7291) * WIP: better completion for applications * Comment out scope thing * Uncomment filter * Remove verbose * Optional dig * Add changelog entry --- CHANGELOG.md | 1 + analysis/src/CompletionBackEnd.ml | 3 +- analysis/src/TypeUtils.ml | 4 +- .../tests/src/CompletionTaggedTemplate.res | 10 +-- .../tests/src/expected/Completion.res.txt | 10 ++- .../expected/CompletionTaggedTemplate.res.txt | 65 +++++++++++++------ 6 files changed, 63 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7a4e157a2..67dcfc7a42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ - Stdlib namespace for Core modules (fixes name clashes with user modules). https://github.com/rescript-lang/rescript/pull/7285 - Fix runtime type check for Object in untagged variants when one variant case is `null`. https://github.com/rescript-lang/rescript/pull/7303 - Fix files that were being truncated when sent to the CDN over FTP. https://github.com/rescript-lang/rescript/pull/7306 +- Fix better editor completion for applications. https://github.com/rescript-lang/rescript/pull/7291 #### :house: Internal diff --git a/analysis/src/CompletionBackEnd.ml b/analysis/src/CompletionBackEnd.ml index 1d618ae9ed..c7e5713123 100644 --- a/analysis/src/CompletionBackEnd.ml +++ b/analysis/src/CompletionBackEnd.ml @@ -965,7 +965,8 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact | [], [(Nolabel | Labelled _ | Optional _)] -> (* should not happen, but just ignore extra arguments *) [] in - match TypeUtils.extractFunctionType ~env ~package typ with + + match TypeUtils.extractFunctionType ~env ~package ~digInto:false typ with | args, tRet when args <> [] -> let args = processApply args labels in let retType = reconstructFunctionType args tRet in diff --git a/analysis/src/TypeUtils.ml b/analysis/src/TypeUtils.ml index 783685d551..c9b1acd011 100644 --- a/analysis/src/TypeUtils.ml +++ b/analysis/src/TypeUtils.ml @@ -243,12 +243,12 @@ let rec extractObjectType ~env ~package (t : Types.type_expr) = | _ -> None) | _ -> None -let extractFunctionType ~env ~package typ = +let extractFunctionType ~env ~package ?(digInto = true) typ = let rec loop ~env acc (t : Types.type_expr) = match t.desc with | Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> loop ~env acc t1 | Tarrow (label, tArg, tRet, _, _) -> loop ~env ((label, tArg) :: acc) tRet - | Tconstr (path, typeArgs, _) -> ( + | Tconstr (path, typeArgs, _) when digInto -> ( match References.digConstructor ~env ~package path with | Some ( env, diff --git a/tests/analysis_tests/tests/src/CompletionTaggedTemplate.res b/tests/analysis_tests/tests/src/CompletionTaggedTemplate.res index dce1a0cbcd..df033cd7c8 100644 --- a/tests/analysis_tests/tests/src/CompletionTaggedTemplate.res +++ b/tests/analysis_tests/tests/src/CompletionTaggedTemplate.res @@ -1,5 +1,5 @@ module M = { - type t + type t = promise let a = (_t:t) => 4 let b = (_:t) => "c" @@ -9,8 +9,10 @@ module M = { @module("meh") @taggedTemplate external meh: (array, array) => M.t = "default" +let w = meh`` + +// let _ = w. +// ^com + // let x = meh`foo`. // ^com - -// let y = meh`bar`.x -// ^com diff --git a/tests/analysis_tests/tests/src/expected/Completion.res.txt b/tests/analysis_tests/tests/src/expected/Completion.res.txt index ea276bdb69..b2aa5b483f 100644 --- a/tests/analysis_tests/tests/src/expected/Completion.res.txt +++ b/tests/analysis_tests/tests/src/expected/Completion.res.txt @@ -1841,13 +1841,19 @@ Resolved opens 3 Stdlib Completion Completion ContextPath Value[withCallback](~a) ContextPath Value[withCallback] Path withCallback -Found type for function (~b: int) => int +Found type for function (~b: int) => callback [{ "label": "b", "kind": 4, "tags": [], "detail": "int", "documentation": null + }, { + "label": "a", + "kind": 4, + "tags": [], + "detail": "int", + "documentation": null }] Complete src/Completion.res 332:21 @@ -1860,7 +1866,7 @@ Resolved opens 3 Stdlib Completion Completion ContextPath Value[withCallback](~b) ContextPath Value[withCallback] Path withCallback -Found type for function (~a: int) => int +Found type for function callback [{ "label": "a", "kind": 4, diff --git a/tests/analysis_tests/tests/src/expected/CompletionTaggedTemplate.res.txt b/tests/analysis_tests/tests/src/expected/CompletionTaggedTemplate.res.txt index ce42cdebe0..1cbc945f83 100644 --- a/tests/analysis_tests/tests/src/expected/CompletionTaggedTemplate.res.txt +++ b/tests/analysis_tests/tests/src/expected/CompletionTaggedTemplate.res.txt @@ -1,16 +1,15 @@ -Complete src/CompletionTaggedTemplate.res 11:20 -posCursor:[11:20] posNoWhite:[11:19] Found expr:[11:11->0:-1] -Completable: Cpath Value[meh](Nolabel, Nolabel)."" +Complete src/CompletionTaggedTemplate.res 13:13 +posCursor:[13:13] posNoWhite:[13:12] Found expr:[13:11->13:13] +Pexp_field [13:11->13:12] _:[19:0->13:13] +Completable: Cpath Value[w]."" Package opens Stdlib.place holder Pervasives.JsxModules.place holder Resolved opens 1 Stdlib -ContextPath Value[meh](Nolabel, Nolabel)."" -ContextPath Value[meh](Nolabel, Nolabel) -ContextPath Value[meh] -Path meh -ContextPath Value[meh](Nolabel, Nolabel, Nolabel)-> -ContextPath Value[meh](Nolabel, Nolabel, Nolabel) -ContextPath Value[meh] -Path meh +ContextPath Value[w]."" +ContextPath Value[w] +Path w +ContextPath Value[w]-> +ContextPath Value[w] +Path w CPPipe pathFromEnv:M found:true Path M. [{ @@ -22,7 +21,7 @@ Path M. "sortText": "xyz", "insertText": "->M.xyz", "additionalTextEdits": [{ - "range": {"start": {"line": 11, "character": 19}, "end": {"line": 11, "character": 20}}, + "range": {"start": {"line": 13, "character": 12}, "end": {"line": 13, "character": 13}}, "newText": "" }] }, { @@ -34,7 +33,7 @@ Path M. "sortText": "b", "insertText": "->M.b", "additionalTextEdits": [{ - "range": {"start": {"line": 11, "character": 19}, "end": {"line": 11, "character": 20}}, + "range": {"start": {"line": 13, "character": 12}, "end": {"line": 13, "character": 13}}, "newText": "" }] }, { @@ -46,26 +45,26 @@ Path M. "sortText": "a", "insertText": "->M.a", "additionalTextEdits": [{ - "range": {"start": {"line": 11, "character": 19}, "end": {"line": 11, "character": 20}}, + "range": {"start": {"line": 13, "character": 12}, "end": {"line": 13, "character": 13}}, "newText": "" }] }] -Complete src/CompletionTaggedTemplate.res 14:21 -posCursor:[14:21] posNoWhite:[14:20] Found expr:[14:11->14:21] -Completable: Cpath Value[meh](Nolabel, Nolabel).x +Complete src/CompletionTaggedTemplate.res 16:20 +posCursor:[16:20] posNoWhite:[16:19] Found expr:[16:11->0:-1] +Completable: Cpath Value[meh](Nolabel, Nolabel)."" Package opens Stdlib.place holder Pervasives.JsxModules.place holder Resolved opens 1 Stdlib -ContextPath Value[meh](Nolabel, Nolabel).x +ContextPath Value[meh](Nolabel, Nolabel)."" ContextPath Value[meh](Nolabel, Nolabel) ContextPath Value[meh] Path meh -ContextPath Value[meh](Nolabel, Nolabel, Nolabel)->x +ContextPath Value[meh](Nolabel, Nolabel, Nolabel)-> ContextPath Value[meh](Nolabel, Nolabel, Nolabel) ContextPath Value[meh] Path meh CPPipe pathFromEnv:M found:true -Path M.x +Path M. [{ "label": "->M.xyz", "kind": 12, @@ -75,7 +74,31 @@ Path M.x "sortText": "xyz", "insertText": "->M.xyz", "additionalTextEdits": [{ - "range": {"start": {"line": 14, "character": 19}, "end": {"line": 14, "character": 20}}, + "range": {"start": {"line": 16, "character": 19}, "end": {"line": 16, "character": 20}}, + "newText": "" + }] + }, { + "label": "->M.b", + "kind": 12, + "tags": [], + "detail": "t => string", + "documentation": null, + "sortText": "b", + "insertText": "->M.b", + "additionalTextEdits": [{ + "range": {"start": {"line": 16, "character": 19}, "end": {"line": 16, "character": 20}}, + "newText": "" + }] + }, { + "label": "->M.a", + "kind": 12, + "tags": [], + "detail": "t => int", + "documentation": null, + "sortText": "a", + "insertText": "->M.a", + "additionalTextEdits": [{ + "range": {"start": {"line": 16, "character": 19}, "end": {"line": 16, "character": 20}}, "newText": "" }] }]