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

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

[elpa] externals/phps-mode f619fe6911 07/15: Started on helper function


From: Christian Johansson
Subject: [elpa] externals/phps-mode f619fe6911 07/15: Started on helper function for calculating indentation for multi-line chaining expressions
Date: Fri, 4 Mar 2022 14:13:03 -0500 (EST)

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

    Started on helper function for calculating indentation for multi-line 
chaining expressions
---
 phps-mode-indent.el           | 198 +++++++++++++++++-------------------------
 test/phps-mode-test-indent.el |  48 ++++++++++
 2 files changed, 129 insertions(+), 117 deletions(-)

diff --git a/phps-mode-indent.el b/phps-mode-indent.el
index e6099e5da9..44821bb110 100644
--- a/phps-mode-indent.el
+++ b/phps-mode-indent.el
@@ -366,6 +366,80 @@
             nil
           reference-line)))))
 
+(defun phps-mode-indent--get-previous-start-of-chaining ()
+  "Get previous start of bracket line as reference, if any exist."
+  (let ((reference-line))
+    (save-excursion
+      (end-of-line)
+      (let ((not-found-bracket-start t)
+            (parenthesis-level 0)
+            (same-line-p t)
+            (found-chain-on-this-line))
+        (while
+            (and
+             not-found-bracket-start
+             (search-backward-regexp
+              "\\([][{}()=\n]\\|->\\)"
+              nil
+              t))
+          (let ((match (match-string-no-properties 0)))
+            ;; (message "match: %S" match)
+            (cond
+
+             ((string= "\n" match)
+              (if found-chain-on-this-line
+                  (progn
+                    (setq
+                     reference-line
+                     found-chain-on-this-line)
+                    (setq
+                     found-chain-on-this-line
+                     nil))
+                (setq
+                 reference-line
+                 nil))
+              (setq
+               same-line-p
+               nil))
+
+             ((string= "->" match)
+              (setq
+               found-chain-on-this-line
+               (buffer-substring-no-properties
+                (line-beginning-position)
+                (line-end-position))))
+
+             ((string= "=" match)
+              (setq
+               not-found-bracket-start
+               nil))
+
+             ((or
+               (string= "(" match)
+               (string= "[" match)
+               (string= "{" match))
+              (setq
+               parenthesis-level
+               (1+ parenthesis-level))
+              (when (= parenthesis-level 1)
+                (setq
+                 not-found-bracket-start
+                 nil)))
+
+             ((or
+               (string= ")" match)
+               (string= "]" match)
+               (string= "}" match))
+              (setq
+               parenthesis-level
+               (1- parenthesis-level)))
+
+             )))
+        (message "reference-line: %S" reference-line)
+        (if same-line-p
+            nil
+          reference-line)))))
+
 (defun phps-mode-indent--get-previous-reference-command-line ()
   "Get previous line that is a command (if any)."
   (let ((not-found t)
@@ -901,125 +975,15 @@
                 (setq
                  match-type
                  'line-continuing-object-operators)
-                (let ((not-found t)
-                      (started-chaining-on-this-line t)
-                      (is-string-concatenation)
-                      (is-bracket-less-command)
-                      (is-same-line-p t)
-                      (chaining-line)
-                      (reference-line)
-                      (chaining-was-this-line-p))
-                  (while
-                      (and
-                       not-found
-                       (search-backward-regexp
-                        "\\(;\\|{\\|(\\|)\\|=\\|->\\|echo[\t ]+\\|print[\t 
]+\\|\n\\|^[\t ]*\\.\\|\\.[\t ]*$\\)"
-                        nil
-                        t))
-                    (let ((match (match-string-no-properties 0)))
-                      (message "match: %S" match)
-                      (cond
-
-                       ((string=
-                         "->"
-                         match)
-                        (setq
-                         chaining-was-this-line-p
-                         t)
-                        (setq
-                         chaining-line
-                         (buffer-substring-no-properties
-                          (line-beginning-position)
-                          (line-end-position)))
-                        (setq
-                         started-chaining-on-this-line
-                         is-same-line-p))
-
-                       ;; TODO Make helper function that determines chaining 
indentation reference line
-
-                       ((string=
-                         "\n"
-                         match)
-                        (if chaining-was-this-line-p
-                            (progn
-                              (setq
-                               chaining-was-this-line-p
-                               nil)
-                              (setq
-                               reference-line
-                               chaining-line))
-                          (setq
-                           reference-line
-                           nil))
-                        (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)
-                         (string=
-                          "{"
-                          match))
-                        (setq
-                         reference-line
-                         nil)
-                        (setq
-                         not-found
-                         nil))
-
-                       ((string=
-                         "="
-                         match)
-                        (setq
-                         not-found
-                         nil))
-
-                       ((string-match-p
-                         "\\(^[\t ]*\\.\\|\\.[\t ]*\\)$"
-                         match)
-                        (setq
-                         is-string-concatenation
-                         t)
-                        (setq
-                         not-found
-                         nil))
-
-                       )))
-
-                  (when (and
-                         (not is-string-concatenation)
-                         (not started-chaining-on-this-line)
-                         (not is-bracket-less-command))
-                    (if reference-line
-                        (progn
-                          (phps-mode-debug-message
-                           (message "reference-line: %S" reference-line))
-                          (setq
-                           new-indentation
-                           (phps-mode-indent--string-indentation
-                            reference-line)))
+                (let ((reference-line 
(phps-mode-indent--get-previous-start-of-chaining)))
+                  (if reference-line
                       (setq
                        new-indentation
-                       (+ new-indentation tab-width))))))
+                       (phps-mode-indent--string-indentation
+                        reference-line))
+                    (setq
+                     new-indentation
+                     (+ new-indentation tab-width)))))
 
                ;; LINE AFTER OPENING HEREDOC/NOWDOC
                ;; echo <<<VAR
