Skip to content

Commit 7bde652

Browse files
committed
Keep string and heredoc lines untouched when indenting
php-indent-line forced any heredoc line made of a single identifier to column zero. That check also matched body lines such as "FROM" in an SQL heredoc, silently changing the string, and it moved real closing markers too: since PHP 7.3 the closing marker's indentation is stripped from every body line, so dedenting it changes the string value as well. Leave every string, heredoc and nowdoc line as it is, like the CC Mode based implementation did. When such a line (or one in an HTML region) is left unchanged, still move point out of the leading whitespace to the end of the indentation, matching CC Mode's TAB behavior.
1 parent 0e32f79 commit 7bde652

2 files changed

Lines changed: 35 additions & 19 deletions

File tree

‎lisp/php-indent.el‎

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -668,36 +668,26 @@ PHP open/close tag precedes the line, it is treated as PHP."
668668
(and (re-search-backward "<\\?php\\|<\\?=\\|<\\?\\|\\?>" nil t)
669669
(string= (match-string 0) "?>"))))
670670

671-
(defun php-indent--heredoc-closing-line-p ()
672-
"Return non-nil if the current line is a heredoc/nowdoc closing marker.
673-
The closing marker is a bare identifier, optionally followed by `;', `,'
674-
or `)', on its own line."
675-
(save-excursion
676-
(beginning-of-line)
677-
(looking-at "[ \t]*[A-Za-z_][A-Za-z0-9_]*[ \t]*[;,)]?[ \t]*$")))
678-
679671
;;; Entry points
680672

681673
;;;###autoload
682674
(defun php-indent-line ()
683675
"Indent the current line as PHP.
684676
Intended for use as `indent-line-function'. Lines inside an HTML region
685-
or inside a string/heredoc body are left unchanged; heredoc closing
686-
markers are forced to column zero."
677+
or inside a string, heredoc or nowdoc (including its closing marker) keep
678+
their indentation; as in CC Mode, point still moves to the end of the
679+
indentation when it was inside it."
687680
(interactive)
688681
(let* ((parse-status
689682
(save-excursion (syntax-ppss (line-beginning-position))))
690683
(offset (- (point) (save-excursion (back-to-indentation) (point)))))
691684
(cond
692-
;; Do not touch lines that belong to an HTML (?> ... <?php) region.
693-
((php-indent--in-html-p) nil)
694-
;; Heredoc/nowdoc closing marker: force column zero.
695-
((and (nth 3 parse-status)
696-
(php-indent--heredoc-closing-line-p))
697-
(indent-line-to 0)
698-
(when (> offset 0) (forward-char offset)))
699-
;; Inside a string or heredoc body: keep the current indentation.
700-
((nth 3 parse-status) nil)
685+
;; Do not touch lines that belong to an HTML (?> ... <?php) region,
686+
;; nor string or heredoc/nowdoc lines. Since PHP 7.3 the closing
687+
;; marker's indentation is stripped from every heredoc body line, so
688+
;; moving either one would change the string value.
689+
((or (php-indent--in-html-p) (nth 3 parse-status))
690+
(when (< offset 0) (back-to-indentation)))
701691
(t
702692
(indent-line-to (php-indent--proper-indentation parse-status))
703693
(when (> offset 0) (forward-char offset))))))

‎tests/php-mode-test.el‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,32 @@ runs from `hack-local-variables-hook'."
545545
(php-indent-line)
546546
(should (= (current-indentation) php-indent-offset))))
547547

548+
(ert-deftest php-mode-test-indent-keeps-heredoc-lines ()
549+
"Indenting never changes heredoc body or closing marker indentation.
550+
Since PHP 7.3 the closing marker's indentation is stripped from every
551+
body line, so moving either one changes the string value."
552+
(let ((code "<?php\nfunction f() {\n $sql = <<<SQL\n SELECT *\n FROM\n SQL;\n return $sql;\n}\n"))
553+
(with-temp-buffer
554+
(insert code)
555+
(php-mode)
556+
(indent-region (point-min) (point-max))
557+
(should (equal (buffer-string) code)))))
558+
559+
(ert-deftest php-mode-test-tab-moves-point-into-string-indentation ()
560+
"TAB in the leading whitespace of a string line moves to its indentation.
561+
The line itself is left unchanged, as in CC Mode."
562+
(with-temp-buffer
563+
(insert "<?php\n$s = 'multi\n line';\n")
564+
(php-mode)
565+
(goto-char (point-min))
566+
(forward-line 2)
567+
(forward-char 2)
568+
(let ((tick (buffer-chars-modified-tick))
569+
(tab-always-indent t))
570+
(indent-for-tab-command)
571+
(should (= (buffer-chars-modified-tick) tick))
572+
(should (= (current-column) 5)))))
573+
548574
(ert-deftest php-mode-test-switch-statements()
549575
"Test indentation inside switch statements"
550576
(with-php-mode-test ("switch-statements.php" :indent t :style pear)

0 commit comments

Comments
 (0)