[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))))
- [elpa] externals/phps-mode a5ced61 158/405: New indentation function passes all original unit tests, (continued)
- [elpa] externals/phps-mode a5ced61 158/405: New indentation function passes all original unit tests, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode e3eedd7 167/405: Assignment indentation passes tests, class declaration remains, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode e37c1cf 150/405: New indention calculation works with doc-comments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f789468 172/405: Took notes of new indentation tests, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 20e77aa 181/405: Removed debug output, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 321803a 182/405: Line indents are now cleaning on buffer changes, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 38bcabb 176/405: Improved detection of assignment ends in function optional arguments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f1236e2 169/405: Unit tests for indentation calculation for multi-line strings passes, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 105fece 149/405: New indent calculation for inline syntax passes test, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 0a32b17 161/405: Improved calculation for indentation for multi-line assignments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode b2eac4d 154/405: More work on switch case default,
Stefan Monnier <=
- [elpa] externals/phps-mode 6b5a5c3 155/405: More work on calculating indentations, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode ce2d4bd 148/405: New indentation calculation function passes alternative, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 3718931 156/405: Basic tests for indentation calculation passes, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 13e9148 159/405: Indentation unit test for assignment is passing, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode b5c64c1 162/405: Added failing tests for HEREDOC and NOWDOC assignments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f283e4b 173/405: Added new failing unit tests for multiple-lines IF and single line class declaration, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 59474ea 180/405: Improved debugging messages, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 9659ab4 174/405: Added failing test for doc-comments are namespace and class, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode c5d8ec6 168/405: Indentation calculation for class implement working, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f93765d 184/405: Improved code structure of indentation were nesting start and end equals, Stefan Monnier, 2019/07/13