Skip to content

Commit 01e3a90

Browse files
committed
add way to use fallbacks when completing patterns, restoring old behavior that was broken by pattern completion
1 parent 60d80ca commit 01e3a90

File tree

5 files changed

+101
-25
lines changed

5 files changed

+101
-25
lines changed

analysis/src/CompletionBackEnd.ml

+24-11
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ let rec resolveNestedPattern typ ~env ~package ~nested =
18111811
typ |> resolveNestedPattern ~env ~package ~nested
18121812
| _ -> None)
18131813

1814-
let processCompletable ~debug ~full ~scope ~env ~pos ~forHover
1814+
let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
18151815
(completable : Completable.t) =
18161816
let package = full.package in
18171817
let rawOpens = Scope.getRawOpens scope in
@@ -2173,20 +2173,27 @@ Note: The `@react.component` decorator requires the react-jsx config to be set i
21732173
Utils.startsWith name prefix
21742174
&& (forHover || not (List.mem name identsSeen)))
21752175
|> List.map mkLabel
2176-
| Cpattern {typ; prefix; nested = []} -> (
2176+
| Cpattern {typ; prefix; nested = []; fallback} -> (
21772177
let envWhereCompletionStarted = env in
21782178
match
21792179
typ
21802180
|> getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
21812181
~exact:true ~scope
21822182
|> completionsGetTypeEnv
21832183
with
2184-
| Some (typ, env) ->
2185-
typ
2186-
|> completeTypedValue ~env ~envWhereCompletionStarted ~full ~prefix
2187-
~expandOption:false ~includeLocalValues:false ~completionContext:None
2184+
| Some (typ, env) -> (
2185+
let items =
2186+
typ
2187+
|> completeTypedValue ~env ~envWhereCompletionStarted ~full ~prefix
2188+
~expandOption:false ~includeLocalValues:false
2189+
~completionContext:None
2190+
in
2191+
match (items, fallback) with
2192+
| [], Some fallback ->
2193+
fallback |> processCompletable ~debug ~full ~scope ~env ~pos ~forHover
2194+
| items, _ -> items)
21882195
| None -> [])
2189-
| Cpattern {typ; prefix; nested} -> (
2196+
| Cpattern {typ; prefix; nested; fallback} -> (
21902197
let envWhereCompletionStarted = env in
21912198
match
21922199
typ
@@ -2197,8 +2204,14 @@ Note: The `@react.component` decorator requires the react-jsx config to be set i
21972204
| Some (typ, env) -> (
21982205
match typ |> resolveNestedPattern ~env ~package:full.package ~nested with
21992206
| None -> []
2200-
| Some (typ, env, completionContext) ->
2201-
typ
2202-
|> completeTypedValue ~env ~envWhereCompletionStarted ~full ~prefix
2203-
~expandOption:false ~includeLocalValues:false ~completionContext)
2207+
| Some (typ, env, completionContext) -> (
2208+
let items =
2209+
typ
2210+
|> completeTypedValue ~env ~envWhereCompletionStarted ~full ~prefix
2211+
~expandOption:false ~includeLocalValues:false ~completionContext
2212+
in
2213+
match (items, fallback) with
2214+
| [], Some fallback ->
2215+
fallback |> processCompletable ~debug ~full ~scope ~env ~pos ~forHover
2216+
| items, _ -> items))
22042217
| None -> [])

analysis/src/CompletionFrontEnd.ml

+17-11
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,12 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
617617
| Some (prefix, nestedPattern), Some ctxPath ->
618618
setResult
619619
(Completable.Cpattern
620-
{typ = ctxPath; prefix; nested = List.rev nestedPattern})
620+
{
621+
typ = ctxPath;
622+
prefix;
623+
nested = List.rev nestedPattern;
624+
fallback = None;
625+
})
621626
| _ -> ()
622627
in
623628
let scopeValueBinding (vb : Parsetree.value_binding) =
@@ -684,7 +689,8 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
684689
| None -> ()
685690
| Some ctxPath ->
686691
setResult
687-
(Completable.Cpattern {typ = ctxPath; nested = []; prefix = ""}))
692+
(Completable.Cpattern
693+
{typ = ctxPath; nested = []; prefix = ""; fallback = None}))
688694
| Pexp_match (exp, cases) -> (
689695
(* If there's more than one case, or the case isn't a pattern hole, figure out if we're completing another
690696
broken parser case (`switch x { | true => () | <com> }` for example). *)
@@ -710,7 +716,8 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
710716
| true, false ->
711717
(* If there's no case with the cursor, but a broken parser case, complete for the top level. *)
712718
setResult
713-
(Completable.Cpattern {typ = ctxPath; nested = []; prefix = ""})
719+
(Completable.Cpattern
720+
{typ = ctxPath; nested = []; prefix = ""; fallback = None})
714721
| false, false -> ()))
715722
| _ -> unsetLookingForPat ()
716723
in
@@ -1114,19 +1121,18 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
11141121
Printf.printf "posCursor:[%s] posNoWhite:[%s] Found pattern:%s\n"
11151122
(Pos.toString posCursor) (Pos.toString posNoWhite)
11161123
(Loc.toString pat.ppat_loc);
1117-
(* TODO:
1118-
This change breaks old behavior of completing constructors in scope.
1119-
Either be fine with it, fix it somehow, or incorporate completing
1120-
constructors in scope when regular completion for variants can't
1121-
be done. *)
1122-
(match (!lookingForPat, pat.ppat_desc) with
1123-
| None, Ppat_construct (lid, _) ->
1124+
(match pat.ppat_desc with
1125+
| Ppat_construct (lid, _) -> (
11241126
let lidPath = flattenLidCheckDot lid in
11251127
if debug then
11261128
Printf.printf "Ppat_construct %s:%s\n"
11271129
(lidPath |> String.concat ".")
11281130
(Loc.toString lid.loc);
1129-
setResult (Cpath (CPId (lidPath, Value)))
1131+
let completion = Completable.Cpath (CPId (lidPath, Value)) in
1132+
match !result with
1133+
| Some (Completable.Cpattern p, scope) ->
1134+
result := Some (Cpattern {p with fallback = Some completion}, scope)
1135+
| _ -> setResult completion)
11301136
| _ -> ());
11311137
Ast_iterator.default_iterator.pat iterator pat)
11321138
in

