branch: externals/phps-mode commit 8692934d20e44babb257e439535145128e2d8f7c Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Improved token-blind indentation for } else { blocks --- phps-mode-analyzer.el | 22 +++++++++++++++------- phps-mode.el | 4 ++-- test/phps-mode-test-functions.el | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/phps-mode-analyzer.el b/phps-mode-analyzer.el index d8418c6..4b2291e 100644 --- a/phps-mode-analyzer.el +++ b/phps-mode-analyzer.el @@ -3268,7 +3268,8 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." "Apply alternative indentation at POINT here." (unless point (setq point (point))) - (let ((new-indentation)) + (let ((new-indentation) + (point-at-end-of-line (equal point (line-end-position)))) (save-excursion (let ((line-number (line-number-at-pos point)) (move-length 0) @@ -3304,7 +3305,8 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (unless line-is-empty (let* ((old-indentation (current-indentation)) - (new-bracket-level (phps-mode-analyzer--get-string-brackets-count current-line-string)) + (current-line-starts-with-closing-bracket (phps-mode-analyzer--string-starts-with-closing-bracket-p current-line-string)) + (line-starts-with-closing-bracket (phps-mode-analyzer--string-starts-with-closing-bracket-p line-string)) (bracket-level (phps-mode-analyzer--get-string-brackets-count line-string))) (setq new-indentation old-indentation) @@ -3313,10 +3315,11 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (when (> bracket-level 0) (setq new-indentation (+ new-indentation tab-width))) - (when (< bracket-level 0) - (setq new-indentation (- new-indentation tab-width))) + (when (and (= bracket-level 0) + line-starts-with-closing-bracket) + (setq new-indentation (+ new-indentation tab-width))) - (when (< new-bracket-level 0) + (when current-line-starts-with-closing-bracket (setq new-indentation (- new-indentation tab-width))) ;; Decrease indentation if current line decreases in bracket level @@ -3324,8 +3327,9 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (setq new-indentation 0)) (indent-line-to new-indentation)))))) - ;; Only move to end of line if point is the current point - (when (equal point (point)) + ;; Only move to end of line if point is the current point and is at end of line + (when (and (equal point (point)) + point-at-end-of-line) (end-of-line)) new-indentation)) @@ -3354,6 +3358,10 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (setq bracket-level (1- bracket-level))))))) (* bracket-level tab-width))) +(defun phps-mode-analyzer--string-starts-with-closing-bracket-p (string) + "Get bracket count for STRING." + (string-match-p "^\\([\]{}()[]\\|<[a-zA-Z]+\\|</[a-zA-Z]+\\|/>\\)" string)) + (defun phps-mode-functions--cancel-idle-timer () "Cancel idle timer." (phps-mode-debug-message (message "Cancelled idle timer")) diff --git a/phps-mode.el b/phps-mode.el index 131de4c..203ae0e 100644 --- a/phps-mode.el +++ b/phps-mode.el @@ -5,8 +5,8 @@ ;; Author: Christian Johansson <christ...@cvj.se> ;; Maintainer: Christian Johansson <christ...@cvj.se> ;; Created: 3 Mar 2018 -;; Modified: 21 Nov 2019 -;; Version: 0.3.17 +;; Modified: 24 Nov 2019 +;; Version: 0.3.19 ;; Keywords: tools, convenience ;; URL: https://github.com/cjohansson/emacs-phps-mode diff --git a/test/phps-mode-test-functions.el b/test/phps-mode-test-functions.el index f87c16d..77aaf14 100644 --- a/test/phps-mode-test-functions.el +++ b/test/phps-mode-test-functions.el @@ -118,6 +118,30 @@ buffer-contents "<?php\nif ($test) {\n if ($test2) {\n\n }\n}")))) + (phps-mode-test-with-buffer + "<?php\nif ($test) {\n if ($test2) {\n \n }\n\n}" + "Alternative indentation on multiple closing brackets" + (goto-char 53) + (should (equal + (phps-mode-analyzer--alternative-indentation) + 4)) + (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) + (should (equal + buffer-contents + "<?php\nif ($test) {\n if ($test2) {\n \n }\n \n}")))) + + (phps-mode-test-with-buffer + "<?php\nif ($test) {\n \n} else if ($test) {\n \n}\n" + "Alternative indentation on elseif block" + (goto-char 25) + (should (equal + (phps-mode-analyzer--alternative-indentation) + 0)) + (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) + (should (equal + buffer-contents + "<?php\nif ($test) {\n \n} else if ($test) {\n \n}\n")))) + ) (defun phps-mode-test-functions-move-lines-indent ()