branch: externals/phps-mode commit 2833a34b0d56e7b8240e809f467634fc5e29953b Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Improved alternative for assignment --- phps-mode-analyzer.el | 37 +++++++++++++++++-- phps-mode.el | 4 +-- test/phps-mode-test-functions.el | 76 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 5 deletions(-) diff --git a/phps-mode-analyzer.el b/phps-mode-analyzer.el index 5dd928d..36d5bf1 100644 --- a/phps-mode-analyzer.el +++ b/phps-mode-analyzer.el @@ -3268,7 +3268,7 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." "Apply alternative indentation at POINT here." (unless point (setq point (point))) - (let ((new-indentation) + (let ((new-indentation 0) (point-at-end-of-line (equal point (line-end-position)))) (save-excursion (let ((line-number (line-number-at-pos point)) @@ -3307,9 +3307,10 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (let* ((old-indentation (current-indentation)) (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)) + (line-ends-with-assignment (phps-mode-analyzer--string-ends-with-assignment-p line-string)) + (line-ends-with-semicolon (phps-mode-analyzer--string-ends-with-semicolon-p line-string)) (bracket-level (phps-mode-analyzer--get-string-brackets-count line-string))) (setq new-indentation old-indentation) - (forward-line move-length) (when (> bracket-level 0) @@ -3327,6 +3328,28 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (when current-line-starts-with-closing-bracket (setq new-indentation (- new-indentation tab-width))) + (when line-ends-with-assignment + (setq new-indentation (+ new-indentation tab-width))) + + (when line-ends-with-semicolon + ;; Back-trace buffer from previous line + ;; Determine if semi-colon ended an assignment or not + (forward-line (* -1 move-length)) + (let ((not-found t) + (is-assignment nil)) + (while (and + not-found + (search-backward-regexp "\\(;\\|=\\)" nil t)) + (let ((match (buffer-substring-no-properties (match-beginning 0) (match-end 0)))) + (setq is-assignment (string= match "=")) + (setq not-found nil) + )) + ;; If it ended an assignment, decrease indentation + (when is-assignment + (setq new-indentation (- new-indentation tab-width)))) + + (goto-char point)) + ;; Decrease indentation if current line decreases in bracket level (when (< new-indentation 0) (setq new-indentation 0)) @@ -3371,7 +3394,15 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (defun phps-mode-analyzer--string-starts-with-closing-bracket-p (string) "Get bracket count for STRING." - (string-match-p "^[\n\r\t ]*\\([\]{}()[]\\|<[a-zA-Z]+\\|</[a-zA-Z]+\\|/>\\)" string)) + (string-match-p "^[\r\t ]*\\([\]{}()[]\\|<[a-zA-Z]+\\|</[a-zA-Z]+\\|/>\\)" string)) + +(defun phps-mode-analyzer--string-ends-with-assignment-p (string) + "Get bracket count for STRING." + (string-match-p "[\t ]*=$" string)) + +(defun phps-mode-analyzer--string-ends-with-semicolon-p (string) + "Get bracket count for STRING." + (string-match-p ";$" string)) (defun phps-mode-functions--cancel-idle-timer () "Cancel idle timer." diff --git a/phps-mode.el b/phps-mode.el index 2b7f7df..8601856 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: 12 Dec 2019 -;; Version: 0.3.24 +;; Modified: 13 Dec 2019 +;; Version: 0.3.25 ;; 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 739c865..31aef62 100644 --- a/test/phps-mode-test-functions.el +++ b/test/phps-mode-test-functions.el @@ -178,6 +178,82 @@ buffer-contents "/**\n *\n */\n")))) + (phps-mode-test-with-buffer + "$var = 'abc';\n// Comment" + "Alternative indentation on single-line assignment" + (goto-char 1) + (should (equal + (phps-mode-analyzer--alternative-indentation) + 0)) + (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) + (should (equal + buffer-contents + "$var = 'abc';\n// Comment")))) + + (phps-mode-test-with-buffer + "$var = 'abc';\n// Comment" + "Alternative indentation on line after single-line assignment" + (goto-char 15) + (should (equal + (phps-mode-analyzer--alternative-indentation) + 0)) + (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) + (should (equal + buffer-contents + "$var = 'abc';\n// Comment")))) + + (phps-mode-test-with-buffer + "$var =\n 'abc';\n$var =\n 'abc'\n . 'def';\n// Comment\n" + "Alternative indentation on first line of multi-line assignment" + (goto-char 1) + (should (equal + (phps-mode-analyzer--alternative-indentation) + 0)) + (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) + (should (equal + buffer-contents + "$var =\n 'abc';\n$var =\n 'abc'\n . 'def';\n// Comment\n")))) + + (phps-mode-test-with-buffer + "$var =\n 'abc';\n$var =\n 'abc'\n . 'def';\n// Comment\n" + "Alternative indentation on second line of multi-line assignment" + (goto-char 30) + (should (equal + (phps-mode-analyzer--alternative-indentation) + 4)) + (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) + (should (equal + buffer-contents + "$var =\n 'abc';\n$var =\n 'abc'\n . 'def';\n// Comment\n")))) + + (phps-mode-test-with-buffer + "$var =\n 'abc';\n$var =\n 'abc'\n . 'def';\n// Comment\n" + "Alternative indentation on last line of multi-line assignment" + (goto-char 12) + (should (equal + (phps-mode-analyzer--alternative-indentation) + 4)) + (goto-char 40) + (should (equal + (phps-mode-analyzer--alternative-indentation) + 4)) + (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) + (should (equal + buffer-contents + "$var =\n 'abc';\n$var =\n 'abc'\n . 'def';\n// Comment\n")))) + + (phps-mode-test-with-buffer + "$var =\n 'abc';\n$var =\n 'abc'\n . 'def';\n// Comment\n" + "Alternative indentation on line after multi-line assignment" + (goto-char 53) + (should (equal + (phps-mode-analyzer--alternative-indentation) + 0)) + (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) + (should (equal + buffer-contents + "$var =\n 'abc';\n$var =\n 'abc'\n . 'def';\n// Comment\n")))) + ) (defun phps-mode-test-functions-move-lines-indent ()