analysis/src/SharedTypes.ml

+6-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,12 @@ module Completable = struct
592592
propName: string;
593593
prefix: string;
594594
}
595-
| Cpattern of {typ: contextPath; nested: patternPath list; prefix: string}
595+
| Cpattern of {
596+
typ: contextPath;
597+
nested: patternPath list;
598+
prefix: string;
599+
fallback: t option;
600+
}
596601

597602
(** An extracted type from a type expr *)
598603
type extractedType =

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

+39-2
Original file line numberDiff line numberDiff line change
@@ -1446,18 +1446,55 @@ looking for: Cpath Value[x]
14461446
posCursor:[362:8] posNoWhite:[362:7] Found expr:[361:2->365:3]
14471447
posCursor:[362:8] posNoWhite:[362:7] Found pattern:[362:7->364:5]
14481448
posCursor:[362:8] posNoWhite:[362:7] Found pattern:[362:7->362:8]
1449+
Ppat_construct T:[362:7->362:8]
14491450
Completable: Cpattern Value[x]=T
14501451
Raw opens: 2 Shadow.B.place holder ... Shadow.A.place holder
14511452
Resolved opens 2 Completion.res Completion.res
1452-
[]
1453+
Raw opens: 2 Shadow.B.place holder ... Shadow.A.place holder
1454+
Resolved opens 2 Completion.res Completion.res
1455+
[{
1456+
"label": "That",
1457+
"kind": 4,
1458+
"tags": [],
1459+
"detail": "That\n\ntype v = This | That",
1460+
"documentation": null
1461+
}, {
1462+
"label": "This",
1463+
"kind": 4,
1464+
"tags": [],
1465+
"detail": "This\n\ntype v = This | That",
1466+
"documentation": null
1467+
}, {
1468+
"label": "TableclothMap",
1469+
"kind": 9,
1470+
"tags": [],
1471+
"detail": "file module",
1472+
"documentation": null
1473+
}, {
1474+
"label": "TypeDefinition",
1475+
"kind": 9,
1476+
"tags": [],
1477+
"detail": "file module",
1478+
"documentation": null
1479+
}]
14531480

