branch: externals/phps-mode commit a6b7363492b982d1163b29323779b5dcfc663395 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Improved token-blind indentation around echo and print statements --- phps-mode-lex-analyzer.el | 36 ++++++++++++++++++++++++------------ phps-mode.el | 4 ++-- test/phps-mode-test-lex-analyzer.el | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/phps-mode-lex-analyzer.el b/phps-mode-lex-analyzer.el index 231a167..4497595 100644 --- a/phps-mode-lex-analyzer.el +++ b/phps-mode-lex-analyzer.el @@ -766,7 +766,7 @@ "Moved imenu from START by DIFF points." (when phps-mode-lex-analyzer--imenu (setq phps-mode-lex-analyzer--imenu - (phps-mode-lex-analyzer--get-moved-imenu phps-mode-lex-analyzer--imenu start diff)) + (phps-mode-lex-analyzer--get-moved-imenu phps-mode-lex-analyzer--imenu start diff)) (phps-mode-lex-analyzer--reset-imenu))) (defun phps-mode-lex-analyzer--move-lines-indent (start-line-number diff) @@ -2087,39 +2087,51 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (when line-starts-with-opening-doc-comment (setq new-indentation (+ new-indentation 1))) - (when line-ends-with-assignment + (when (and + line-ends-with-assignment + (<= bracket-level 0)) (setq new-indentation (+ new-indentation tab-width))) - (when (and line-ends-with-opening-bracket - (< bracket-level 0)) + (when (and + line-ends-with-opening-bracket + (< bracket-level 0)) (setq new-indentation (+ new-indentation tab-width))) (when line-ends-with-terminus ;; Back-trace buffer from previous line - ;; Determine if semi-colon ended an assignment or not + ;; Determine if semi-colon ended an assignment or bracket-less command or not (forward-line (* -1 move-length)) (end-of-line) (forward-char -1) (let ((not-found t) (is-assignment nil) - (parenthesis-level 0)) + (parenthesis-level 0) + (is-bracket-less-command nil)) (while (and not-found - (search-backward-regexp "\\(;\\|{\\|(\\|)\\|=\\)" nil t)) + (search-backward-regexp "\\(;\\|{\\|(\\|)\\|=\\|echo[\t ]+\\|print[\t ]+\\)" nil t)) (let ((match (buffer-substring-no-properties (match-beginning 0) (match-end 0)))) (when (string= match ")") (setq parenthesis-level (1- parenthesis-level))) (when (= parenthesis-level 0) (setq is-assignment (string= match "=")) + (setq is-bracket-less-command + (string-match-p + "\\(echo[\t ]+\\|print[\t ]+\\)" + match)) (setq not-found nil)) (when (string= match "(") (setq parenthesis-level (1+ parenthesis-level))))) ;; If it ended an assignment on a previous line, decrease indentation - (when (and is-assignment - (> bracket-level -1) - (not - (= line-number (line-number-at-pos)))) + (when + (and + (or + (and + is-assignment + (> bracket-level -1)) + is-bracket-less-command) + (not (= line-number (line-number-at-pos)))) ;; NOTE stuff like $var = array(\n 4\n);\n ;; will end assignment but also decrease bracket-level (setq new-indentation (- new-indentation tab-width)))) @@ -2175,7 +2187,7 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (string-match-p "^[\t ]*\\([\]})[]\\|</[a-zA-Z]+\\|/>\\)" string)) (defun phps-mode-lex-analyzer--string-starts-with-opening-doc-comment-p (string) - "Get bracket count for STRING." + "Does string start with opening doc comment?" (string-match-p "^[\t ]*/\\*\\*" string)) (defun phps-mode-lex-analyzer--string-ends-with-opening-bracket-p (string) diff --git a/phps-mode.el b/phps-mode.el index 7af158c..7e1c941 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: 19 May 2020 -;; Version: 0.3.50 +;; Modified: 25 May 2020 +;; Version: 0.3.51 ;; Keywords: tools, convenience ;; URL: https://github.com/cjohansson/emacs-phps-mode diff --git a/test/phps-mode-test-lex-analyzer.el b/test/phps-mode-test-lex-analyzer.el index f466219..7faabbe 100644 --- a/test/phps-mode-test-lex-analyzer.el +++ b/test/phps-mode-test-lex-analyzer.el @@ -249,6 +249,26 @@ "$applications =\n $transaction->getResponseBodyDecoded();\n// TODO Here\n" )))) + (phps-mode-test--with-buffer + "<?php\necho '<dl><dt>' . __('Data', 'something')\n . ':</dt><dd><pre>' . print_r($decodedData, true) . '</pre></dd></dl>';\necho '<div class=\"meta actions\">';\n" + "Two echo statements, one spans two lines" + (phps-mode-test-lex-analyzer--alternative-indentation-whole-buffer) + (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) + (should (equal + buffer-contents + "<?php\necho '<dl><dt>' . __('Data', 'something')\n . ':</dt><dd><pre>' . print_r($decodedData, true) . '</pre></dd></dl>';\necho '<div class=\"meta actions\">';\n " + )))) + + (phps-mode-test--with-buffer + "<?php\nif ($shippingMethod->id ===\n \\MyClass::METHOD_ID\n ) {\n" + "Multi-linte if statement testing equality in two lines" + (phps-mode-test-lex-analyzer--alternative-indentation-whole-buffer) + (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) + (should (equal + buffer-contents + "<?php\nif ($shippingMethod->id ===\n \\MyClass::METHOD_ID\n) {\n " + )))) + ) (defun phps-mode-test-lex-analyzer--move-lines-indent ()