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

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

[elpa] externals/phps-mode 811c499 220/405: New algorithm now works for


From: Stefan Monnier
Subject: [elpa] externals/phps-mode 811c499 220/405: New algorithm now works for switch, case syntax
Date: Sat, 13 Jul 2019 10:00:17 -0400 (EDT)

branch: externals/phps-mode
commit 811c4998425aedf9b9cd08c13808fd0f13624d40
Author: Christian Johansson <address@hidden>
Commit: Christian Johansson <address@hidden>

    New algorithm now works for switch, case syntax
---
 phps-mode-functions.el      | 57 ++++++++++++++++++++++++++++++++++++---------
 phps-mode-test-functions.el |  9 +++++--
 2 files changed, 53 insertions(+), 13 deletions(-)

diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index d954e93..7555c93 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -60,6 +60,8 @@
               (after-special-control-structure-first-on-line nil)
               (after-extra-special-control-structure nil)
               (after-extra-special-control-structure-first-on-line nil)
+              (switch-curly-stack nil)
+              (switch-alternative-stack nil)
               (switch-case-alternative-stack nil)
               (curly-bracket-level 0)
               (round-bracket-level 0)
@@ -178,17 +180,26 @@
                   (setq line-contained-nesting-decrease t)
                   (setq curly-bracket-level (1- curly-bracket-level))
 
-                  ;; Keep track of in scripting
-                  (when (or (equal token 'T_OPEN_TAG)
-                            (equal token 'T_OPEN_TAG_WITH_ECHO))
-                    (setq in-scripting t))
-                  (when (equal token 'T_CLOSE_TAG)
-                    (setq in-scripting nil))
+                  (when (and switch-curly-stack
+                             (= (1+ curly-bracket-level) (car 
switch-curly-stack)))
 
+                    (when phps-mode-functions-verbose
+                      (message "Ended switch curly stack at %s" 
curly-bracket-level))
 
+                    (setq allow-custom-column-decrement t)
+                    (setq alternative-control-structure-level (1- 
alternative-control-structure-level))
+                    (pop switch-curly-stack))
+                  
                   (when first-token-on-line
                     (setq first-token-is-nesting-decrease t)))
 
+                ;; Keep track of in scripting
+                (when (or (equal token 'T_OPEN_TAG)
+                          (equal token 'T_OPEN_TAG_WITH_ECHO))
+                  (setq in-scripting t))
+                (when (equal token 'T_CLOSE_TAG)
+                  (setq in-scripting nil))
+
                 ;; Keep track of ending alternative control structure level
                 (when (or (equal token 'T_ENDIF)
                           (equal token 'T_ENDWHILE)
@@ -200,7 +211,14 @@
 
                   (when (and (equal token 'T_ENDSWITCH)
                              switch-case-alternative-stack)
-                    (pop switch-case-alternative-stack))
+
+                    (when phps-mode-functions-verbose
+                      (message "Ended alternative switch stack at %s" 
alternative-control-structure-level))
+                    
+                    (pop switch-alternative-stack)
+                    (pop switch-case-alternative-stack)
+                    (setq allow-custom-column-decrement t)
+                    (setq alternative-control-structure-level (1- 
alternative-control-structure-level)))
 
                   (when first-token-on-line
                     (setq first-token-is-nesting-decrease t)))
@@ -216,13 +234,29 @@
                       (setq after-special-control-structure-token token)
 
                     ;; Is token not a curly bracket - because that is a 
ordinary control structure syntax
-                    (unless (string= token "{")
+                    (if (string= token "{")
+
+                        ;; Save curly bracket level when switch starts
+                        (when (equal after-special-control-structure-token 
'T_SWITCH)
+
+                          (when phps-mode-functions-verbose
+                            (message "Started switch curly stack at %s" 
curly-bracket-level))
+
+                          (push curly-bracket-level switch-curly-stack))
 
                       ;; Is it the start of an alternative control structure?
                       (if (string= token ":")
 
                           (progn
 
+                            ;; Save alternative nesting level for switch
+                            (when (equal after-special-control-structure-token 
'T_SWITCH)
+
+                              (when phps-mode-functions-verbose
+                                (message "Started switch alternative stack at 
%s" alternative-control-structure-level))
+
+                              (push alternative-control-structure-level 
switch-alternative-stack))
+
                             (setq alternative-control-structure-level (1+ 
alternative-control-structure-level))
 
                             (when phps-mode-functions-verbose
@@ -312,11 +346,12 @@
                   (setq after-extra-special-control-structure t)
                   (setq after-extra-special-control-structure-first-on-line 
first-token-on-line)
 
-                  (when phps-mode-functions-verbose
-                    (message "Found CASE %s vs %s" (1- 
alternative-control-structure-level) (car switch-case-alternative-stack)))
-
                   (when (and switch-case-alternative-stack
                              (= (1- alternative-control-structure-level) (car 
switch-case-alternative-stack)))
+
+                    (when phps-mode-functions-verbose
+                      (message "Found CASE %s vs %s" (1- 
alternative-control-structure-level) (car switch-case-alternative-stack)))
+
                     (setq alternative-control-structure-level (1- 
alternative-control-structure-level))
                     (when first-token-on-line
                       (setq first-token-is-nesting-decrease t))
diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el
index e765760..c59ffcb 100644
--- a/phps-mode-test-functions.el
+++ b/phps-mode-test-functions.el
@@ -294,12 +294,17 @@
 (defun phps-mode-test-functions-get-lines-indent-switch-case ()
   "Test for switch-case indentation."
 
-    (phps-mode-test-with-buffer
+  (phps-mode-test-with-buffer
    "<?php\nswitch ($condition) {\n    case true:\n        echo 'here';\n       
 echo 'here 2';\n    case false:\n        echo 'here 4';\n    default:\n        
echo 'here 3';\n}\n"
    "Switch, case, default"
    (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (2 0)) (5 (2 0)) (6 (1 
0)) (7 (2 0)) (8 (1 0)) (9 (2 0)) (10 (0 0))) 
(phps-mode-test-functions--hash-to-list 
(phps-mode-functions-get-lines-indent)))))
 
-)
+  (phps-mode-test-with-buffer
+   "<?php\nswitch ($condition):\n    case true:\n        echo 'here';\n        
echo 'here 2';\n    case false:\n        echo 'here 4';\n    default:\n        
echo 'here 3';\nendswitch;\n"
+   "Switch, case, default with alternative control structure"
+   (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (2 0)) (5 (2 0)) (6 (1 
0)) (7 (2 0)) (8 (1 0)) (9 (2 0)) (10 (0 0))) 
(phps-mode-test-functions--hash-to-list 
(phps-mode-functions-get-lines-indent)))))
+
+  )
 
 (defun phps-mode-test-functions-indent-line ()
   "Test for indentation."



reply via email to

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