ocaml-ppx / ocamlformat

Auto-formatter for OCaml code
MIT License
619 stars 174 forks source link

Improve the indentation of classes and class types #2423

Open Julow opened 1 year ago

Julow commented 1 year ago

The indentation of the object keyword in class types is not ideal as it's aligned with both what came before it and with the content of the object.

class cls : object
  method foo : string
end

class cls :
  foooooooooooooooooooooo:string ->
  baaaaaaaaaaaaaaaaaaaaar:float ->
  object
  method foo : string
end

class cls :
  let open Foo in
  object
  method foo : string
end

The indentation of classes is entirely different, the object keyword is indented and its content too. This wastes vertical space for no reason as every classes defined with an object .. end have the same structure, where the object is at the end.

class cls =
  object
    method foo = bar
  end

class cls ~foooooooooooooooooooooooooooooooo
  ~baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar =
  object
    method foo = bar
  end

class cls =
  let open Foo in
  object
    method foo = bar
  end

Proposal

class cls : object
  method foo : string
end

class cls :
  foooooooooooooooooooooo:string ->
  baaaaaaaaaaaaaaaaaaaaar:float ->
object
  method foo : string
end

class cls :
  let open Foo in
object
  method foo : string
end
class cls = object
  method foo = bar
end

class cls ~foooooooooooooooooooooooooooooooo
  ~baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar =
object
  method foo = bar
end

class cls =
  let open Foo in
object
  method foo = bar
end
gpetiot commented 1 year ago

It wouldn't shock me to de-indent the object keyword, that's done for struct in some cases and it looks nice:

module M (A : sig
  module type T
end) (B : sig
  module type T
end) =
struct
  let f : ((module A.T), (module B.T)) t -> string = function B s -> s
end
Julow commented 12 months ago

The smallest of the two proposed changes is merged: https://github.com/ocaml-ppx/ocamlformat/pull/2425