Skip to content

Commit

Permalink
Tweak indentation of class definition with non-hanging object
Browse files Browse the repository at this point in the history
Indent non-hanging `object` instead of aligning with their preceding
`class` token. Previously:

class c =
object
  method m = 1
end

Now:

class c =
  object
    method m = 1
  end

which agrees with default ocp-indent and seems to be the more modern
usage. Also indent `initialize` clauses correctly. (ocaml#239)
  • Loading branch information
mattiase committed Jun 27, 2021
1 parent e46ce6f commit c1d5aa1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
29 changes: 28 additions & 1 deletion indent-test.ml
Expand Up @@ -852,11 +852,38 @@ let () =
>>= fun () -> step2)

class c (a : b) =
object
object
inherit d
method m = 1
end

class c (a : b) =
object(self)
inherit d
method m = 1
end

class c (a : b) = object
inherit d
method m = 1
end

class c (a : b) = object(self)
inherit d
method m = 1
end

class type restricted_point_type =
object
method get_x : int
method bump : unit
end

class type restricted_point_type = object
method get_x : int
method bump : unit
end

let f = {
a = 1;
}
Expand Down
5 changes: 3 additions & 2 deletions tuareg.el
Expand Up @@ -2221,15 +2221,16 @@ Return values can be
;; An important role of this first condition is to call smie-indent-virtual
;; so that we get called back to compute the (virtual) indentation of
;; "object", thus making sure we get called back to apply the second rule.
((and (member token '("inherit" "val" "method" "constraint"))
((and (member token '("inherit" "val" "method" "constraint" "initializer"))
(smie-rule-parent-p "object"))
(save-excursion
(forward-word 1)
(goto-char (nth 1 (smie-backward-sexp 'halfsexp)))
(let ((col (smie-indent-virtual)))
`(column . ,(+ tuareg-default-indent col)))))
;; For "class foo = object(type)...end", align object...end with class.
((and (equal token "object") (smie-rule-parent-p "class"))
((and (equal token "object") (smie-rule-parent-p "class")
(not (smie-rule-bolp)))
(smie-rule-parent))))

(defun tuareg-smie--if-then-hack (token)
Expand Down

0 comments on commit c1d5aa1

Please sign in to comment.