Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete switch constructor #415

Merged
merged 12 commits into from
May 12, 2022
35 changes: 31 additions & 4 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type labelled = {
}

type label = labelled option

type arg = {label : label; exp : Parsetree.expression}

let findNamedArgCompletable ~(args : arg list) ~endPos ~posBeforeCursor
Expand Down Expand Up @@ -561,10 +562,10 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
| Pexp_apply ({pexp_desc = Pexp_ident {txt = Lident "|."}}, [_; _]) ->
()
| Pexp_apply (funExpr, args)
when not
(* Normally named arg completion fires when the cursor is right after the expression.
E.g in foo(~<---there
But it should not fire in foo(~a)<---there *)
when (* Normally named arg completion fires when the cursor is right after the expression.
E.g in foo(~<---there
But it should not fire in foo(~a)<---there *)
not
(Loc.end_ expr.pexp_loc = posCursor
&& charBeforeCursor = Some ')') ->
let args = extractExpApplyArgs ~args in
Expand Down Expand Up @@ -644,6 +645,15 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
iterator.expr iterator modBody;
scope := oldScope;
processed := true
| Pexp_match (_, cases) -> (
Printf.printf "XXX Pexp_match with %d cases not handled\n"
(List.length cases);
match cases with
| {pc_lhs; pc_rhs} :: _ ->
Printf.printf "XXX first case pattern:%s expression:%s\n"
(Loc.toString pc_lhs.ppat_loc)
(Loc.toString pc_rhs.pexp_loc)
| _ -> ())
| _ -> ());
if not !processed then Ast_iterator.default_iterator.expr iterator expr
in
Expand All @@ -666,6 +676,22 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
| _ -> ());
Ast_iterator.default_iterator.typ iterator core_type
in
let pat (iterator : Ast_iterator.iterator) (pat : Parsetree.pattern) =
if pat.ppat_loc |> Loc.hasPos ~pos:posNoWhite then (
found := true;
if debug then
Printf.printf "posCursor:[%s] posNoWhite:[%s] Found pattern:%s\n"
(Pos.toString posCursor) (Pos.toString posNoWhite)
(Loc.toString pat.ppat_loc);
(match pat.ppat_desc with
| Ppat_construct (lid, _) ->
if debug then
Printf.printf "XXX Ppat_construct %s:%s\n"
(flattenLidCheckDot lid |> String.concat ".")
(Loc.toString lid.loc)
| _ -> ());
Ast_iterator.default_iterator.pat iterator pat)
in
let module_expr (iterator : Ast_iterator.iterator)
(me : Parsetree.module_expr) =
(match me.pmod_desc with
Expand Down Expand Up @@ -725,6 +751,7 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
location;
module_expr;
module_type;
pat;
signature;
signature_item;
structure;
Expand Down
20 changes: 20 additions & 0 deletions analysis/tests/src/Completion.res
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,23 @@ let _ = <div name="" />

// (let _ = ff(~opt1=3))
// ^com

type v = This | That

let _ = x =>
switch x {
// | T
// ^com
| _ => 4
}

// let _ = x => switch x { | T
// ^com

let _ = x =>
switch x {
// | T
// ^com
| _ if false => 4
| _ => 4
}
35 changes: 35 additions & 0 deletions analysis/tests/src/expected/Completion.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,8 @@ Completable: Cnone

Complete tests/src/Completion.res 243:8
posCursor:[243:8] posNoWhite:[243:7] Found expr:[241:8->246:1]
XXX Pexp_match with 1 cases not handled
XXX first case pattern:[242:2->242:10] expression:[242:14->245:8]
posCursor:[243:8] posNoWhite:[243:7] Found expr:[242:14->245:8]
posCursor:[243:8] posNoWhite:[243:7] Found expr:[242:14->245:1]
Pexp_apply ...[243:3->243:4] (...[242:14->242:15], ...[243:5->245:1])
Expand Down Expand Up @@ -1030,6 +1032,7 @@ Completable: Cpath Value[SomeLocal]
}]

Complete tests/src/Completion.res 271:20
posCursor:[271:20] posNoWhite:[271:19] Found pattern:[271:7->274:3]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, and other similar lines indicate that this affects a bit existing test cases, but does not lead to a different outcome.
It just means that the relevant type, found below, happens to be inside a pattern containing the cursor.

