Skip to content

Commit c342130

Browse files
committed
common filter
1 parent aaed47a commit c342130

File tree

1 file changed

+44
-57
lines changed

1 file changed

+44
-57
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,13 @@ let rec extractType ~env ~package (t : Types.type_expr) =
15021502
| Ttuple expressions -> Some (Tuple (env, expressions))
15031503
| _ -> None
15041504

1505+
let filterItems items ~prefix =
1506+
if prefix = "" then items
1507+
else
1508+
items
1509+
|> List.filter (fun (item : Completion.t) ->
1510+
Utils.startsWith item.name prefix)
1511+
15051512
let completeTypedValue ~env ~envWhereCompletionStarted ~full ~prefix
15061513
~expandOption =
15071514
let namesUsed = Hashtbl.create 10 in
@@ -1511,65 +1518,45 @@ let completeTypedValue ~env ~envWhereCompletionStarted ~full ~prefix
15111518
| Some (Toption (env, typ)) when expandOption ->
15121519
typ |> completeTypedValueInner ~env ~full ~prefix ~expandOption:false
15131520
| Some (Tbool env) ->
1514-
let items =
1515-
[
1516-
Completion.create ~name:"true"
1517-
~kind:(Label (t |> Shared.typeToString))
1518-
~env;
1519-
Completion.create ~name:"false"
1520-
~kind:(Label (t |> Shared.typeToString))
1521-
~env;
1522-
]
1523-
in
1524-
if prefix = "" then items
1525-
else
1526-
items
1527-
|> List.filter (fun (item : Completion.t) ->
1528-
Utils.startsWith item.name prefix)
1521+
[
1522+
Completion.create ~name:"true"
1523+
~kind:(Label (t |> Shared.typeToString))
1524+
~env;
1525+
Completion.create ~name:"false"
1526+
~kind:(Label (t |> Shared.typeToString))
1527+
~env;
1528+
]
1529+
|> filterItems ~prefix
15291530
| Some (Tvariant {env; constructors; variantDecl; variantName}) ->
1530-
let items =
1531-
constructors
1532-
|> List.filter_map (fun (constructor : Constructor.t) ->
1533-
if
1534-
prefix <> ""
1535-
&& not (Utils.startsWith constructor.cname.txt prefix)
1536-
then None
1537-
else
1538-
Some
1539-
(Completion.create
1540-
~name:
1541-
(constructor.cname.txt
1542-
^
1543-
if constructor.args |> List.length > 0 then
1544-
"("
1545-
^ (constructor.args
1546-
|> List.map (fun _ -> "_")
1547-
|> String.concat ", ")
1548-
^ ")"
1549-
else "")
1550-
~kind:
1551-
(Constructor
1552-
( constructor,
1553-
variantDecl |> Shared.declToString variantName ))
1554-
~env))
1555-
in
1556-
items
1531+
constructors
1532+
|> List.map (fun (constructor : Constructor.t) ->
1533+
Completion.create
1534+
~name:
1535+
(constructor.cname.txt
1536+
^
1537+
if constructor.args |> List.length > 0 then
1538+
"("
1539+
^ (constructor.args
1540+
|> List.map (fun _ -> "_")
1541+
|> String.concat ", ")
1542+
^ ")"
1543+
else "")
1544+
~kind:
1545+
(Constructor
1546+
( constructor,
1547+
variantDecl |> Shared.declToString variantName ))
1548+
~env)
1549+
|> filterItems ~prefix
15571550
| Some (Toption (env, t)) ->
1558-
let items =
1559-
[
1560-
Completion.create ~name:"None"
1561-
~kind:(Label (t |> Shared.typeToString))
1562-
~env;
1563-
Completion.create ~name:"Some(_)"
1564-
~kind:(Label (t |> Shared.typeToString))
1565-
~env;
1566-
]
1567-
in
1568-
if prefix = "" then items
1569-
else
1570-
items
1571-
|> List.filter (fun (item : Completion.t) ->
1572-
Utils.startsWith item.name prefix)
1551+
[
1552+
Completion.create ~name:"None"
1553+
~kind:(Label (t |> Shared.typeToString))
1554+
~env;
1555+
Completion.create ~name:"Some(_)"
1556+
~kind:(Label (t |> Shared.typeToString))
1557+
~env;
1558+
]
1559+
|> filterItems ~prefix
15731560
| _ -> []
15741561
in
15751562
(* Include all values and modules in completion if there's a prefix, not otherwise *)

0 commit comments

Comments
 (0)