14541481
Complete src/Completion.res 373:21
14551482
posCursor:[373:21] posNoWhite:[373:20] Found expr:[371:8->376:3]
14561483
looking for: Cpath Value[x]
14571484
posCursor:[373:21] posNoWhite:[373:20] Found expr:[372:2->376:3]
14581485
posCursor:[373:21] posNoWhite:[373:20] Found pattern:[373:7->375:5]
14591486
posCursor:[373:21] posNoWhite:[373:20] Found pattern:[373:7->373:21]
1460-
[]
1487+
Ppat_construct AndThatOther.T:[373:7->373:21]
1488+
Completable: Cpath Value[AndThatOther, T]
1489+
Raw opens: 2 Shadow.B.place holder ... Shadow.A.place holder
1490+
Resolved opens 2 Completion.res Completion.res
1491+
[{
1492+
"label": "ThatOther",
1493+
"kind": 4,
1494+
"tags": [],
1495+
"detail": "ThatOther\n\ntype v = And | ThatOther",
1496+
"documentation": null
1497+
}]
14611498

14621499
Complete src/Completion.res 378:24
14631500
posCursor:[378:24] posNoWhite:[378:23] Found expr:[378:12->378:26]

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

+15
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ Complete src/CompletionPattern.res 87:20
261261
looking for: Cpath Value[z]
262262
posCursor:[87:20] posNoWhite:[87:19] Found expr:[87:3->87:22]
263263
posCursor:[87:20] posNoWhite:[87:19] Found pattern:[87:16->87:21]
264+
Ppat_construct Two:[87:16->87:19]
264265
posCursor:[87:20] posNoWhite:[87:19] Found pattern:[87:19->87:21]
266+
Ppat_construct ():[87:19->87:21]
265267
Completable: Cpattern Value[z]->variantPayload::Two($0)
266268
[{
267269
"label": "true",
@@ -281,6 +283,7 @@ Complete src/CompletionPattern.res 90:21
281283
looking for: Cpath Value[z]
282284
posCursor:[90:21] posNoWhite:[90:20] Found expr:[90:3->90:23]
283285
posCursor:[90:21] posNoWhite:[90:20] Found pattern:[90:16->90:22]
286+
Ppat_construct Two:[90:16->90:19]
284287
posCursor:[90:21] posNoWhite:[90:20] Found pattern:[90:20->90:21]
285288
Completable: Cpattern Value[z]=t->variantPayload::Two($0)
286289
[{
@@ -295,6 +298,7 @@ Complete src/CompletionPattern.res 93:23
295298
looking for: Cpath Value[z]
296299
posCursor:[93:23] posNoWhite:[93:22] Found expr:[93:3->93:26]
297300
posCursor:[93:23] posNoWhite:[93:22] Found pattern:[93:16->93:25]
301+
Ppat_construct Three:[93:16->93:21]
298302
posCursor:[93:23] posNoWhite:[93:22] Found pattern:[93:22->93:24]
299303
Completable: Cpattern Value[z]->variantPayload::Three($0), recordBody
300304
[{
@@ -327,6 +331,7 @@ Complete src/CompletionPattern.res 96:27
327331
looking for: Cpath Value[z]
328332
posCursor:[96:27] posNoWhite:[96:26] Found expr:[96:3->96:29]
329333
posCursor:[96:27] posNoWhite:[96:26] Found pattern:[96:16->96:28]
334+
Ppat_construct Three:[96:16->96:21]
330335
posCursor:[96:27] posNoWhite:[96:26] Found pattern:[96:21->96:29]
331336
posCursor:[96:27] posNoWhite:[96:26] Found pattern:[96:26->96:27]
332337
Completable: Cpattern Value[z]=t->variantPayload::Three($1)
@@ -343,6 +348,7 @@ looking for: Cpath Value[b]
343348
posCursor:[103:21] posNoWhite:[103:20] Found expr:[103:3->103:23]
344349
posCursor:[103:21] posNoWhite:[103:20] Found pattern:[103:16->103:22]
345350
posCursor:[103:21] posNoWhite:[103:20] Found pattern:[103:20->103:21]
351+
Ppat_construct ():[103:20->103:21]
346352
Completable: Cpattern Value[b]->polyvariantPayload::two($0)
347353
[{
348354
"label": "true",
@@ -456,7 +462,9 @@ Complete src/CompletionPattern.res 127:21
456462
looking for: Cpath Value[o]
457463
posCursor:[127:21] posNoWhite:[127:20] Found expr:[127:3->127:24]
458464
posCursor:[127:21] posNoWhite:[127:20] Found pattern:[127:16->127:22]
465+
Ppat_construct Some:[127:16->127:20]
459466
posCursor:[127:21] posNoWhite:[127:20] Found pattern:[127:20->127:22]
467+
Ppat_construct ():[127:20->127:22]
460468
Completable: Cpattern Value[o]->variantPayload::Some($0)
461469
[{
462470
"label": "true",
@@ -476,6 +484,7 @@ Complete src/CompletionPattern.res 134:23
476484
looking for: Cpath Value[p]
477485
posCursor:[134:23] posNoWhite:[134:22] Found expr:[134:3->134:26]
478486
posCursor:[134:23] posNoWhite:[134:22] Found pattern:[134:16->134:25]
487+
Ppat_construct Test:[134:16->134:20]
479488
Completable: Cpattern Value[p]->variantPayload::Test($1)
480489
[{
481490
"label": "true",
@@ -495,6 +504,7 @@ Complete src/CompletionPattern.res 137:29
495504
looking for: Cpath Value[p]
496505
posCursor:[137:29] posNoWhite:[137:28] Found expr:[137:3->137:32]
497506
posCursor:[137:29] posNoWhite:[137:28] Found pattern:[137:16->137:31]
507+
Ppat_construct Test:[137:16->137:20]
498508
posCursor:[137:29] posNoWhite:[137:28] Found pattern:[137:20->137:32]
499509
Completable: Cpattern Value[p]->variantPayload::Test($2)
500510
[{
@@ -517,6 +527,7 @@ Complete src/CompletionPattern.res 140:23
517527
looking for: Cpath Value[p]
518528
posCursor:[140:23] posNoWhite:[140:22] Found expr:[140:3->140:32]
519529
posCursor:[140:23] posNoWhite:[140:22] Found pattern:[140:16->140:31]
530+
Ppat_construct Test:[140:16->140:20]
520531
posCursor:[140:23] posNoWhite:[140:22] Found pattern:[140:20->140:32]
521532
Completable: Cpattern Value[p]->variantPayload::Test($1)
522533
[{
@@ -537,6 +548,7 @@ Complete src/CompletionPattern.res 143:35
537548
looking for: Cpath Value[p]
538549
posCursor:[143:35] posNoWhite:[143:34] Found expr:[143:3->143:38]
539550
posCursor:[143:35] posNoWhite:[143:34] Found pattern:[143:16->143:37]
551+
Ppat_construct Test:[143:16->143:20]
540552
posCursor:[143:35] posNoWhite:[143:34] Found pattern:[143:20->143:38]
541553
Completable: Cpattern Value[p]->variantPayload::Test($3)
542554
[{
@@ -632,6 +644,7 @@ Complete src/CompletionPattern.res 164:17
632644
looking for: Cpath Value[s]
633645
posCursor:[164:17] posNoWhite:[164:16] Found expr:[164:3->164:20]
634646
posCursor:[164:17] posNoWhite:[164:16] Found pattern:[164:16->164:18]
647+
Ppat_construct ():[164:16->164:18]
635648
Completable: Cpattern Value[s]->tuple($0)
636649
[{
637650
"label": "true",
@@ -757,6 +770,7 @@ looking for: Cpath Value[z]
757770
posCursor:[182:32] posNoWhite:[182:31] Found expr:[182:3->182:37]
758771
posCursor:[182:32] posNoWhite:[182:31] Found pattern:[182:16->182:34]
759772
posCursor:[182:32] posNoWhite:[182:31] Found pattern:[182:22->182:34]
773+
Ppat_construct Two:[182:22->182:25]
760774
Completable: Cpattern Value[z]->variantPayload::Two($0)
761775
[{
762776
"label": "true",
@@ -777,6 +791,7 @@ looking for: Cpath Value[z]
777791
posCursor:[185:48] posNoWhite:[185:47] Found expr:[185:3->185:53]
778792
posCursor:[185:48] posNoWhite:[185:47] Found pattern:[185:16->185:50]
779793
posCursor:[185:48] posNoWhite:[185:47] Found pattern:[185:22->185:50]
794+
Ppat_construct Three:[185:22->185:27]
780795
posCursor:[185:48] posNoWhite:[185:47] Found pattern:[185:27->185:53]
781796
Completable: Cpattern Value[z]->variantPayload::Three($1)
782797
[{

0 commit comments

Comments
 (0)