[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode d8503c70bb 106/212: Passing indent tests for
From: |
Christian Johansson |
Subject: |
[elpa] externals/phps-mode d8503c70bb 106/212: Passing indent tests for switch case |
Date: |
Wed, 26 Jan 2022 01:51:07 -0500 (EST) |
branch: externals/phps-mode
commit d8503c70bbc86707c70e27e6c515ab08e811a788
Author: Christian Johansson <christian@cvj.se>
Commit: Christian Johansson <christian@cvj.se>
Passing indent tests for switch case
---
phps-mode-indent.el | 109 ++++++++++++++++++++++++++++++------------
test/phps-mode-test-indent.el | 10 ++--
2 files changed, 86 insertions(+), 33 deletions(-)
diff --git a/phps-mode-indent.el b/phps-mode-indent.el
index 70b0bd6d12..56cc892463 100644
--- a/phps-mode-indent.el
+++ b/phps-mode-indent.el
@@ -34,23 +34,27 @@
backward-string
nil))))
-(defun phps-mode-indent--string-starts-with-regexp (string regexp)
- "If STRING start with REGEXP, return it, otherwise nil."
+(defun phps-mode-indent--string-starts-with-regexp (string regexp &optional
match-index)
+ "If STRING start with REGEXP, return it, otherwise nil. With optional
MATCH-INDEX."
(phps-mode-indent--string-match-regexp
string
- (concat "^" regexp)))
+ (concat "^" regexp)
+ match-index))
-(defun phps-mode-indent--string-ends-with-regexp (string regexp)
- "If STRING end with REGEXP, return it, otherwise nil."
+(defun phps-mode-indent--string-ends-with-regexp (string regexp &optional
match-index)
+ "If STRING end with REGEXP, return it, otherwise nil. With optional
MATCH-INDEX."
(phps-mode-indent--string-match-regexp
string
- (concat regexp "$")))
+ (concat regexp "$")
+ match-index))
-(defun phps-mode-indent--string-match-regexp (string regexp)
- "If STRING match REGEXP, return it, otherwise nil."
+(defun phps-mode-indent--string-match-regexp (string regexp &optional
match-index)
+ "If STRING match REGEXP, return it, otherwise nil. With optional
MATCH-INDEX."
+ (unless match-index
+ (setq match-index 0))
(if
(string-match regexp string)
- (match-string 0 string)
+ (match-string match-index string)
nil))
@@ -61,43 +65,50 @@
"If STRING start with closing bracket, return it, otherwise return nil."
(phps-mode-indent--string-starts-with-regexp
string
- "[\t ]*\\([\]})[]\\)"))
+ "[\t ]*\\([\]})[]\\)"
+ 1))
(defun phps-mode-indent--string-starts-with-opening-bracket (string)
"If STRING start with opening bracket return it otherwise nil."
(phps-mode-indent--string-starts-with-regexp
string
- "[\t ]*\\([\[{(]\\)"))
+ "[\t ]*\\([\[{(]\\)"
+ 1))
(defun phps-mode-indent--string-starts-with-opening-doc-comment (string)
"Does STRING start with opening doc comment?"
(phps-mode-indent--string-starts-with-regexp
string
- "[\t ]*/\\*\\*"))
+ "[\t ]*\\(/\\*\\*\\)"
+ 1))
(defun phps-mode-indent--string-ends-with-assignment (string)
"If STRING end with terminus, return it, otherwise return nil."
(phps-mode-indent--string-ends-with-regexp
string
- "=>?[\t ]*"))
+ "\\(=>?\\)[\t ]*"
+ 1))
(defun phps-mode-indent--string-ends-with-closing-bracket (string)
"If STRING end with closing bracket, return it, otherwise nil."
(phps-mode-indent--string-ends-with-regexp
string
- "\\([\]})]\\)[\t ]*"))
+ "\\([\]})]\\)[\t ]*"
+ 1))
(defun phps-mode-indent--string-ends-with-opening-bracket (string)
"If STRING end with opening bracket, return it, otherwise nil."
(phps-mode-indent--string-ends-with-regexp
string
- "\\([\[{(]\\)[\t ]*"))
+ "\\([\[{(]\\)[\t ]*"
+ 1))
(defun phps-mode-indent--string-ends-with-terminus (string)
"If STRING end with terminus, return it, otherwise return nil."
(phps-mode-indent--string-ends-with-regexp
string
- "\\(;\\|,\\)[\t ]*"))
+ "\\(;\\|,\\)[\t ]*"
+ 1))
;; Main functions
@@ -224,7 +235,9 @@
;; or
;; class MyClass extends
;; myParent
- (when (string-match-p "[\t ]+\\(extends\\|implements\\)$"
previous-line-string)
+ (when (string-match-p
+ "[\t ]+\\(extends\\|implements\\)$"
+ previous-line-string)
(setq previous-bracket-level (+ tab-width)))
;; class MyClass
@@ -371,7 +384,7 @@
;; echo 'Something';
;; echo 'Afterwards';
(when (string-match-p
- "[\t ]*\\(else[\t ]*$\\|else.*)[\t
]*$\\|if.*)$\\|while.*)$\\)"
+ "^[\t ]*\\(else[\t ]*$\\|else.*)[\t
]*$\\|if.*)$\\|while.*)$\\)"
previous2-line-string)
(setq
new-indentation
@@ -390,7 +403,7 @@
;; ];
(when
(string-match-p
- "[\t ]*\\()\\|]\\);[\t ]*$"
+ "^[\t ]*\\()\\|]\\);[\t ]*$"
current-line-string)
(let ((old-point (point))
(still-looking t)
@@ -549,6 +562,7 @@
;; default:
(when (and
(not previous-line-ends-with-opening-bracket)
+ (not (string-match-p ":[\t ]*$" previous-line-string))
(or
(string-match-p
"^[\t ]*case[\t ]+.*\\(;\\|:\\)[\t ]*$"
@@ -560,6 +574,14 @@
new-indentation
(- new-indentation tab-width)))
+ ;; (message "\ncurrent-line-string: %S" current-line-string)
+ ;; ;; (message "current-line-starts-with-closing-bracket: %S"
current-line-starts-with-closing-bracket)
+ ;; (message "previous-line-string: %S" previous-line-string)
+ ;; ;; (message "previous-line-starts-with-opening-doc-comment:
%S" previous-line-starts-with-opening-doc-comment)
+ ;; (message "previous-bracket-level: %S" previous-bracket-level)
+ ;; (message "previous-indentation: %S" previous-indentation)
+ ;; (message "new-indentation: %S" new-indentation)
+
;; switch ($condition) {
;; case true:
;; echo 'here';
@@ -569,23 +591,50 @@
(string= current-line-starts-with-closing-bracket "}"))
(let ((old-point (point))
(end-of-switch-statement))
- (when (search-backward-regexp "{" nil t)
- (let ((bracket-start-line
- (buffer-substring-no-properties
- (line-beginning-position)
- (line-end-position))))
- (when (string-match-p
- "[\t ]*switch[\t ]*("
- bracket-start-line)
- (setq
- end-of-switch-statement
- t)))
+ (when (search-backward-regexp "[{}]" nil t)
+ (when (looking-at-p "{")
+ (let ((bracket-start-line
+ (buffer-substring-no-properties
+ (line-beginning-position)
+ (line-end-position))))
+ (when (string-match-p
+ "^[\t ]*switch[\t ]*("
+ bracket-start-line)
+ (setq
+ end-of-switch-statement
+ t))))
(goto-char old-point)
(when end-of-switch-statement
(setq
new-indentation
(- new-indentation tab-width))))))
+ ;; switch (blala):
+ ;; case bla:
+ ;; echo 'bla';
+ ;; endswitch;
+ (when (and
+ (string-match-p
+ "^[\t ]*endswitch[\t ]*;[\t ]*$"
+ current-line-string)
+ (not
+ (string-match-p
+ "[\t ]*switch"
+ previous-line-string)))
+ (setq
+ new-indentation
+ (- new-indentation tab-width tab-width)))
+
+ ;; switch ($array):
+ ;; case 'Something';
+ (when (and
+ (string-match-p
+ "^[\t ]*\\(case.+\\|default\\)\\(;\\|:\\)[\t ]*$"
+ current-line-string)
+ (string-match-p
+ "^[\t ]*\\(switch\\)[\t ]*(.+):$"
+ previous-line-string))
+ (setq new-indentation (+ new-indentation tab-width)))
(when (> previous-bracket-level 0)
diff --git a/test/phps-mode-test-indent.el b/test/phps-mode-test-indent.el
index c658387575..93b21101fd 100644
--- a/test/phps-mode-test-indent.el
+++ b/test/phps-mode-test-indent.el
@@ -517,12 +517,16 @@
"Switch, case, default with alternative control structure")
(phps-mode-test-indent--should-equal
- "<?php\nif (true) {\n switch ($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 endswitch;\n
sprintf(__(\n 'Error: %s',\n $error\n ));\n}\n"
- "Alternative switch, case, default with exception after it")
+ "<?php\nif (true) {\n switch ($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}\n"
+ "Curly switch, case, default inside if expression body")
(phps-mode-test-indent--should-equal
"<?php\nif (true) {\n switch ($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
sprintf(__(\n 'Error: %s',\n $error\n ));\n}\n"
- "Curly switch, case, default with exception after it")
+ "Curly switch, case, default with expression after it")
+
+ (phps-mode-test-indent--should-equal
+ "<?php\nif (true) {\n switch ($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 endswitch;\n
sprintf(__(\n 'Error: %s',\n $error\n ));\n}\n"
+ "Alternative switch, case, default with expression after it")
(phps-mode-test-indent--should-equal
"<?php\n$product_path = \"${filename[0]}/${filename[1]}/\";\necho 'here';\n"
- [elpa] externals/phps-mode 8115521ef8 011/212: Imenu-generation via parser SDT passing more tests, (continued)
- [elpa] externals/phps-mode 8115521ef8 011/212: Imenu-generation via parser SDT passing more tests, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 040b2564b0 031/212: Some work on AST bookkeeping, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 16c15c9ef4 004/212: Added type check to imenu-index generation, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 35c9b0fd1c 003/212: Imenu generated correctly for one case, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 8652f7162e 014/212: Started on bookkeeping generation via parser SDT, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode a0d2abc2ee 009/212: More working on imenu generation via parser SDT, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode c7e308a13f 024/212: Added SDT foreach($x as $key => $value), Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode d44e79ad03 028/212: More work on grammar SDT, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode f79b6118a4 087/212: Optimization of string indentation function, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode b94ee63396 089/212: More work on indentation, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode d8503c70bb 106/212: Passing indent tests for switch case,
Christian Johansson <=
- [elpa] externals/phps-mode 4f20de353e 109/212: Passed indent tests for PSR-2, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 6655248e86 105/212: Passing indent test for regular switch statement, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 4e5be4f8f8 112/212: More indentation work for mixed PHP/HTML content, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode ff62626f48 110/212: Passing indent test for square bracket array, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 50d006852c 129/212: Fixed issue with indent being dependent on position of point on current line, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 3de8eff9f1 114/212: More wrestling with indentation, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode d3b5a85b70 001/212: Using property list as basis for AST, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode a80c12ef14 005/212: Passing imenu-generation for file with multiple namespaces, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 0071b40aae 007/212: Syntax coloring is now in separate file, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 1cc4aed616 016/212: Passing first bookkeeping test via generation via parser SDT, Christian Johansson, 2022/01/26