Skip to content

Commit

Permalink
Merge pull request #703 from emacs-php/fix/indentation
Browse files Browse the repository at this point in the history
Fix indentation of expression continuation in arglist
  • Loading branch information
zonuexe authored Sep 17, 2022
2 parents 817ac84 + ff16da2 commit 6513efa
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 5 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@

All notable changes of the PHP Mode 1.19.1 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.

<!-- ## Unreleased -->
## Unreleased

### Changed

* Make continued expressions inside lists (arguments and arrays, etc.) have the same indent width as outside the list ([#703])
* (internal) Improved readability of test failures about indentation ([#707])

### Fixed

* Removed invalid definitions that caused errors in some expressions ([#704])

[#703]: https://github.com/emacs-php/php-mode/pull/703
[#704]: https://github.com/emacs-php/php-mode/pull/704
[#707]: https://github.com/emacs-php/php-mode/pull/707

## [1.24.1] - 2022-10-08

Expand Down
35 changes: 32 additions & 3 deletions lisp/php-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
(require 'regexp-opt)
(defvar add-log-current-defun-header-regexp)
(defvar add-log-current-defun-function)
(defvar c-syntactic-context)
(defvar c-vsemi-status-unknown-p)
(defvar syntax-propertize-via-font-lock))

Expand Down Expand Up @@ -603,11 +604,39 @@ might be to handle switch and goto labels differently."
(defun php-lineup-cascaded-calls (langelem)
"Line up chained methods using `c-lineup-cascaded-calls',
but only if the setting is enabled."
(if php-mode-lineup-cascaded-calls
(c-lineup-cascaded-calls langelem)
(cond
(php-mode-lineup-cascaded-calls (c-lineup-cascaded-calls langelem))
((assq 'arglist-cont-nonempty c-syntactic-context) nil)
((assq 'defun-block-intro c-syntactic-context) nil)
((assq 'defun-close c-syntactic-context) nil)
((assq 'statement-cont c-syntactic-context) nil)
(t
(save-excursion
(beginning-of-line)
(if (looking-at-p "\\s-*->") '+ nil))))
(let ((beginning-of-langelem (cdr langelem))
(beginning-of-current-line (point))
start)
(skip-chars-forward " ")
(cond
((looking-at-p "->") '+)
((looking-at-p "[:?]") '+)
((looking-at-p "[,;]") nil)
;; Is the previous line terminated with `,' ?
((progn
(forward-line -1)
(end-of-line)
(skip-chars-backward " ")
(backward-char 1)
(while (and (< beginning-of-langelem (point))
(setq start (php-in-string-or-comment-p)))
(goto-char start)
(skip-chars-backward " ")
(backward-char 1))
(and (not (eq (point) beginning-of-current-line))
(not (looking-at-p ","))
(not (php-in-string-or-comment-p))))
'+)
(t nil)))))))

(defun php-c-looking-at-or-maybe-in-bracelist (&optional _containing-sexp lim)
"Replace `c-looking-at-or-maybe-in-bracelist'.
Expand Down
2 changes: 1 addition & 1 deletion tests/indent/issue-623.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

$arr = [
$object->something() // ###php-mode-test### ((indent 4))
/* comment */ ->something() // ###php-mode-test### ((indent 4))
/* comment */ ->something() // ###php-mode-test### ((indent 8))
->something(), // ###php-mode-test### ((indent 8))
]; // ###php-mode-test### ((indent 0))
37 changes: 37 additions & 0 deletions tests/indent/issue-702.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

PHP_VERSION_ID === 80000
? 'foo'
: 'bar';

$a = [
'key' => PHP_VERSION_ID === 80000
? 'foo'
: 'bar',
true &&
false,
false
|| true,
'value1'
,
'value2'
,
];

var_dump(
PHP_VERSION_ID === 80000
? 'foo'
: 'bar',
true && // ###php-mode-test### ((indent 4))
false,
false // ###php-mode-test### ((indent 4))
|| true, // ###php-mode-test### ((indent 8))
// ###php-mode-test### ((indent 4))
1 // ###php-mode-test### ((indent 4))
+ 2 // ###php-mode-test### ((indent 8))
/ 3, // ###php-mode-test### ((indent 8))
'value1' // ###php-mode-test### ((indent 4))
, // ###php-mode-test### ((indent 4))
'value2' // ###php-mode-test### ((indent 4))
, // ###php-mode-test### ((indent 4))
);
5 changes: 5 additions & 0 deletions tests/php-mode-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ be processed."
(lambda (offset)
(let ((current-offset (current-indentation)))
(unless (eq current-offset offset)
(warn "line: %d context: %s\n" (line-number-at-pos) (c-guess-basic-syntax))
(list :line (line-number-at-pos)
:expected offset
:actual current-offset))))))
Expand Down Expand Up @@ -649,6 +650,10 @@ Meant for `php-mode-test-issue-503'."
"Proper alignment object -> accessor."
(with-php-mode-test ("indent/issue-623.php" :indent t :magic t)))

(ert-deftest php-mode-test-issue-702 ()
"Proper alignment arglist."
(with-php-mode-test ("indent/issue-702.php" :indent t :magic t)))

(ert-deftest php-mode-test-php74 ()
"Test highlighting language constructs added in PHP 7.4."
(with-php-mode-test ("7.4/arrow-function.php" :faces t))
Expand Down

0 comments on commit 6513efa

Please sign in to comment.