Skip to content

Commit 1e5c1aa

Browse files
committed
handle dot completions on piped idents
1 parent 9bdaed3 commit 1e5c1aa

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,13 +1102,16 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
11021102
DotCompletionUtils.fieldCompletionsForDotCompletion typ ~env ~package
11031103
~prefix:fieldName ?posOfDot ~exact
11041104
in
1105+
(* Get additional completions acting as if this field completion was actually a pipe completion. *)
11051106
let cpAsPipeCompletion =
11061107
Completable.CPPipe
11071108
{
11081109
contextPath =
11091110
(match cp with
11101111
| CPApply (c, args) -> CPApply (c, args @ [Asttypes.Nolabel])
1111-
| c -> c);
1112+
| CPId _ when TypeUtils.isFunctionType ~env ~package typ ->
1113+
CPApply (cp, [Asttypes.Nolabel])
1114+
| _ -> cp);
11121115
id = fieldName;
11131116
inJsx = false;
11141117
lhsLoc = exprLoc;

analysis/src/TypeUtils.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,11 @@ let rec extractType ?(printOpeningDebug = true)
491491
if Debug.verbose () then print_endline "[extract_type]--> miss";
492492
None
493493

494+
let isFunctionType ~env ~package t =
495+
match extractType ~env ~package t with
496+
| Some (Tfunction _, _) -> true
497+
| _ -> false
498+
494499
let findReturnTypeOfFunctionAtLoc loc ~(env : QueryEnv.t) ~full ~debug =
495500
match References.getLocItem ~full ~pos:(loc |> Loc.end_) ~debug with
496501
| Some {locType = Typed (_, typExpr, _)} -> (

analysis/tests/src/DotPipeCompletionSpec.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,11 @@ let xxxx = [1, 2]
8989

9090
// xxxx->Js.Array2.filter(v => v > 10)->Js.Array2.joinWith(",").includ
9191
// ^com
92+
93+
let str = "hello"
94+
95+
// str->Js.String2.toLowerCase.toUpperCa
96+
// ^com
97+
98+
// str->Js.String2.toLowerCase->Js.String2.toUpperCase.toLowerC
99+
// ^com

analysis/tests/src/expected/DotPipeCompletionSpec.res.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,61 @@ Path Js.String2.includ
409409
}]
410410
}]
411411

412+
Complete src/DotPipeCompletionSpec.res 94:40
413+
posCursor:[94:40] posNoWhite:[94:39] Found expr:[94:3->94:40]
414+
posCursor:[94:40] posNoWhite:[94:39] Found expr:[94:8->94:40]
415+
Pexp_field [94:8->94:30] toUpperCa:[94:31->94:40]
416+
Completable: Cpath Value[Js, String2, toLowerCase].toUpperCa
417+
Package opens Pervasives.JsxModules.place holder
418+
Resolved opens 1 pervasives
419+
ContextPath Value[Js, String2, toLowerCase].toUpperCa
420+
ContextPath Value[Js, String2, toLowerCase]
421+
Path Js.String2.toLowerCase
422+
ContextPath Value[Js, String2, toLowerCase](Nolabel)->toUpperCa
423+
ContextPath Value[Js, String2, toLowerCase](Nolabel)
424+
ContextPath Value[Js, String2, toLowerCase]
425+
Path Js.String2.toLowerCase
426+
Path Js.String2.toUpperCa
427+
[{
428+
"label": "->Js.String2.toUpperCase",
429+
"kind": 12,
430+
"tags": [],
431+
"detail": "t => t",
432+
"documentation": {"kind": "markdown", "value": "\n`toUpperCase(str)` converts `str` to upper case using the locale-insensitive\ncase mappings in the Unicode Character Database. Notice that the conversion can\nexpand the number of letters in the result; for example the German ß\ncapitalizes to two Ses in a row.\n\nSee [`String.toUpperCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase)\non MDN.\n\n## Examples\n\n```rescript\nJs.String2.toUpperCase(\"abc\") == \"ABC\"\nJs.String2.toUpperCase(`Straße`) == `STRASSE`\nJs.String2.toUpperCase(`πς`) == `ΠΣ`\n```\n"},
433+
"sortText": "toUpperCase",
434+
"insertText": "->Js.String2.toUpperCase",
435+
"additionalTextEdits": [{
436+
"range": {"start": {"line": 94, "character": 30}, "end": {"line": 94, "character": 31}},
437+
"newText": ""
438+
}]
439+
}]
440+
441+
Complete src/DotPipeCompletionSpec.res 97:63
442+
posCursor:[97:63] posNoWhite:[97:62] Found expr:[97:3->97:63]
443+
posCursor:[97:63] posNoWhite:[97:62] Found expr:[97:32->97:63]
444+
Pexp_field [97:32->97:54] toLowerC:[97:55->97:63]
445+
Completable: Cpath Value[Js, String2, toUpperCase].toLowerC
446+
Package opens Pervasives.JsxModules.place holder
447+
Resolved opens 1 pervasives
448+
ContextPath Value[Js, String2, toUpperCase].toLowerC
449+
ContextPath Value[Js, String2, toUpperCase]
450+
Path Js.String2.toUpperCase
451+
ContextPath Value[Js, String2, toUpperCase](Nolabel)->toLowerC
452+
ContextPath Value[Js, String2, toUpperCase](Nolabel)
453+
ContextPath Value[Js, String2, toUpperCase]
454+
Path Js.String2.toUpperCase
455+
Path Js.String2.toLowerC
456+
[{
457+
"label": "->Js.String2.toLowerCase",
458+
"kind": 12,
459+
"tags": [],
460+
"detail": "t => t",
461+
"documentation": {"kind": "markdown", "value": "\n`toLowerCase(str)` converts `str` to lower case using the locale-insensitive\ncase mappings in the Unicode Character Database. Notice that the conversion can\ngive different results depending upon context, for example with the Greek\nletter sigma, which has two different lower case forms; one when it is the last\ncharacter in a string and another when it is not.\n\nSee [`String.toLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase)\non MDN.\n\n## Examples\n\n```rescript\nJs.String2.toLowerCase(\"ABC\") == \"abc\"\nJs.String2.toLowerCase(`ΣΠ`) == `σπ`\nJs.String2.toLowerCase(`ΠΣ`) == `πς`\n```\n"},
462+
"sortText": "toLowerCase",
463+
"insertText": "->Js.String2.toLowerCase",
464+
"additionalTextEdits": [{
465+
"range": {"start": {"line": 97, "character": 54}, "end": {"line": 97, "character": 55}},
466+
"newText": ""
467+
}]
468+
}]
469+

0 commit comments

Comments
 (0)