Skip to content

Commit

Permalink
Indent according to parent depth, even with trailing text
Browse files Browse the repository at this point in the history
Previously we had custom logic to handle hanging arguments.

    foo($x,
        $y);

This was getting confused by other forms of trailing text after
parens:

    if (true) { // hello
      foo();
    }

foo() should not line up with { here.

This heuristic isn't useful, because hackfmt only uses the following
two forms:

    foo($x, $y);
    // or
    foo(
      $x,
      $y,
    );

Remove this heuristic. This also solves most of #29.
  • Loading branch information
Wilfred committed Apr 21, 2020
1 parent e3f8c50 commit 330df61
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
20 changes: 0 additions & 20 deletions hack-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -1720,14 +1720,6 @@ Preserves point position in the line where possible."
(paren-depth (hack--paren-depth-for-indent (line-beginning-position)))

(ppss (syntax-ppss (line-beginning-position)))
(current-paren-pos (nth 1 ppss))
(text-after-paren
(when current-paren-pos
(save-excursion
(goto-char current-paren-pos)
(buffer-substring
(1+ current-paren-pos)
(line-end-position)))))
(in-multiline-comment-p (nth 4 ppss))
(current-line (hack--current-line)))
;; If the current line is just a closing paren, unindent by one level.
Expand All @@ -1749,18 +1741,6 @@ Preserves point position in the line where possible."
(when (or (string-match-p (rx bol (0+ space) "*") current-line)
(string= "" current-line))
(hack--indent-preserve-point (1+ (* hack-indent-offset paren-depth)))))
;; Indent according to the last paren position, if there is text
;; after the paren. For example:
;; foo(bar,
;; baz, <- this line
((and
text-after-paren
(not (string-match-p (rx bol (0+ space) eol) text-after-paren)))
(let (open-paren-column)
(save-excursion
(goto-char current-paren-pos)
(setq open-paren-column (current-column)))
(hack--indent-preserve-point (1+ open-paren-column))))
;; Indent according to the amount of nesting.
(t
(let ((current-line (s-trim current-line))
Expand Down
11 changes: 11 additions & 0 deletions test/hack-unit-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ $foo
(indent-region (point-min) (point-max))
(should (string= (buffer-string) src)))))

(ert-deftest hack-indent-trailing-comment ()
(let ((src "function foo(): void { // stuff
bar();
}"))
(with-temp-buffer
(hack-mode)
(insert src)

(indent-region (point-min) (point-max))
(should (string= (buffer-string) src)))))

(ert-deftest hack-indent-xhp ()
"Ensure we indent XHP expressions correctly."
(let ((src "<?hh
Expand Down

0 comments on commit 330df61

Please sign in to comment.