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

Fix formatting of custom indexing operators #975

Merged
merged 2 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 21 additions & 7 deletions src/Ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,32 @@ let parens_kind i =
Some (txt, opn, cls)
| _ -> None

let index_op_get i =
match (i.[0], parens_kind i) with
| '.', Some (s, o, c) -> Some (s, o, c)
let split_index_op s =
match String.rindex s '.' with
| Some i when String.length s > i + 2 ->
let before = String.sub s ~pos:0 ~len:i in
let after = String.sub s ~pos:i ~len:(String.length s - i) in
Some ((if String.is_empty before then "" else "." ^ before), after)
| _ -> None

let index_op_get i =
match split_index_op i with
| None -> None
| Some (before, after) -> (
match parens_kind after with
| Some (s, o, c) -> Some (before ^ s, o, c)
| None -> None )

let index_op_set i =
match String.chop_suffix i ~suffix:"<-" with
| None -> None
| Some i -> (
match (i.[0], parens_kind i) with
| '.', Some (s, o, c) -> Some (s, o, c)
| _ -> None )
| Some i -> index_op_get i

let index_op_lid i ~f = f (String.concat ~sep:"" (Longident.flatten i))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why convert it to a string and then parse it in split_index_op ?
Why not inspect the long indent value (or the result of Longident.flatten) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I reduced the diff, but it is indeed easier to convert the identifiers to strings and re-use the existing index_op_get function.


let index_op_get_lid = index_op_lid ~f:index_op_get

let index_op_set_lid = index_op_lid ~f:index_op_set

let index_op_string = (".", '[', ']')

Expand Down
4 changes: 2 additions & 2 deletions src/Ast.mli
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ val is_infix_id : string -> bool
val is_infix : expression -> bool
(** Holds of infix symbol expressions. *)

val index_op_get : string -> (string * Char.t * Char.t) option
val index_op_get_lid : Longident.t -> (string * Char.t * Char.t) option

val index_op_set : string -> (string * Char.t * Char.t) option
val index_op_set_lid : Longident.t -> (string * Char.t * Char.t) option

val index_op_get_sugar :
Longident.t Location.loc
Expand Down
12 changes: 6 additions & 6 deletions src/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1719,23 +1719,23 @@ and fmt_expression c ?(box = true) ?pro ?epi ?eol ?parens ?(indent_wrap = 0)
(has_cmts, fmt_before_cmts, fmt_after_cmts, (fmt_op, args))
| None -> (false, noop, noop, (noop, args))))
| Pexp_apply
( { pexp_desc= Pexp_ident {txt= Lident id; loc}
( { pexp_desc= Pexp_ident {txt= id; loc}
; pexp_loc
; pexp_attributes= _
; _ }
, (Nolabel, s) :: (Nolabel, i) :: _ )
when Option.is_some (index_op_get id) ->
let index_op = Option.value_exn (index_op_get id) in
when Option.is_some (index_op_get_lid id) ->
let index_op = Option.value_exn (index_op_get_lid id) in
Cmts.relocate c.cmts ~src:pexp_loc ~before:loc ~after:loc ;
fmt_index_op c ctx ~parens {txt= index_op; loc} s [i]
| Pexp_apply
( { pexp_desc= Pexp_ident {txt= Lident id; loc}
( { pexp_desc= Pexp_ident {txt= id; loc}
; pexp_loc
; pexp_attributes= _
; _ }
, (Nolabel, s) :: (Nolabel, i) :: (Nolabel, e) :: _ )
when Option.is_some (index_op_set id) ->
let index_op = Option.value_exn (index_op_set id) in
when Option.is_some (index_op_set_lid id) ->
let index_op = Option.value_exn (index_op_set_lid id) in
Cmts.relocate c.cmts ~src:pexp_loc ~before:loc ~after:loc ;
fmt_index_op c ctx ~parens {txt= index_op; loc} s [i] ~set:e
| Pexp_apply (e0, [(Nolabel, e1)]) when is_prefix e0 ->
Expand Down
6 changes: 6 additions & 0 deletions test/passing/index_op.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,9 @@ let _ = Bigarray.Genarray.set s [|1; 2|] 1
let _ = Bigarray.Genarray.set s [|1; 2; 3|] 1

let _ = s.{1, 2, 3, 4} <- 1

let () =
let m = Mat.zeros 5 5 in
m.Mat.${[[2]; [5]]} |> ignore ;
let open Mat in
m.${[[2]; [5]]} |> ignore
6 changes: 3 additions & 3 deletions test/passing/js_source.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -9734,9 +9734,9 @@ module Indexop = struct

;;
let h = Hashtbl.create 17 in
Def.( .%[]<- ) h "one" 1;
Def.( .%()<- ) h "two" 2;
Def.( .%{}<- ) h "three" 3
h.Def.%["one"] <- 1;
h.Def.%("two") <- 2;
h.Def.%{"three"} <- 3

let x, y, z = Def.(h.%["one"], h.%("two"), h.%{"three"})
end
Expand Down
4 changes: 1 addition & 3 deletions test/passing/source.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -9252,9 +9252,7 @@ module Indexop = struct

;;
let h = Hashtbl.create 17 in
Def.( .%[]<- ) h "one" 1 ;
Def.( .%()<- ) h "two" 2 ;
Def.( .%{}<- ) h "three" 3
h.Def.%["one"] <- 1 ; h.Def.%("two") <- 2 ; h.Def.%{"three"} <- 3

let x, y, z = Def.(h.%["one"], h.%("two"), h.%{"three"})
end
Expand Down