Skip to content

Commit 9db7b7d

Browse files
committed
WIP
1 parent ea2f571 commit 9db7b7d

File tree

2 files changed

+101
-39
lines changed

2 files changed

+101
-39
lines changed

compiler/syntax/src/res_comments_table.ml

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ let log t =
8888
let attach tbl loc comments =
8989
match comments with
9090
| [] -> ()
91-
| comments -> Hashtbl.replace tbl loc comments
91+
| comments ->
92+
Printf.eprintf "DEBUG: Attaching %d comments to ___location [%d:%d-%d:%d]\n"
93+
(List.length comments)
94+
loc.Location.loc_start.pos_lnum (loc.Location.loc_start.pos_cnum - loc.Location.loc_start.pos_bol)
95+
loc.Location.loc_end.pos_lnum (loc.Location.loc_end.pos_cnum - loc.Location.loc_end.pos_bol);
96+
List.iter (fun c -> Printf.eprintf " Comment: %S\n" (Comment.txt c)) comments;
97+
Hashtbl.replace tbl loc comments
9298

9399
(* Partitions a list of comments into three groups based on their position relative to a ___location:
94100
* - leading: comments that end before the ___location's start position
@@ -1208,6 +1214,12 @@ and walk_expression expr t comments =
12081214
| Pexp_variant (_label, None) -> ()
12091215
| Pexp_variant (_label, Some expr) -> walk_expression expr t comments
12101216
| Pexp_array exprs | Pexp_tuple exprs ->
1217+
Printf.eprintf "DEBUG: Processing array/tuple with %d expressions:\n" (List.length exprs);
1218+
List.iteri (fun i e ->
1219+
Printf.eprintf " [%d] Expression at [%d:%d-%d:%d]\n" i
1220+
e.Parsetree.pexp_loc.loc_start.pos_lnum (e.Parsetree.pexp_loc.loc_start.pos_cnum - e.Parsetree.pexp_loc.loc_start.pos_bol)
1221+
e.Parsetree.pexp_loc.loc_end.pos_lnum (e.Parsetree.pexp_loc.loc_end.pos_cnum - e.Parsetree.pexp_loc.loc_end.pos_bol)
1222+
) exprs;
12111223
walk_list (exprs |> List.map (fun e -> Expression e)) t comments
12121224
| Pexp_record (rows, spread_expr) ->
12131225
if rows = [] then attach t.inside expr.pexp_loc comments
@@ -1540,34 +1552,81 @@ and walk_expression expr t comments =
15401552
when Res_parsetree_viewer.is_tuple_array key_values ->
15411553
walk_list [Expression key_values] t comments
15421554
| Pexp_apply {funct = call_expr; args = arguments} ->
1543-
let before, inside, after = partition_by_loc comments call_expr.pexp_loc in
1544-
let after =
1545-
if is_block_expr call_expr then (
1546-
let after_expr, rest =
1547-
partition_adjacent_trailing call_expr.pexp_loc after
1548-
in
1549-
walk_expression call_expr t (List.concat [before; inside; after_expr]);
1550-
rest)
1551-
else (
1552-
attach t.leading call_expr.pexp_loc before;
1553-
walk_expression call_expr t inside;
1554-
after)
1555-
in
1556-
let after_expr, rest =
1557-
partition_adjacent_trailing call_expr.pexp_loc after
1558-
in
1559-
attach t.trailing call_expr.pexp_loc after_expr;
1560-
walk_list
1561-
(arguments
1562-
|> List.map (fun (lbl, expr) ->
1563-
let loc =
1564-
match lbl with
1565-
| Asttypes.Labelled {loc} | Optional {loc} ->
1566-
{loc with loc_end = expr.Parsetree.pexp_loc.loc_end}
1567-
| _ -> expr.pexp_loc
1568-
in
1569-
ExprArgument {expr; loc}))
1570-
t rest
1555+
(* Special handling for Belt.Array.concatMany - treat like an array *)
1556+
(match call_expr.pexp_desc with
1557+
| Pexp_ident {txt = Longident.Ldot (Longident.Ldot (Longident.Lident "Belt", "Array"), "concatMany")}
1558+
when List.length arguments = 1 -> (
1559+
match arguments with
1560+
| [(_, {pexp_desc = Pexp_array sub_arrays})] ->
1561+
Printf.eprintf "DEBUG: Special handling for Belt.Array.concatMany with %d sub-arrays\n" (List.length sub_arrays);
1562+
(* Collect all individual expressions from sub-arrays *)
1563+
let all_exprs = List.fold_left (fun acc sub_array ->
1564+
match sub_array.Parsetree.pexp_desc with
1565+
| Pexp_array exprs -> acc @ exprs
1566+
| _ -> acc @ [sub_array]
1567+
) [] sub_arrays in
1568+
Printf.eprintf "DEBUG: Collected %d individual expressions\n" (List.length all_exprs);
1569+
walk_list (all_exprs |> List.map (fun e -> Expression e)) t comments
1570+
| _ ->
1571+
(* Fallback to regular apply handling *)
1572+
let before, inside, after = partition_by_loc comments call_expr.pexp_loc in
1573+
let after =
1574+
if is_block_expr call_expr then (
1575+
let after_expr, rest =
1576+
partition_adjacent_trailing call_expr.pexp_loc after
1577+
in
1578+
walk_expression call_expr t (List.concat [before; inside; after_expr]);
1579+
rest)
1580+
else (
1581+
attach t.leading call_expr.pexp_loc before;
1582+
walk_expression call_expr t inside;
1583+
after)
1584+
in
1585+
let after_expr, rest =
1586+
partition_adjacent_trailing call_expr.pexp_loc after
1587+
in
1588+
attach t.trailing call_expr.pexp_loc after_expr;
1589+
walk_list
1590+
(arguments
1591+
|> List.map (fun (lbl, expr) ->
1592+
let loc =
1593+
match lbl with
1594+
| Asttypes.Labelled {loc} | Optional {loc} ->
1595+
{loc with loc_end = expr.Parsetree.pexp_loc.loc_end}
1596+
| _ -> expr.pexp_loc
1597+
in
1598+
ExprArgument {expr; loc}))
1599+
t rest)
1600+
| _ ->
1601+
(* Regular apply handling *)
1602+
let before, inside, after = partition_by_loc comments call_expr.pexp_loc in
1603+
let after =
1604+
if is_block_expr call_expr then (
1605+
let after_expr, rest =
1606+
partition_adjacent_trailing call_expr.pexp_loc after
1607+
in
1608+
walk_expression call_expr t (List.concat [before; inside; after_expr]);
1609+
rest)
1610+
else (
1611+
attach t.leading call_expr.pexp_loc before;
1612+
walk_expression call_expr t inside;
1613+
after)
1614+
in
1615+
let after_expr, rest =
1616+
partition_adjacent_trailing call_expr.pexp_loc after
1617+
in
1618+
attach t.trailing call_expr.pexp_loc after_expr;
1619+
walk_list
1620+
(arguments
1621+
|> List.map (fun (lbl, expr) ->
1622+
let loc =
1623+
match lbl with
1624+
| Asttypes.Labelled {loc} | Optional {loc} ->
1625+
{loc with loc_end = expr.Parsetree.pexp_loc.loc_end}
1626+
| _ -> expr.pexp_loc
1627+
in
1628+
ExprArgument {expr; loc}))
1629+
t rest)
15711630
| Pexp_fun _ | Pexp_newtype _ -> (
15721631
let _, parameters, return_expr = fun_expr expr in
15731632
let comments =

compiler/syntax/src/res_printer.ml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4054,18 +4054,21 @@ and print_belt_array_concat_apply ~state sub_lists cmt_tbl =
40544054
| _ -> Doc.concat [Doc.text ","; Doc.line]
40554055
in
40564056
let spread_doc = make_spread_doc comma_before_spread spread in
4057+
let expressions_doc =
4058+
Doc.join
4059+
~sep:(Doc.concat [Doc.text ","; Doc.line])
4060+
(List.map
4061+
(fun expr ->
4062+
let doc = print_expression_with_comments ~state expr cmt_tbl in
4063+
match Parens.expr expr with
4064+
| Parens.Parenthesized -> add_parens doc
4065+
| Braced braces -> print_braces doc expr braces
4066+
| Nothing -> doc)
4067+
expressions)
4068+
in
40574069
Doc.concat
40584070
[
4059-
Doc.join
4060-
~sep:(Doc.concat [Doc.text ","; Doc.line])
4061-
(List.map
4062-
(fun expr ->
4063-
let doc = print_expression_with_comments ~state expr cmt_tbl in
4064-
match Parens.expr expr with
4065-
| Parens.Parenthesized -> add_parens doc
4066-
| Braced braces -> print_braces doc expr braces
4067-
| Nothing -> doc)
4068-
expressions);
4071+
expressions_doc;
40694072
spread_doc;
40704073
]
40714074
in

0 commit comments

Comments
 (0)