posCursor:[271:20] posNoWhite:[271:19] Found type:[271:11->274:3]
Ptyp_constr SomeLocal:[271:11->274:3]
Completable: Cpath Type[SomeLocal]
Expand Down Expand Up @@ -1313,6 +1316,7 @@ posCursor:[336:26] posNoWhite:[336:25] Found expr:[334:13->346:23]
posCursor:[336:26] posNoWhite:[336:25] Found expr:[334:13->338:6]
posCursor:[336:26] posNoWhite:[336:25] Found expr:[335:6->338:5]
posCursor:[336:26] posNoWhite:[336:25] Found expr:[336:16->338:5]
posCursor:[336:26] posNoWhite:[336:25] Found pattern:[336:20->338:5]
posCursor:[336:26] posNoWhite:[336:25] Found type:[336:23->338:5]
Ptyp_constr Res:[336:23->338:5]
Completable: Cpath Type[Res]
Expand All @@ -1332,6 +1336,8 @@ Completable: Cpath Type[Res]

Complete tests/src/Completion.res 343:57
posCursor:[343:57] posNoWhite:[343:56] Found expr:[343:10->346:23]
XXX Pexp_match with 1 cases not handled
XXX first case pattern:[343:29->343:49] expression:[343:53->346:23]
posCursor:[343:57] posNoWhite:[343:56] Found expr:[343:53->346:23]
posCursor:[343:57] posNoWhite:[343:56] Found expr:[343:53->343:57]
Pexp_ident this:[343:53->343:57]
Expand Down Expand Up @@ -1381,3 +1387,32 @@ posCursor:[355:23] posNoWhite:[355:22] Found expr:[0:-1->355:23]
posCursor:[355:23] posNoWhite:[355:22] Found expr:[355:12->355:23]
[]

Complete tests/src/Completion.res 362:8
posCursor:[362:8] posNoWhite:[362:7] Found expr:[360:8->365:3]
posCursor:[362:8] posNoWhite:[362:7] Found expr:[361:2->365:3]
XXX Pexp_match with 1 cases not handled
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows that parser recovery leads to a single case being considered in the switch.

XXX first case pattern:[362:7->364:5] expression:[364:9->364:10]
posCursor:[362:8] posNoWhite:[362:7] Found pattern:[362:7->364:5]
posCursor:[362:8] posNoWhite:[362:7] Found pattern:[362:7->362:8]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the location, this could be the pattern T.

XXX Ppat_construct T:[362:7->362:8]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed here's the T pattern.

[]

Complete tests/src/Completion.res 367:30
posCursor:[367:30] posNoWhite:[367:29] Found expr:[367:11->376:3]
posCursor:[367:30] posNoWhite:[367:29] Found expr:[367:16->376:3]
XXX Pexp_match with 1 cases not handled
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows that parser recovery also gives 1 case when no more cases others follow.

XXX first case pattern:[367:29->367:30] expression:[370:0->376:3]
posCursor:[367:30] posNoWhite:[367:29] Found pattern:[367:29->367:30]
XXX Ppat_construct T:[367:29->367:30]
[]

Complete tests/src/Completion.res 372:8
posCursor:[372:8] posNoWhite:[372:7] Found expr:[370:8->376:3]
posCursor:[372:8] posNoWhite:[372:7] Found expr:[371:2->376:3]
XXX Pexp_match with 2 cases not handled
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example confirms that the first 2 cases are squashed into one.

XXX first case pattern:[372:7->374:5] expression:[374:18->374:19]
posCursor:[372:8] posNoWhite:[372:7] Found pattern:[372:7->374:5]
posCursor:[372:8] posNoWhite:[372:7] Found pattern:[372:7->372:8]
XXX Ppat_construct T:[372:7->372:8]
[]

2 changes: 2 additions & 0 deletions analysis/tests/src/expected/Jsx.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ Completable: Cjsx([WithChildren], n, [n])
}]

Complete tests/src/Jsx.res 94:18
posCursor:[94:18] posNoWhite:[94:17] Found pattern:[94:7->94:18]
posCursor:[94:18] posNoWhite:[94:17] Found type:[94:11->94:18]
Ptyp_constr React.e:[94:11->94:18]
Completable: Cpath Type[React, e]
Expand All @@ -288,6 +289,7 @@ Completable: Cpath Type[React, e]
}]

Complete tests/src/Jsx.res 96:20
posCursor:[96:20] posNoWhite:[96:19] Found pattern:[96:7->99:6]
posCursor:[96:20] posNoWhite:[96:19] Found type:[96:11->99:6]
Ptyp_constr ReactDOMR:[96:11->99:6]
Completable: Cpath Type[ReactDOMR]
Expand Down