branch: externals/phps-mode
commit 806bb902b3c053fda3c9aac846191a43a1a1fb58
Author: Christian Johansson <[email protected]>
Commit: Christian Johansson <[email protected]>
Improved indentation for chaining objects
---
phps-mode-indent.el | 116 ++++++++++++++++++++++++++++++++++++++++--
test/phps-mode-test-indent.el | 2 +-
2 files changed, 112 insertions(+), 6 deletions(-)
diff --git a/phps-mode-indent.el b/phps-mode-indent.el
index ee117ead07..7b7318eb69 100644
--- a/phps-mode-indent.el
+++ b/phps-mode-indent.el
@@ -762,12 +762,14 @@
(parenthesis-level 0)
(is-bracket-less-command nil)
(is-same-line-p t)
+ (is-object-chaining)
+ (is-object-chaining-on-same-line)
(bracket-opened-on-first-line))
(while
(and
not-found
(search-backward-regexp
- "\\(;\\|{\\|(\\|)\\|=\\|echo[\t ]+\\|print[\t
]+\\|\n\\|<<<'?\"?[a-zA-Z0-9]+'?\"?\\)"
+ "\\(;\\|{\\|(\\|)\\|=\\|echo[\t ]+\\|print[\t
]+\\|\n\\|<<<'?\"?[a-zA-Z0-9]+'?\"?\\|->\\)"
nil
t))
(let ((match (match-string-no-properties 0)))
@@ -791,6 +793,14 @@
(setq
parenthesis-level
(1- parenthesis-level)))
+ ((string= match "->")
+ (when (= parenthesis-level 0)
+ (setq
+ is-object-chaining
+ t)
+ (setq
+ is-object-chaining-on-same-line
+ is-same-line-p)))
((= parenthesis-level 0)
(setq is-assignment (string= match "="))
(setq is-bracket-less-command
@@ -799,11 +809,13 @@
match))
(setq not-found nil)))))
- (when (and
- (not is-same-line-p)
+ (when (or
(and
- is-assignment
- (not bracket-opened-on-first-line)))
+ (not is-same-line-p)
+ is-assignment)
+ (and
+ (not is-object-chaining-on-same-line)
+ is-object-chaining))
(setq
new-indentation
(- new-indentation tab-width)))
@@ -980,6 +992,100 @@
new-indentation
(+ new-indentation tab-width)))
+ ;; $myObject->myFunction()
+ ;; ->myFunction2()
+ ((string-match-p
+ "->"
+ previous-line-string)
+ (let ((not-found t)
+ (started-chaining-on-this-line t)
+ (is-assignment)
+ (is-string-concatenation)
+ (parenthesis-level 0)
+ (is-bracket-less-command)
+ (is-same-line-p t)
+ (bracket-opened-on-first-line))
+ (while
+ (and
+ not-found
+ (search-backward-regexp
+ "\\(;\\|{\\|(\\|)\\|=\\|->\\|echo[\t ]+\\|print[\t
]+\\|\n\\|^[\t ]*\\.\\|\\.[\t ]*$\\)"
+ nil
+ t))
+ (let ((match (match-string-no-properties 0)))
+ (cond
+
+ ((string=
+ "->"
+ match)
+ (setq
+ started-chaining-on-this-line
+ is-same-line-p))
+
+ ((string=
+ "\n"
+ match)
+ (setq
+ is-same-line-p
+ nil))
+
+ ((or
+ (string=
+ "echo"
+ match)
+ (string=
+ "print"
+ match))
+ (setq
+ is-bracket-less-command
+ t)
+ (setq
+ not-found
+ nil))
+
+ ((or
+ (string=
+ ";"
+ match)
+ (string=
+ "}"
+ match))
+ (setq
+ not-found
+ nil))
+
+ ((string=
+ "="
+ match)
+ (setq
+ is-assignment
+ t)
+ (setq
+ not-found
+ nil))
+
+ ((string-match-p
+ "\\(^[\t ]*\\.\\|\\.[\t ]*\\)$"
+ match)
+ (setq
+ is-string-concatenation
+ t)
+ (setq
+ not-found
+ nil))
+
+ )))
+
+ (when (and
+ (not is-assignment)
+ (not is-string-concatenation)
+ (not started-chaining-on-this-line)
+ (not is-bracket-less-command))
+ (setq
+ new-indentation
+ (+ new-indentation tab-width))))
+ (goto-char point))
+
;; /**
;; *
;; */
diff --git a/test/phps-mode-test-indent.el b/test/phps-mode-test-indent.el
index 010b841b6f..f74feaa133 100644
--- a/test/phps-mode-test-indent.el
+++ b/test/phps-mode-test-indent.el
@@ -198,7 +198,7 @@
"Indentation of chained class method calls outside of assignments and
conditionals")
(phps-mode-test-indent--should-equal
- "<?php\n\n$myVar = $myClass->meMethod()\n ->mySecondMethod()\n
->myThirdMethod()\n->myFourthFunction(\n $myVariable\n);"
+ "<?php\n\n$myVar = $myClass->meMethod()\n ->mySecondMethod()\n
->myThirdMethod()\n ->myFourthFunction(\n $myVariable\n );"
"Indentation for chained object operators in assignment with method call
with arguments")
(phps-mode-test-indent--should-equal