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

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

[elpa] externals/phps-mode b2eac4d 154/405: More work on switch case def


From: Stefan Monnier
Subject: [elpa] externals/phps-mode b2eac4d 154/405: More work on switch case default
Date: Sat, 13 Jul 2019 10:00:03 -0400 (EDT)

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

    More work on switch case default
---
 phps-mode-functions.el      | 47 +++++++++++++++++++++++++++++++++------------
 phps-mode-lexer.el          |  4 +++-
 phps-mode-test-functions.el | 30 ++++++++++++++++++++++-------
 3 files changed, 61 insertions(+), 20 deletions(-)

diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index 4d856ed..a9c1b70 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -46,7 +46,7 @@
   "Get the column and tuning indentation-numbers for each line in buffer that 
contain tokens."
   (if (boundp 'phps-mode-lexer-tokens)
       (save-excursion
-        (beginning-of-buffer)
+        (goto-char (point-min))
         (let ((in-scripting nil)
               (in-heredoc nil)
               (in-doc-comment nil)
@@ -55,6 +55,8 @@
               (after-special-control-structure-token nil)
               (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)
               (curly-bracket-level 0)
               (round-bracket-level 0)
               (square-bracket-level 0)
@@ -171,6 +173,13 @@
                   (setq first-token-is-nesting-increase t)))
               (when (string= token "}")
                 (setq curly-bracket-level (1- curly-bracket-level))
+
+                ;; Decrease switch curly stack if any
+                (when (and switch-curly-stack
+                           (= curly-bracket-level (car switch-curly-stack)))
+                  (setq curly-bracket-level (1- curly-bracket-level))
+                  (pop switch-curly-stack))
+
                 (when first-token-on-line
                   (setq first-token-is-nesting-decrease t)))
 
@@ -181,7 +190,11 @@
                         (equal token 'T_ENDFOREACH)
                         (equal token 'T_ENDSWITCH))
                 (setq alternative-control-structure-level (1- 
alternative-control-structure-level))
-                (message "Found ending alternative token %s %s" token 
alternative-control-structure-level)
+                ;; (message "Found ending alternative token %s %s" token 
alternative-control-structure-level)
+
+                (when (equal token 'T_ENDSWITCH)
+                  (setq alternative-control-structure-level (1- 
alternative-control-structure-level)))
+
                 (when first-token-on-line
                   (setq first-token-is-nesting-decrease t)))
 
@@ -193,16 +206,26 @@
                          (not (string= token "(")))
 
                 ;; Is token not a curly bracket - because that is a ordinary 
control structure syntax
-                (unless (string= token "{")
+                (if (string= token "{")
+
+                    (when (equal after-special-control-structure-token 
'T_SWITCH)
+                      (setq curly-bracket-level (1+ curly-bracket-level))
+                      (message "Opening switch, increase curly brackets to %s" 
curly-bracket-level)
+                      (push curly-bracket-level switch-curly-stack))
 
                   ;; Is it the start of an alternative control structure?
                   (if (string= token ":")
                       (progn
                         (if (or (equal after-special-control-structure-token 
'T_ELSE)
-                                (equal after-special-control-structure-token 
'T_ELSEIF))
+                                (equal after-special-control-structure-token 
'T_ELSEIF)
+                                (equal after-special-control-structure-token 
'T_DEFAULT))
                             (progn
                               (when 
after-special-control-structure-first-on-line
                                 (setq first-token-is-nesting-decrease t)))
+
+                          (when (equal after-special-control-structure-token 
'T_SWITCH)
+                            (setq alternative-control-structure-level (1+ 
alternative-control-structure-level)))
+
                           (setq alternative-control-structure-level (1+ 
alternative-control-structure-level))
                           (when after-special-control-structure-first-on-line
                             (setq first-token-is-nesting-increase t))))
@@ -219,12 +242,11 @@
 
                 (setq after-special-control-structure nil))
 
-              ;; Support extra special control structures (CASE and DEFAULT)
+              ;; Support extra special control structures (CASE)
               (when (and after-extra-special-control-structure
                          (string= token ":"))
-                (setq alternative-control-structure-level (1+ 
alternative-control-structure-level))
-                (when first-token-on-line
-                  (setq first-token-is-nesting-increase t))
+                (when after-extra-special-control-structure-first-on-line
+                  (setq first-token-is-nesting-decrease t))
                 (setq after-extra-special-control-structure nil))
 
               ;; Did we reach a semicolon inside a inline block? Close the 
inline block
@@ -240,15 +262,16 @@
                         (equal token 'T_FOREACH)
                         (equal token 'T_SWITCH)
                         (equal token 'T_ELSE)
-                        (equal token 'T_ELSEIF))
+                        (equal token 'T_ELSEIF)
+                        (equal token 'T_DEFAULT))
                 (setq after-special-control-structure-first-on-line 
first-token-on-line)
                 (setq after-special-control-structure round-bracket-level)
                 (setq after-special-control-structure-token token))
 
               ;; Did we encounter a token that supports extra special 
alternative control structures?
-              (when (or (equal token 'T_CASE)
-                        (equal token 'T_DEFAULT))
-                (setq after-extra-special-control-structure t))
+              (when (equal token 'T_CASE)
+                (setq after-extra-special-control-structure t)
+                (setq after-extra-special-control-structure-first-on-line 
first-token-on-line))
 
               ;; Keep track of in scripting
               (when (or (equal token 'T_OPEN_TAG)
diff --git a/phps-mode-lexer.el b/phps-mode-lexer.el
index bffc0b5..e1ea834 100644
--- a/phps-mode-lexer.el
+++ b/phps-mode-lexer.el
@@ -1265,7 +1265,9 @@ ANY_CHAR'
       (setq phps-mode-lexer-buffer-changes--start nil))
 
     (setq phps-mode-lexer-states nil)
-    
+    (when (and (boundp 'phps-mode-functions-lines-indent)
+               phps-mode-functions-lines-indent)
+      (setq phps-mode-functions-lines-indent nil))
     (phps-mode-lexer-BEGIN phps-mode-lexer-ST_INITIAL)))
 
 (defun phps-mode-lexer-run ()
diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el
index f41a0e6..660f6e6 100644
--- a/phps-mode-test-functions.el
+++ b/phps-mode-test-functions.el
@@ -73,7 +73,10 @@
    "Round and square bracket expressions"
    (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (1 0)) (5 (2 0)) (6 (1 
0)) (7 (0 0)) (8 (0 0))) (phps-mode-test-functions--hash-to-list 
(phps-mode-functions-get-lines-indent)))))
 
-  ;; TODO CASE, DEFAULT
+  (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)))))
 
   ;; TODO NOWDOC, HEREDOC
 
@@ -89,7 +92,7 @@
   ;; Curly bracket tests
   (phps-mode-test-with-buffer
    "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) 
{\necho $title;\n\n} ?></title><body>Bla bla</body></html>"
-   nil
+   "Curly bracket test"
    (goto-char 69)
    (phps-mode-functions-indent-line)
    (let ((buffer-contents (buffer-substring-no-properties (point-min) 
(point-max))))
@@ -97,6 +100,7 @@
 
   (phps-mode-test-with-buffer
    "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) 
{\necho $title1;\n} ?></title><body>Bla bla</body></html>"
+   "Curly bracket test 2"
    (goto-char 75)
    (phps-mode-functions-indent-line)
    (let ((buffer-contents (buffer-substring-no-properties (point-min) 
(point-max))))
@@ -104,7 +108,7 @@
 
   (phps-mode-test-with-buffer
    "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) 
{\necho $title2;\n\n} ?></title><body>Bla bla</body></html>"
-   nil
+   "Curly bracket test 3"
    (goto-char 98)
    (phps-mode-functions-indent-line)
    (let ((buffer-contents (buffer-substring-no-properties (point-min) 
(point-max))))
@@ -112,6 +116,7 @@
 
   (phps-mode-test-with-buffer
    "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) 
{\necho $title3;\n\n}\n?>\n</title><body>Bla bla</body></html>"
+   "Curly bracket test 4"
    (goto-char 110)
    (phps-mode-functions-indent-line)
    (let ((buffer-contents (buffer-substring-no-properties (point-min) 
(point-max))))
@@ -119,7 +124,7 @@
 
   (phps-mode-test-with-buffer
    "<?php\n$variable = array(\n'random3'\n);\n$variable = true;\n"
-   nil
+   "Assignment test 1"
    (goto-char 28)
    (phps-mode-functions-indent-line)
    (let ((buffer-contents (buffer-substring-no-properties (point-min) 
(point-max))))
@@ -127,6 +132,7 @@
 
   (phps-mode-test-with-buffer
    "<?php\n$variable = array(\n    'random2'\n    );\n$variable = true;\n"
+   "Assignment test 2"
    (goto-char 43)
    ;; (message "Tokens %s point %s" phps-mode-lexer-tokens (point))
    (phps-mode-functions-indent-line)
@@ -135,7 +141,7 @@
 
   (phps-mode-test-with-buffer
    "<?php\n/**\n* My first line\n* My second line\n**/\n"
-   nil
+   "Doc-comment test 1"
    (goto-char 20)
    (phps-mode-functions-indent-line)
    ;; (message "Tokens %s point %s" phps-mode-lexer-tokens (point))
@@ -144,6 +150,7 @@
 
   (phps-mode-test-with-buffer
    "<?php\n/**\n* My first line\n* My second line\n**/\n"
+   "Doc-comment test 2"
    (goto-char 9)
    (phps-mode-functions-indent-line)
    ;; (message "Tokens %s point %s" phps-mode-lexer-tokens (point))
@@ -152,7 +159,7 @@
 
   (phps-mode-test-with-buffer
    "<?php\n/**\n* My first line\n* My second line\n**/\n"
-   nil
+   "Doc-comment test 3"
    (goto-char 46)
    (phps-mode-functions-indent-line)
    ;; (message "Tokens %s point %s" phps-mode-lexer-tokens (point))
@@ -161,6 +168,7 @@
 
   (phps-mode-test-with-buffer
    "<?php\n$variable = array(\n'random4');\n$variable = true;\n"
+   "Round bracket test 1"
    (goto-char 29)
    (phps-mode-functions-indent-line)
    ;; (message "Tokens %s point %s" phps-mode-lexer-tokens (point))
@@ -169,7 +177,7 @@
 
   (phps-mode-test-with-buffer
    "<?php\nadd_filter(\n\"views_{$screen->id}\",'__return_empty_array'\n);"
-   nil
+   "Round bracket test 2"
    (goto-char 25)
    (phps-mode-functions-indent-line)
    ;; (message "Tokens %s point %s" phps-mode-lexer-tokens (point))
@@ -178,6 +186,7 @@
 
   (phps-mode-test-with-buffer
    "<?php\nif (random_expression(\ntrue\n)) {\nsome_logic_here();\n}"
+   nil
    (goto-char 36)
    (phps-mode-functions-indent-line)
    ;; (message "Tokens %s point %s" phps-mode-lexer-tokens (point))
@@ -195,6 +204,7 @@
 
   (phps-mode-test-with-buffer
    "<?php\nif (myFirstCondition()) {\n    $this->var = 'abc123';\n    } else 
{\n    $this->var = 'def456';\n}\n"
+   nil
    (goto-char 68)
    (phps-mode-functions-indent-line)
    ;; (message "Tokens %s point %s" phps-mode-lexer-tokens (point))
@@ -213,6 +223,7 @@
   ;; Square bracket
   (phps-mode-test-with-buffer
    "<?php\n$var = [\n    'random' => [\n        'hello',\n],\n];\n"
+   nil
    (goto-char 51)
    (phps-mode-functions-indent-line)
    (let ((buffer-contents (buffer-substring-no-properties (point-min) 
(point-max))))
@@ -231,6 +242,7 @@
   
   (phps-mode-test-with-buffer
    "<?php\nswitch (myRandomCondition()) {\ncase 'Something here':\necho 
'Something else here';\n}\n"
+   nil
    (goto-char 45)
    (phps-mode-functions-indent-line)
    (let ((buffer-contents (buffer-substring-no-properties (point-min) 
(point-max))))
@@ -246,6 +258,7 @@
 
   (phps-mode-test-with-buffer
    "<?php\nif (myRandomCondition())\necho 'Something here';\necho 'Something 
else here';\n"
+   nil
    (goto-char 40)
    (phps-mode-functions-indent-line)
    (let ((buffer-contents (buffer-substring-no-properties (point-min) 
(point-max))))
@@ -261,6 +274,7 @@
 
   (phps-mode-test-with-buffer
    "<?php\nif (myRandomCondition()):\necho 'Something here';\n    echo 
'Something else here';\nendif;\n"
+   nil
    (goto-char 40)
    (phps-mode-functions-indent-line)
    (let ((buffer-contents (buffer-substring-no-properties (point-min) 
(point-max))))
@@ -276,6 +290,7 @@
 
   (phps-mode-test-with-buffer
    "<?php\n$var = $var2->getHead()\n->getTail();\n"
+   nil
    (goto-char 35)
    (phps-mode-functions-indent-line)
    (let ((buffer-contents (buffer-substring-no-properties (point-min) 
(point-max))))
@@ -291,6 +306,7 @@
 
   (phps-mode-test-with-buffer
    "<?php\nif (empty($this->var)):\n$this->var = 'abc123';\n    endif;"
+   nil
    (goto-char 60)
    (phps-mode-functions-indent-line)
    (let ((buffer-contents (buffer-substring-no-properties (point-min) 
(point-max))))



reply via email to

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