Skip to content

Commit b7a76fc

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 c52647f commit b7a76fc

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

analysis/src/Completion.ml

+35-5
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
@@ -713,6 +742,7 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
713742
{
714743
Ast_iterator.default_iterator with
715744
attribute;
745+
case;
716746
expr;
717747
module_expr;
718748
module_type;

analysis/tests/src/Completion.res

+9-1
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,12 @@ let name = "abc"
238238
// ^com
239239

240240
let notHere = " "
241-
// ^com
241+
// ^com
242+
243+
let someR = Some(r)
244+
let _ = switch someR {
245+
| Some(_z) => 1
246+
// + _z.
247+
// ^com
248+
| _ => 3
249+
}

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

+27
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,11 @@ DocumentSymbol tests/src/Completion.res
837837
"name": "notHere",
838838
"kind": 15,
839839
"location": {"uri": "Completion.res", "range": {"start": {"line": 239, "character": 0}, "end": {"line": 239, "character": 22}}}
840+
},
841+
{
842+
"name": "someR",
843+
"kind": 13,
844+
"location": {"uri": "Completion.res", "range": {"start": {"line": 242, "character": 0}, "end": {"line": 242, "character": 19}}}
840845
}
841846
]
842847

@@ -1316,3 +1321,25 @@ posCursor:[239:17] posNoWhite:[239:14] Found expr:[239:14->239:22]
13161321
Completable: Cnone
13171322
[]
13181323

1324+
Complete tests/src/Completion.res 245:8
1325+
posCursor:[245:8] posNoWhite:[245:7] Found expr:[243:8->248:1]
1326+
posCursor:[245:8] posNoWhite:[245:7] Found expr:[244:14->247:8]
1327+
posCursor:[245:8] posNoWhite:[245:7] Found expr:[244:14->247:1]
1328+
Pexp_apply ...[245:3->245:4] (...[244:14->244:15], ...[245:5->247:1])
1329+
posCursor:[245:8] posNoWhite:[245:7] Found expr:[245:5->247:1]
1330+
Pexp_field [245:5->245:7] _:[247:0->247:1]
1331+
Completable: Cpath Value[_z].""
1332+
[{
1333+
"label": "x",
1334+
"kind": 5,
1335+
"tags": [],
1336+
"detail": "x: int\n\ntype r = {x: int, y: string}",
1337+
"documentation": null
1338+
}, {
1339+
"label": "y",
1340+
"kind": 5,
1341+
"tags": [],
1342+
"detail": "y: string\n\ntype r = {x: int, y: string}",
1343+
"documentation": null
1344+
}]
1345+

0 commit comments

Comments
 (0)