branch: externals/phps-mode
commit 837412c0e5423b4ceb38a8630b37e2d2f485e035
Author: Christian Johansson <christ...@cvj.se>
Commit: Christian Johansson <christ...@cvj.se>

    Improved indentation on lines starting with a closing bracket
---
 phps-mode-indent.el           | 81 ++++++++++++++++++++++++++++++++++---------
 test/phps-mode-test-indent.el |  4 +--
 2 files changed, 67 insertions(+), 18 deletions(-)

diff --git a/phps-mode-indent.el b/phps-mode-indent.el
index c8a4d1db39..fb6b0090f0 100644
--- a/phps-mode-indent.el
+++ b/phps-mode-indent.el
@@ -1361,30 +1361,79 @@
                ;; or
                ;; array(
                ;; )
+               ;; or
+               ;; if (
+               ;;     myFunction(
+               ;;         'random')
+               ;; ) {
                ;; but ignore
                ;; var_dump(array(<<<EOD
                ;; ölöas
                ;; EOD
                ;; ));
-               ;; TODO Should backtrack to were bracket started
-               ;; TODO and use indentation from that line
                ((and
                  current-line-starts-with-closing-bracket
                  (not previous-line-ends-with-opening-bracket))
-                (setq
-                 new-indentation
-                 (- new-indentation tab-width))
-
-                ;; if (
-                ;;     myFunction(
-                ;;         'random')
-                ;; ) {
-                (when (and
-                       previous-line-ends-with-closing-bracket
-                       (not previous-line-starts-with-closing-bracket))
-                  (setq
-                   new-indentation
-                   (- new-indentation tab-width))))
+                ;; Backtrack to line were bracket started
+                ;; and use indentation from that line for this line
+                (forward-line (* -1 move-length1))
+                (end-of-line)
+                (let ((not-found t)
+                      (reference-line)
+                      (reference-indentation)
+                      (parenthesis-level -1))
+                  (while
+                      (and
+                       not-found
+                       (search-backward-regexp
+                        "[][(){}]"
+                        nil
+                        t))
+                    (let ((match (match-string-no-properties 0)))
+                      (cond
+
+                       ((or
+                         (string= "(" match)
+                         (string= "[" match)
+                         (string= "{" match))
+                        (setq
+                         parenthesis-level
+                         (1+ parenthesis-level))
+                        (when (= parenthesis-level 0)
+                          (setq
+                           not-found
+                           nil)))
+
+                       ((or
+                         (string= ")" match)
+                         (string= "]" match)
+                         (string= "}" match))
+                        (setq
+                         parenthesis-level
+                         (1- parenthesis-level))
+                        (when (= parenthesis-level 0)
+                          (setq
+                           not-found
+                           nil)))
+
+                       )))
+                  (unless not-found
+                    (setq
+                     reference-line
+                     (buffer-substring-no-properties
+                      (line-beginning-position)
+                      (line-end-position)))
+                    (setq
+                     reference-indentation
+                     (phps-mode-indent--string-indentation
+                      reference-line)))
+
+                  (goto-char point)
+
+                  (when reference-indentation
+                    (setq
+                     new-indentation
+                     reference-indentation))))
 
                ;; /**
                ;;  * here
diff --git a/test/phps-mode-test-indent.el b/test/phps-mode-test-indent.el
index 83a6f473ae..11964aca5f 100644
--- a/test/phps-mode-test-indent.el
+++ b/test/phps-mode-test-indent.el
@@ -305,11 +305,11 @@
      "Mutiline define statement")
 
     (phps-mode-test-indent--should-equal
-     "<?php\nif ($useRuntimeCache\n    && self::isWritingnabled()\n    && 
\\Aomebo\\Cache\\System::cacheExists(\n        $cacheParameters,\n        
$cacheKey,\n        
\\Aomebo\\Cache\\System::CACHE_STORAGE_LOCATION_FILESYSTEM)\n) {\n"
+     "<?php\nif ($useRuntimeCache\n    && self::isWritingnabled()\n    && 
\\Aomebo\\Cache\\System::cacheExists(\n        $cacheParameters,\n        
$cacheKey,\n        
\\Aomebo\\Cache\\System::CACHE_STORAGE_LOCATION_FILESYSTEM)\n) {    "
      "Multiline if condition")
 
     (phps-mode-test-indent--should-equal
-     "<?php\nif ($data = \\Aomebo\\Cache\\System::loadCache(\n    
$cacheParameters,\n    $cacheKey,\n    
\\Aomebo\\Cache\\System::FORMAT_SERIALIZE,\n    
\\Aomebo\\Cache\\System::CACHE_STORAGE_LOCATION_FILESYSTEM)\n) {\n"
+     "<?php\nif ($data = \\Aomebo\\Cache\\System::loadCache(\n    
$cacheParameters,\n    $cacheKey,\n    
\\Aomebo\\Cache\\System::FORMAT_SERIALIZE,\n    
\\Aomebo\\Cache\\System::CACHE_STORAGE_LOCATION_FILESYSTEM)\n) {    "
      "Multiline if-condition with assignment")
 
     (phps-mode-test-indent--should-equal

Reply via email to