diff --git a/test/phps-mode-test-indent.el b/test/phps-mode-test-indent.el
index 1cdc99da9d..8f86925124 100644
--- a/test/phps-mode-test-indent.el
+++ b/test/phps-mode-test-indent.el
@@ -213,6 +213,54 @@
       (phps-mode-indent--get-previous-start-of-bracket-line t)
       nil)))
 
+  (with-temp-buffer
+    (insert "<?php\nif (true) {\n    $table = $installer->getConnection()\n    
    ->newTable($installer->getTable('my_table'))\n        ->addColumn();\n}\n")
+    (goto-char 127)
+    (should
+     (equal
+      (phps-mode-indent--get-previous-start-of-chaining)
+      "        ->newTable($installer->getTable('my_table'))")))
+
+  (with-temp-buffer
+    (insert "<?php\nif (true) {\n    $table = $installer->getConnection()\n    
    ->newTable($installer->getTable('my_table'))\n        ->addColumn();\n}\n")
+    (goto-char 73)
+    (should
+     (equal
+      (phps-mode-indent--get-previous-start-of-chaining)
+      "        ->newTable($installer->getTable('my_table'))")))
+
+  (with-temp-buffer
+    (insert "<?php\nif (true) {\n    $criteria = 
$this->searchCriteriaBuilder\n        ->addFilter('status', $status)\n        
->addFilter(method', 'my_method_' . $object->getId())\n        
->create();\n}\n")
+    (goto-char 177)
+    (should
+     (equal
+      (phps-mode-indent--get-previous-start-of-chaining)
+      "        ->addFilter('status', $status)")))
+
+  (with-temp-buffer
+    (insert "<?php\nif (true) {\n    $criteria = 
$this->searchCriteriaBuilder\n        ->addFilter('status', $status)\n        
->addFilter(method', 'my_method_' . $object->getId())\n        
->create();\n}\n")
+    (goto-char 135)
+    (should
+     (equal
+      (phps-mode-indent--get-previous-start-of-chaining)
+      "        ->addFilter('status', $status)")))
+
+  (with-temp-buffer
+    (insert "<?php\nif (true) {\n    $criteria = 
$this->searchCriteriaBuilder\n        ->addFilter('status', $status)\n        
->addFilter(method', 'my_method_' . $object->getId())\n        
->create();\n}\n")
+    (goto-char 96)
+    (should
+     (equal
+      (phps-mode-indent--get-previous-start-of-chaining)
+      "        ->addFilter('status', $status)")))
+
+  (with-temp-buffer
+    (insert "<?php\nif (true) {\n    $criteria = 
$this->searchCriteriaBuilder\n        ->addFilter('status', $status)\n        
->addFilter(method', 'my_method_' . $object->getId())\n        
->create();\n}\n")
+    (goto-char 51)
+    (should
+     (equal
+      (phps-mode-indent--get-previous-start-of-chaining)
+      nil)))
+
   (should
    (equal
     (phps-mode-indent--get-html-string-bracket-level "<!DOCTYPE html>")



reply via email to

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