Skip to content

Pexp_braces #7373

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion analysis/src/CompletionExpressions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let rec traverseExpr (exp : Parsetree.expression) ~exprPath ~pos
let locHasCursor loc = loc |> CursorPosition.locHasCursor ~pos in
let someIfHasCursor v = if locHasCursor exp.pexp_loc then Some v else None in
match exp.pexp_desc with
| Pexp_ident {txt = Lident txt} when Utils.hasBraces exp.pexp_attributes ->
| Pexp_braces {pexp_desc = Pexp_ident {txt = Lident txt}} ->
(* An ident with braces attribute corresponds to for example `{n}`.
Looks like a record but is parsed as an ident with braces. *)
someIfHasCursor (txt, [Completable.NRecordBody {seenFields = []}] @ exprPath)
Expand Down
4 changes: 1 addition & 3 deletions analysis/src/Utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ let identifyPexp pexp =
| Pexp_pack _ -> "Pexp_pack"
| Pexp_extension _ -> "Pexp_extension"
| Pexp_open _ -> "Pexp_open"
| Pexp_braces _ -> "Pexp_braces"

let identifyPpat pat =
match pat with
Expand Down Expand Up @@ -140,9 +141,6 @@ let rec skipWhite text i =
| ' ' | '\n' | '\r' | '\t' -> skipWhite text (i - 1)
| _ -> i

let hasBraces attributes =
attributes |> List.exists (fun (loc, _) -> loc.Location.txt = "res.braces")

let rec unwrapIfOption (t : Types.type_expr) =
match t.desc with
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> unwrapIfOption t1
Expand Down
74 changes: 40 additions & 34 deletions analysis/src/Xform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -223,56 +223,62 @@ end
module AddBracesToFn = struct
(* Add braces to fn without braces *)

let mkIterator ~pos ~changed =
let map_structure ~pos ~(changed : Parsetree.structure) =
(* While iterating the AST, keep info on which structure item we are in.
Printing from the structure item, rather than the body of the function,
gives better local pretty printing *)
let currentStructureItem = ref None in

let structure_item (iterator : Ast_iterator.iterator)
(item : Parsetree.structure_item) =
let saved = !currentStructureItem in
currentStructureItem := Some item;
Ast_iterator.default_iterator.structure_item iterator item;
currentStructureItem := saved
let did_map = ref false in
let current_structure = ref None in
let structure_item (mapper : Ast_mapper.mapper)
(structure_item : Parsetree.structure_item) =
if !did_map then structure_item
else (
current_structure := Some structure_item;
mapper.structure_item mapper structure_item)
in
let expr (iterator : Ast_iterator.iterator) (e : Parsetree.expression) =
let bracesAttribute =
let loc =
{
Location.none with
loc_start = Lexing.dummy_pos;
loc_end =
{
Lexing.dummy_pos with
pos_lnum = Lexing.dummy_pos.pos_lnum + 1 (* force line break *);
};
}
in
(Location.mkloc "res.braces" loc, Parsetree.PStr [])
let expr (mapper : Ast_mapper.mapper) (e : Parsetree.expression) =
let braces_loc =
{
Location.none with
loc_start = Lexing.dummy_pos;
loc_end =
{
Lexing.dummy_pos with
pos_lnum = Lexing.dummy_pos.pos_lnum + 1 (* force line break *);
};
}
in
let isFunction = function
| {Parsetree.pexp_desc = Pexp_fun _} -> true
| _ -> false
in
(match e.pexp_desc with
| Pexp_fun {rhs = bodyExpr}
match e.pexp_desc with
| Pexp_fun ({rhs = bodyExpr} as f)
when Loc.hasPos ~pos bodyExpr.pexp_loc
&& isBracedExpr bodyExpr = false
&& isFunction bodyExpr = false ->
bodyExpr.pexp_attributes <- bracesAttribute :: bodyExpr.pexp_attributes;
changed := !currentStructureItem
| _ -> ());
Ast_iterator.default_iterator.expr iterator e
did_map := true;
{
e with
pexp_desc =
Pexp_fun
{f with rhs = Ast_helper.Exp.braces ~loc:braces_loc bodyExpr};
}
| _ -> mapper.expr mapper e
in

{Ast_iterator.default_iterator with expr; structure_item}
let mapper = {Ast_mapper.default_mapper with expr; structure_item} in
let rec visit structure =
match structure with
| [] -> None
| x :: xs ->
let mapped = mapper.structure_item mapper x in
if !did_map then Some mapped else visit xs
in
visit changed

let xform ~pos ~codeActions ~path ~printStructureItem structure =
let changed = ref None in
let iterator = mkIterator ~pos ~changed in
iterator.structure iterator structure;
match !changed with
match map_structure ~pos ~changed:structure with
| None -> ()
| Some newStructureItem ->
let range = Loc.rangeOfLoc newStructureItem.pstr_loc in
Expand Down
1 change: 1 addition & 0 deletions compiler/frontend/bs_ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ module E = struct
let loc = sub.location sub loc in
let attrs = sub.attributes sub attrs in
match desc with
| Pexp_braces inner -> braces ~loc ~attrs (sub.expr sub inner)
| Pexp_ident x -> ident ~loc ~attrs (map_loc sub x)
| Pexp_constant x -> constant ~loc ~attrs x
| Pexp_let (r, vbs, e) ->
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/ast_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ module Exp = struct
let pack ?loc ?attrs a = mk ?loc ?attrs (Pexp_pack a)
let open_ ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_open (a, b, c))
let extension ?loc ?attrs a = mk ?loc ?attrs (Pexp_extension a)
let braces ?loc ?attrs a = mk ?loc ?attrs (Pexp_braces a)

let case lhs ?guard rhs = {pc_lhs = lhs; pc_guard = guard; pc_rhs = rhs}
end
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/ast_helper.mli
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ module Exp : sig
val open_ :
?loc:loc -> ?attrs:attrs -> override_flag -> lid -> expression -> expression
val extension : ?loc:loc -> ?attrs:attrs -> extension -> expression
val braces : ?loc:loc -> ?attrs:attrs -> expression -> expression

val case : pattern -> ?guard:expression -> expression -> case
end
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/ast_iterator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ module E = struct
sub.location sub loc;
sub.attributes sub attrs;
match desc with
| Pexp_braces e -> sub.expr sub e
| Pexp_ident x -> iter_loc sub x
| Pexp_constant _ -> ()
| Pexp_let (_r, vbs, e) ->
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ module E = struct
let loc = sub.location sub loc in
let attrs = sub.attributes sub attrs in
match desc with
| Pexp_braces inner -> braces ~loc ~attrs (sub.expr sub inner)
| Pexp_ident x -> ident ~loc ~attrs (map_loc sub x)
| Pexp_constant x -> constant ~loc ~attrs x
| Pexp_let (r, vbs, e) ->
Expand Down
Loading
Loading