diff --git a/analysis/src/CompletionFrontEnd.ml b/analysis/src/CompletionFrontEnd.ml index 079b52028..35c099463 100644 --- a/analysis/src/CompletionFrontEnd.ml +++ b/analysis/src/CompletionFrontEnd.ml @@ -130,6 +130,7 @@ type labelled = { } type label = labelled option + type arg = {label : label; exp : Parsetree.expression} let findNamedArgCompletable ~(args : arg list) ~endPos ~posBeforeCursor @@ -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 @@ -666,6 +667,24 @@ 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, _) -> + let lidPath = flattenLidCheckDot lid in + if debug then + Printf.printf "Ppat_construct %s:%s\n" + (lidPath |> String.concat ".") + (Loc.toString lid.loc); + setResult (Cpath (CPId (lidPath, Value))) + | _ -> ()); + Ast_iterator.default_iterator.pat iterator pat) + in let module_expr (iterator : Ast_iterator.iterator) (me : Parsetree.module_expr) = (match me.pmod_desc with @@ -725,6 +744,7 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text = location; module_expr; module_type; + pat; signature; signature_item; structure; diff --git a/analysis/tests/src/Completion.res b/analysis/tests/src/Completion.res index 389a91a2c..f364e964a 100644 --- a/analysis/tests/src/Completion.res +++ b/analysis/tests/src/Completion.res @@ -355,3 +355,23 @@ let _ =
// (let _ = ff(~opt1=3)) // ^com + +type v = This | That + +let _ = x => + switch x { + // | T + // ^com + | _ => 4 + } + +module AndThatOther = { + type v = And | ThatOther +} + +let _ = x => + switch x { + // | AndThatOther.T + // ^com + | _ => 4 + } diff --git a/analysis/tests/src/expected/Completion.res.txt b/analysis/tests/src/expected/Completion.res.txt index c51667981..5776927a6 100644 --- a/analysis/tests/src/expected/Completion.res.txt +++ b/analysis/tests/src/expected/Completion.res.txt @@ -1030,6 +1030,7 @@ Completable: Cpath Value[SomeLocal] }] Complete tests/src/Completion.res 271:20 +posCursor:[271:20] posNoWhite:[271:19] Found pattern:[271:7->274:3] posCursor:[271:20] posNoWhite:[271:19] Found type:[271:11->274:3] Ptyp_constr SomeLocal:[271:11->274:3] Completable: Cpath Type[SomeLocal] @@ -1313,6 +1314,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] @@ -1381,3 +1383,51 @@ 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] +posCursor:[362:8] posNoWhite:[362:7] Found pattern:[362:7->364:5] +posCursor:[362:8] posNoWhite:[362:7] Found pattern:[362:7->362:8] +Ppat_construct T:[362:7->362:8] +Completable: Cpath Value[T] +[{ + "label": "That", + "kind": 4, + "tags": [], + "detail": "That\n\ntype v = This | That", + "documentation": null + }, { + "label": "This", + "kind": 4, + "tags": [], + "detail": "This\n\ntype v = This | That", + "documentation": null + }, { + "label": "TableclothMap", + "kind": 9, + "tags": [], + "detail": "file module", + "documentation": null + }, { + "label": "TypeDefinition", + "kind": 9, + "tags": [], + "detail": "file module", + "documentation": null + }] + +Complete tests/src/Completion.res 373:21 +posCursor:[373:21] posNoWhite:[373:20] Found expr:[371:8->376:3] +posCursor:[373:21] posNoWhite:[373:20] Found expr:[372:2->376:3] +posCursor:[373:21] posNoWhite:[373:20] Found pattern:[373:7->375:5] +posCursor:[373:21] posNoWhite:[373:20] Found pattern:[373:7->373:21] +Ppat_construct AndThatOther.T:[373:7->373:21] +Completable: Cpath Value[AndThatOther, T] +[{ + "label": "ThatOther", + "kind": 4, + "tags": [], + "detail": "ThatOther\n\ntype v = And | ThatOther", + "documentation": null + }] + diff --git a/analysis/tests/src/expected/Jsx.res.txt b/analysis/tests/src/expected/Jsx.res.txt index f91078652..d080052c6 100644 --- a/analysis/tests/src/expected/Jsx.res.txt +++ b/analysis/tests/src/expected/Jsx.res.txt @@ -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] @@ -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]