Skip to content

Commit 14e892c

Browse files
committed
Fix issue where variables bound in switches were not be completed.
The scope mechanism was missing a number of cases for complex patterns, as well as cases in switches.
1 parent 312c62e commit 14e892c

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

analysis/src/Completion.ml

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,36 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
319319
scope :=
320320
!scope |> Scope.addValue ~name:vd.pval_name.txt ~loc:vd.pval_name.loc
321321
in
322+
let rec scopePattern (pat : Parsetree.pattern) =
323+
match pat.ppat_desc with
324+
| Ppat_any -> ()
325+
| Ppat_var {txt; loc} -> scope := !scope |> Scope.addValue ~name:txt ~loc
326+
| Ppat_alias (p, asA) ->
327+
scopePattern p;
328+
scope := !scope |> Scope.addValue ~name:asA.txt ~loc:asA.loc
329+
| Ppat_constant _ | Ppat_interval _ -> ()
330+
| Ppat_tuple pl -> pl |> List.iter scopePattern
331+
| Ppat_construct (_, None) -> ()
332+
| Ppat_construct (_, Some p) -> scopePattern p
333+
| Ppat_variant (_, None) -> ()
334+
| Ppat_variant (_, Some p) -> scopePattern p
335+
| Ppat_record (fields, _) ->
336+
fields |> List.iter (fun (_, p) -> scopePattern p)
337+
| Ppat_array pl -> pl |> List.iter scopePattern
338+
| Ppat_or (p1, _) -> scopePattern p1
339+
| Ppat_constraint (p, _) -> scopePattern p
340+
| Ppat_type _ -> ()
341+
| Ppat_lazy p -> scopePattern p
342+
| Ppat_unpack {txt; loc} -> scope := !scope |> Scope.addValue ~name:txt ~loc
343+
| Ppat_exception p -> scopePattern p
344+
| Ppat_extension _ -> ()
345+
| Ppat_open (_, p) -> scopePattern p
346+
in
347+
322348
let scopeValueBinding (vb : Parsetree.value_binding) =
323-
match vb.pvb_pat.ppat_desc with
324-
| Ppat_var {txt; loc}
325-
| Ppat_constraint ({ppat_desc = Ppat_var {txt; loc}}, _) ->
326-
scope := !scope |> Scope.addValue ~name:txt ~loc
327-
| _ -> ()
349+
scopePattern vb.pvb_pat
328350
in
351+
329352
let scopeTypeKind (tk : Parsetree.type_kind) =
330353
match tk with
331354
| Ptype_variant constrDecls ->
@@ -355,6 +378,12 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
355378
!scope |> Scope.addModule ~name:md.pmd_name.txt ~loc:md.pmd_name.loc
356379
in
357380

381+
let case (iterator : Ast_iterator.iterator) (case : Parsetree.case) =
382+
let oldScope = !scope in
383+
scopePattern case.pc_lhs;
384+
Ast_iterator.default_iterator.case iterator case;
385+
scope := oldScope
386+
in
358387
let structure (iterator : Ast_iterator.iterator)
359388
(structure : Parsetree.structure) =
360389
let oldScope = !scope in
@@ -712,6 +741,7 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
712741
{
713742
Ast_iterator.default_iterator with
714743
attribute;
744+
case;
715745
expr;
716746
module_expr;
717747
module_type;

analysis/tests/src/Completion.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,11 @@ module FAO = {
232232

233233
// FAO.forAutoObject["forAutoLabel"].forAuto->ForAuto.a
234234
// ^com
235+
236+
let someR = Some(r)
237+
let _ = switch someR {
238+
| Some(_z) => 1
239+
// + _z.
240+
// ^com
241+
| _ => 3
242+
}

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,11 @@ DocumentSymbol tests/src/Completion.res
827827
"name": "forAutoObject",
828828
"kind": 13,
829829
"location": {"uri": "Completion.res", "range": {"start": {"line": 220, "character": 2}, "end": {"line": 220, "character": 68}}}
830+
},
831+
{
832+
"name": "someR",
833+
"kind": 13,
834+
"location": {"uri": "Completion.res", "range": {"start": {"line": 235, "character": 0}, "end": {"line": 235, "character": 19}}}
830835
}
831836
]
832837

@@ -1228,8 +1233,8 @@ Completable: Cpath Value[FAO, forAutoObject][""]
12281233
}]
12291234

12301235
Complete tests/src/Completion.res 226:37
1231-
posCursor:[226:37] posNoWhite:[226:36] Found expr:[226:3->235:0]
1232-
Pexp_field [226:3->226:36] _:[235:0->235:0]
1236+
posCursor:[226:37] posNoWhite:[226:36] Found expr:[226:3->235:3]
1237+
Pexp_field [226:3->226:36] _:[235:0->235:3]
12331238
Completable: Cpath Value[FAO, forAutoObject]["forAutoLabel"].""
12341239
[{
12351240
"label": "forAuto",
@@ -1281,3 +1286,25 @@ Completable: Cpath Value[ForAuto, a]
12811286
"documentation": null
12821287
}]
12831288

1289+
Complete tests/src/Completion.res 238:8
1290+
posCursor:[238:8] posNoWhite:[238:7] Found expr:[236:8->241:1]
1291+
posCursor:[238:8] posNoWhite:[238:7] Found expr:[237:14->240:8]
1292+
posCursor:[238:8] posNoWhite:[238:7] Found expr:[237:14->240:1]
1293+
Pexp_apply ...[238:3->238:4] (...[237:14->237:15], ...[238:5->240:1])
1294+
posCursor:[238:8] posNoWhite:[238:7] Found expr:[238:5->240:1]
1295+
Pexp_field [238:5->238:7] _:[240:0->240:1]
1296+
Completable: Cpath Value[_z].""
1297+
[{
1298+
"label": "x",
1299+
"kind": 5,
1300+
"tags": [],
1301+
"detail": "x: int\n\ntype r = {x: int, y: string}",
1302+
"documentation": null
1303+
}, {
1304+
"label": "y",
1305+
"kind": 5,
1306+
"tags": [],
1307+
"detail": "y: string\n\ntype r = {x: int, y: string}",
1308+
"documentation": null
1309+
}]
1310+

0 commit comments

Comments
 (0)