Skip to content

Commit

Permalink
Better completion for applications (#7291)
Browse files Browse the repository at this point in the history
* WIP: better completion for applications

* Comment out scope thing

* Uncomment filter

* Remove verbose

* Optional dig

* Add changelog entry
  • Loading branch information
nojaf authored Feb 20, 2025
1 parent fd1e7b8 commit 2f38459
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions analysis/src/TypeUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions tests/analysis_tests/tests/src/CompletionTaggedTemplate.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module M = {
type t
type t = promise<string>

let a = (_t:t) => 4
let b = (_:t) => "c"
Expand All @@ -9,8 +9,10 @@ module M = {
@module("meh") @taggedTemplate
external meh: (array<string>, array<string>) => M.t = "default"

let w = meh``

// let _ = w.
// ^com

// let x = meh`foo`.
// ^com

// let y = meh`bar`.x
// ^com
10 changes: 8 additions & 2 deletions tests/analysis_tests/tests/src/expected/Completion.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
[{
Expand All @@ -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": ""
}]
}, {
Expand All @@ -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": ""
}]
}, {
Expand All @@ -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,
Expand All @@ -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": ""
}]
}]
Expand Down

0 comments on commit 2f38459

Please sign in to comment.