branch: externals/phps-mode commit fe9cb90c441d975f917b58a4d848072597fb74fb Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Passing indent test for some multi-line assignments --- phps-mode-indent.el | 52 +++++++++++++++++++++++++++++++++++++++++++ test/phps-mode-test-indent.el | 20 ++++++++--------- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/phps-mode-indent.el b/phps-mode-indent.el index edb3978a0c..32ad502648 100644 --- a/phps-mode-indent.el +++ b/phps-mode-indent.el @@ -371,6 +371,58 @@ ) + (when + (string-match-p + "[\t ]*\\()\\|]\\);[\t ]*$" + current-line-string) + + ;; $variable = array( + ;; 'random' => + ;; 'hello' + ;; ); + ;; or + ;; $variable = [ + ;; 'random' => + ;; 'hello' + ;; ]; + (let ((old-point (point)) + (still-looking t) + (bracket-count -1)) + + ;; Try to backtrack buffer until we reach start of bracket + (while + (and + still-looking + (search-backward-regexp + "\\((\\|]\\|\\[\\|)\\)" nil t)) + (let ((match-string (match-string-no-properties 0))) + (cond + ((or + (string= match-string "(") + (string= match-string "[")) + (setq bracket-count (1+ bracket-count))) + ((or + (string= match-string ")") + (string= match-string "]")) + (setq bracket-count (1- bracket-count))))) + (when (= bracket-count 0) + (setq still-looking nil))) + + ;; Did we find bracket start line? + (unless still-looking + (let ((bracket-start-indentation + (phps-mode-indent--string-indentation + (buffer-substring-no-properties + (line-beginning-position) + (line-end-position))))) + ;; Use its indentation for this line as well + (setq new-indentation bracket-start-indentation))) + + ;; Reset point + (goto-char old-point)) + + ) + (when (> previous-bracket-level 0) (if (< previous-bracket-level tab-width) (setq new-indentation (+ new-indentation 1)) diff --git a/test/phps-mode-test-indent.el b/test/phps-mode-test-indent.el index 9d4eaff741..cff7c9be2e 100644 --- a/test/phps-mode-test-indent.el +++ b/test/phps-mode-test-indent.el @@ -317,11 +317,19 @@ (phps-mode-test-indent--should-equal "<?php\n$variable = array(\n 'random4'\n);\n$variable = true;\n" - "Array assignment on three lines") + "Array assignment on three lines without trailing comma") + + (phps-mode-test-indent--should-equal + "<?php\n$variable = array(\n 'random4',\n);\n$variable = true;\n" + "Array assignment on three lines with trailing comma") (phps-mode-test-indent--should-equal "<?php\n$variable = array(\n 'random4' =>\n 'hello'\n);" - "Array assignment with double arrow elements on four lines") + "Array assignment with double arrow elements on four lines without trailing comma") + + (phps-mode-test-indent--should-equal + "<?php\n$variable = array(\n 'random4' =>\n 'hello',\n);" + "Array assignment with double arrow elements on four lines with trailing comma") (phps-mode-test-indent--should-equal "<?php\n$variable = array(\n 'random4');\n$variable = true;\n" @@ -393,14 +401,6 @@ "<?php\nforeach ($array as $value):\n echo 'Something';\n echo 'Something';\nendforeach;\necho 'Something else';\n" "Alternative control structures basic foreach-endforeach flow") - (phps-mode-test-indent--should-equal - "<?php\nif (true):\n echo 'Something';\nelseif (true\n && true\n):\n echo 'Something';\nelse:\n echo 'Something else';\n echo 'Something else again';\nendif;\necho true;\n" - "Alternative control structures with multi-line elseif 1") - - (phps-mode-test-indent--should-equal - "<?php\nif (true):\n echo 'Something';\nelseif (true\n && true):\n echo 'Something';\nelse:\n echo 'Something else';\n echo 'Something else again';\nendif;\necho true;\n" - "Alternative control structures with multi-line elseif 2") - ) (defun phps-mode-test-indent--get-lines-indent-classes ()