emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/phps-mode 8c1f529f87 145/212: Passing indent for line a


From: Christian Johansson
Subject: [elpa] externals/phps-mode 8c1f529f87 145/212: Passing indent for line after ending of statement / expression with trailing closing bracket
Date: Wed, 26 Jan 2022 01:51:16 -0500 (EST)

branch: externals/phps-mode
commit 8c1f529f87505300315f201550aa8ada77d719c0
Author: Christian Johansson <christian@cvj.se>
Commit: Christian Johansson <christian@cvj.se>

    Passing indent for line after ending of statement / expression with 
trailing closing bracket
---
 phps-mode-indent.el           | 113 +++++++++++++++++++++++++++++++++++++++---
 test/phps-mode-test-indent.el |   2 +-
 2 files changed, 106 insertions(+), 9 deletions(-)

diff --git a/phps-mode-indent.el b/phps-mode-indent.el
index e5db7504cf..84c4574e39 100644
--- a/phps-mode-indent.el
+++ b/phps-mode-indent.el
@@ -243,6 +243,7 @@
                new-indentation
                previous-indentation)
 
+              ;; debug stuff
               ;; (message "\ncurrent-line-string: %S" current-line-string)
               ;; (message "previous-line-string: %S" previous-line-string)
               ;; (message "current-line-starts-with-closing-bracket: %S" 
current-line-starts-with-closing-bracket)
@@ -1059,6 +1060,110 @@
 
                   ))
 
+               ;; if (true) {
+               ;;     $cacheKey = sprintf(
+               ;;         'key_%s',
+               ;;         md5(json_encode($key))
+               ;;     );
+               ;;     $cache =
+               ;; or
+               ;; if (true) {
+               ;;     $cache =
+               ;;         Cache::getInstance();
+               ;;     echo 'here';
+               ((string-match-p
+                 "[])][\t ]*;[\t ]*\\(\\?>[\t\n ]*\\)?$"
+                 previous-line-string)
+
+                ;; Backtrack first to line were bracket started
+                ;; and then backwards until the line were statement / 
expression
+                ;; started and use indentation from that line from that line
+                (forward-line (* -1 move-length1))
+                (end-of-line)
+                (search-backward-regexp ";" nil t) ;; Skip trailing comma
+                (let ((not-found-bracket-start t)
+                      (reference-line)
+                      (parenthesis-level 0))
+                  (while
+                      (and
+                       not-found-bracket-start
+                       (search-backward-regexp
+                        "[][()]"
+                        nil
+                        t))
+                    (let ((match (match-string-no-properties 0)))
+                      (cond
+
+                       ((or
+                         (string= "(" match)
+                         (string= "[" match))
+                        (setq
+                         parenthesis-level
+                         (1+ parenthesis-level))
+                        (when (= parenthesis-level 0)
+                          (setq
+                           not-found-bracket-start
+                           nil)))
+
+                       ((or
+                         (string= ")" match)
+                         (string= "]" match))
+                        (setq
+                         parenthesis-level
+                         (1- parenthesis-level))
+                        (when (= parenthesis-level 0)
+                          (setq
+                           not-found-bracket-start
+                           nil)))
+
+                       )))
+
+                  ;; Found line were bracket started?
+                  (unless not-found-bracket-start
+                    (setq
+                     reference-line
+                     (buffer-substring-no-properties
+                      (line-beginning-position)
+                      (line-end-position)))
+
+                    ;; Search for first line of statement / expression here
+                    (let ((not-found-command-start t))
+                      (while
+                          (and
+                           not-found-command-start
+                           (search-backward-regexp
+                            "\\(;\\|}\\|{\\|[\t ]*[^\t ]+[\t ]*$\\)"
+                            nil
+                            t))
+                        (let ((match (match-string-no-properties 0)))
+                          (cond
+
+                           ;; End of expression / statement
+                           ((or
+                             (string= ";" match)
+                             (string= "}" match)
+                             (string= "{" match))
+                            (setq
+                             not-found-command-start
+                             nil))
+
+                           ;; Non-empty line
+                           (t
+                            (setq
+                             reference-line
+                             (buffer-substring-no-properties
+                              (line-beginning-position)
+                              (line-end-position))))
+
+                           )))))
+
+                  (when reference-line
+                    (setq
+                     new-indentation
+                     (phps-mode-indent--string-indentation
+                      reference-line))))
+
+                (goto-char point))
 
                ;; $var .=
                ;;     'hello';
@@ -1088,14 +1193,6 @@
                ;; or
                ;; define('_PRIVATE_ROOT_',
                ;;     'here');
-
-               ;; TODO Handle cases like
-               ;; if (true) {
-               ;;     $cacheKey = sprintf(
-               ;;         'key_%s',
-               ;;         md5(json_encode($key))
-               ;;     );
-               ;;     $cache =
                ((and
                  previous-line-ends-with-terminus
                  (string= previous-line-ends-with-terminus ";")
diff --git a/test/phps-mode-test-indent.el b/test/phps-mode-test-indent.el
index e3ab3b1e81..c12b9166b7 100644
--- a/test/phps-mode-test-indent.el
+++ b/test/phps-mode-test-indent.el
@@ -329,7 +329,7 @@
      "Comment after assignment from method call on same line")
 
     (phps-mode-test-indent--should-equal
-     "<?php\nif (true) {\n    // My comment\n    $cacheKey = sprintf(\n        
'key_%s',\n        md5(json_encode($key))\n    );\n    $cache =\n        
Cache::getInstance();\n"
+     "<?php\nif (true) {\n    // My comment\n    $cacheKey = sprintf(\n        
'key_%s',\n        md5(json_encode($key))\n    );\n    $cache =\n        
Cache::getInstance();\n    "
      "Line after assignment from multi-line function-call")
 
   )



reply via email to

[Prev in Thread] Current Thread [Next in Thread]