[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode 2d4646f 203/405: Work on inline control struc
From: |
Stefan Monnier |
Subject: |
[elpa] externals/phps-mode 2d4646f 203/405: Work on inline control structure indentation |
Date: |
Sat, 13 Jul 2019 10:00:13 -0400 (EDT) |
branch: externals/phps-mode
commit 2d4646f2f1e8faf4a79f6b36f30140c233592430
Author: Christian Johansson <address@hidden>
Commit: Christian Johansson <address@hidden>
Work on inline control structure indentation
---
docs/indentation-algorithm.md | 12 +++--
phps-mode-functions.el | 121 +++++++++++++++++-------------------------
phps-mode-test-functions.el | 38 +++++++++----
3 files changed, 85 insertions(+), 86 deletions(-)
diff --git a/docs/indentation-algorithm.md b/docs/indentation-algorithm.md
index 2a4dba8..e967e27 100644
--- a/docs/indentation-algorithm.md
+++ b/docs/indentation-algorithm.md
@@ -26,8 +26,14 @@ foreach token in buffer:
endif;
if we reached end of a line:
+
+ indent-start = indent;
+
+ if temp-pre-indent: // #temp-pre-indent
+ indent-start = temp-pre-indent;
+ endif;
- save line indent; // #save
+ save line indent-start; // #save
if nesting-end > 0 AND (!nesting-stack OR nesting-end >
nesting-stack-end): // #increase
if !nesting-stack:
@@ -56,9 +62,9 @@ if (function( // #save indent: 0, #increase push (0
2) indent: 1
```php
if (true) // #save indent: 0
- echo true; // #pre-temp-increase indent: 1, #save indent: 1,
#pre-temp-decrease indent: 0
+ echo true; // #temp-pre-indent: 1, #save indent: 1
else // #save indent: 0
- echo false; // #pre-temp-increase indent: 1, #save indent: 1,
#pre-temp-decrease indent: 0
+ echo false; // #temp-pre-indent: 1, #save indent: 1
```
## Alternative control structure for if-else 2
diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index 746608f..61b795b 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -63,7 +63,6 @@
(round-bracket-level 0)
(square-bracket-level 0)
(alternative-control-structure-level 0)
- (inline-control-structure-level 0)
(column-level 0)
(column-level-start 0)
(tuning-level 0)
@@ -92,7 +91,8 @@
(changed-nesting-stack-in-line nil)
(after-class-declaration nil)
(class-declaration-started-this-line nil)
- (special-control-structure-started-this-line nil))
+ (special-control-structure-started-this-line nil)
+ (temp-pre-indent nil))
(push `(END_PARSE ,(point-max) . ,(point-max)) tokens)
@@ -105,6 +105,7 @@
(next-token-start-line-number nil)
(next-token-end-line-number nil))
+ ;; Handle the pseudo-token for last-line
(if (equal next-token 'END_PARSE)
(progn
(setq next-token-start-line-number (1+
token-start-line-number))
@@ -213,60 +214,48 @@
(not (string= token ")"))
(not (string= token "(")))
- ;; Is token not a curly bracket - because that is a ordinary
control structure syntax
- (if (string= token "{")
+ ;; Handle the else if case
+ (if (equal 'T_IF token)
+ (setq after-special-control-structure-token token)
- (when (equal after-special-control-structure-token
'T_SWITCH)
- ;; (message "Opening switch, increase curly brackets
to %s" curly-bracket-level)
- (push curly-bracket-level switch-curly-stack)
- (setq allow-custom-column-increment t)
- (setq curly-bracket-level (1+ curly-bracket-level)))
-
- ;; Is it the start of an alternative control structure?
- (if (string= token ":")
-
- (progn
- ;; Alternative syntax for control structures here
- (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_DEFAULT))
- (progn
- (setq line-contained-nesting-increase t)
- (when
after-special-control-structure-first-on-line
- (setq first-token-is-nesting-decrease t)))
+ ;; Is token not a curly bracket - because that is a
ordinary control structure syntax
+ (if (string= token "{")
- (when (equal after-special-control-structure-token
'T_SWITCH)
- (setq alternative-control-structure-level (1+
alternative-control-structure-level))
- (setq allow-custom-column-increment t))
+ (when (equal after-special-control-structure-token
'T_SWITCH)
+ ;; (message "Opening switch, increase curly brackets
to %s" curly-bracket-level)
+ (push curly-bracket-level switch-curly-stack)
+ (setq allow-custom-column-increment t)
+ (setq curly-bracket-level (1+ curly-bracket-level)))
- (setq alternative-control-structure-level (1+
alternative-control-structure-level))
- (setq line-contained-nesting-increase t)
- (when after-special-control-structure-first-on-line
- (setq first-token-is-nesting-increase t))))
+ ;; Is it the start of an alternative control structure?
+ (if (string= token ":")
- ;; Inline syntax for control structures here
- (if (or (equal after-special-control-structure-token
'T_ELSE)
- (equal after-special-control-structure-token
'T_ELSEIF))
(progn
- (setq line-contained-nesting-increase t)
- (when after-special-control-structure-first-on-line
+ ;; Alternative syntax for control structures here
+ (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_DEFAULT))
+ (progn
+ (setq line-contained-nesting-increase t)
+ (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 allow-custom-column-increment t))
+ (setq alternative-control-structure-level (1+
alternative-control-structure-level))
+ (setq line-contained-nesting-increase t)
+ (when
after-special-control-structure-first-on-line
+ (setq first-token-is-nesting-increase t))))
- (setq first-token-is-nesting-increase t)))
- ;; (message "Was inline-control structure %s %s"
after-special-control-structure-token token)
- (setq inline-control-structure-level (1+
inline-control-structure-level))
- (setq line-contained-nesting-increase t)
- (when after-special-control-structure-first-on-line
- (setq first-token-is-nesting-increase t))
- (setq in-inline-control-structure t))
-
- (when (not special-control-structure-started-this-line)
- (setq column-level (1+ column-level)))
- ))
+ ;; (message "Started inline control-structure after %s
at %s" after-special-control-structure-token token)
+ (setq in-inline-control-structure t)
+ (setq temp-pre-indent (1+ column-level))))
- (setq after-special-control-structure nil)
- (setq after-special-control-structure-token nil)
- (setq after-special-control-structure-first-on-line nil))
+ (setq after-special-control-structure nil)
+ (setq after-special-control-structure-token nil)
+ (setq after-special-control-structure-first-on-line nil)))
;; Support extra special control structures (CASE)
(when (and after-extra-special-control-structure
@@ -281,7 +270,6 @@
(string= token ";")
(not special-control-structure-started-this-line))
(setq line-contained-nesting-decrease t)
- (setq inline-control-structure-level (1-
inline-control-structure-level))
(setq in-inline-control-structure nil))
;; Did we encounter a token that supports alternative and
inline control structures?
@@ -346,14 +334,14 @@
(when token
;; Calculate nesting
- (setq nesting-end (+ round-bracket-level square-bracket-level
curly-bracket-level alternative-control-structure-level
inline-control-structure-level in-assignment-level in-class-declaration-level))
+ (setq nesting-end (+ round-bracket-level square-bracket-level
curly-bracket-level alternative-control-structure-level in-assignment-level
in-class-declaration-level))
;; Has nesting increased?
(when (and nesting-stack
(<= nesting-end (car (car nesting-stack))))
(when phps-mode-functions-verbose
- ;; (message "\nPopping %s from nesting-stack since %s is
lesser or equal to %s, next value is: %s\n" (car nesting-stack) nesting-end
(car (car nesting-stack)) (nth 1 nesting-stack))
+ (message "\nPopping %s from nesting-stack since %s is
lesser or equal to %s, next value is: %s\n" (car nesting-stack) nesting-end
(car (car nesting-stack)) (nth 1 nesting-stack))
)
(pop nesting-stack)
@@ -377,37 +365,26 @@
;; ;; Start indentation might differ from ending
indentation in cases like } else {
(setq column-level-start column-level)
- ;; (when (= nesting-end nesting-start)
-
- ;; (when (and after-class-declaration
- ;; (> column-level-start 0))
- ;; (setq column-level-start (1- column-level-start)))
- ;; ;; ;; Handle cases like: } else {
- ;; ;; (when (and first-token-is-nesting-decrease
- ;; ;; (not first-token-is-nesting-increase)
- ;; ;; (> column-level-start 0))
- ;; ;; (setq column-level-start (1-
column-level-start)))
+ ;; Support temporarily pre-indent
+ (when temp-pre-indent
+ (setq column-level-start temp-pre-indent)
+ (setq temp-pre-indent nil))
- ;; ;; ;; Handle cases like if (blaha)\n echo
'blaha';
- ;; ;; (when (and first-token-is-nesting-increase
- ;; ;; (not first-token-is-nesting-decrease))
- ;; ;; (setq column-level-start (1+
column-level-start)))
- ;; )
+ ;; Save line indent
+
(when phps-mode-functions-verbose
(message "Process line ending. nesting: %s-%s,
line-number: %s-%s, indent: %s.%s, token: %s" nesting-start nesting-end
token-start-line-number token-end-line-number column-level-start tuning-level
token))
-
- ;; (message "new line %s or last token at %s, %s %s.%s
(%s - %s) = %s %s %s %s %s [%s %s] %s %s %s" token-start-line-number token
next-token column-level tuning-level nesting-start nesting-end
round-bracket-level square-bracket-level curly-bracket-level
alternative-control-structure-level inline-control-structure-level
first-token-is-nesting-decrease first-token-is-nesting-increase in-assignment
in-assignment-level in-class-declaration-level)
(when (> token-start-line-number 0)
;; Save line indentation
(puthash token-start-line-number `(,column-level-start
,tuning-level) line-indents))
- ;; TODO Handle case were current line number is more
than 1 above last line number and then fill lines in-between with indentation
+ ;; TODO Fill token-less but in-scripting lines
in-between with indentation
;; Does token span over several lines?
@@ -428,7 +405,7 @@
(setq tuning-level 0))
- ;; Has nesting decreased?
+ ;; Decrease indentation
(when (and (> nesting-end 0)
(or (not nesting-stack)
(> nesting-end (car (cdr (car
nesting-stack))))))
@@ -444,7 +421,7 @@
(setq column-level (1+ column-level)))
(when phps-mode-functions-verbose
- (message "\nPushing (%s %s %s) to nesting-stack
since %s is greater than %s or stack is empty" nesting-start nesting-end token
nesting-end (car (cdr (car nesting-stack))))
+ (message "\nPushing (%s %s %s) to nesting-stack
since %s is greater than %s or stack is empty\n" nesting-start nesting-end
token nesting-end (car (cdr (car nesting-stack))))
)
(push `(,nesting-stack-end ,nesting-end ,token)
nesting-stack)
(when phps-mode-functions-verbose
@@ -461,7 +438,7 @@
;; (message "New stack %s, start: %s end: %s"
nesting-stack (car (car nesting-stack)) (car (cdr (car nesting-stack)))))
;; Calculate indentation level at start of line
- (setq nesting-start (+ round-bracket-level
square-bracket-level curly-bracket-level alternative-control-structure-level
inline-control-structure-level in-assignment-level in-class-declaration-level))
+ (setq nesting-start (+ round-bracket-level
square-bracket-level curly-bracket-level alternative-control-structure-level
in-assignment-level in-class-declaration-level))
;; Set initial values for tracking first token
(when (> token-start-line-number last-line-number)
diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el
index ebc2f8f..62f8146 100644
--- a/phps-mode-test-functions.el
+++ b/phps-mode-test-functions.el
@@ -48,11 +48,6 @@
"Test `phps-mode-functions-get-lines-indent' function."
(phps-mode-test-with-buffer
- "<?php\nif (true)\n echo 'Something';\nelse\n echo 'Something
else';\necho true;\n"
- "Inline control structures"
- (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (0 0)) (5 (1 0)) (6 (0
0))) (phps-mode-test-functions--hash-to-list
(phps-mode-functions-get-lines-indent)))))
-
- (phps-mode-test-with-buffer
"<?php\n/**\n * Bla\n */"
"DOC-COMMENT"
(should (equal '((1 (0 0)) (2 (0 0)) (3 (0 1)) (4 (0 1)))
(phps-mode-test-functions--hash-to-list
(phps-mode-functions-get-lines-indent)))))
@@ -148,6 +143,26 @@
)
+(defun phps-mode-test-functions-get-lines-indent-inline-if ()
+ "Test for inline if indentations."
+
+ (phps-mode-test-with-buffer
+ "<?php\nif (true)\n echo 'Something';\nelse\n echo 'Something
else';\necho true;\n"
+ "Inline control structures if else"
+ (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (0 0)) (5 (1 0)) (6 (0
0))) (phps-mode-test-functions--hash-to-list
(phps-mode-functions-get-lines-indent)))))
+
+ (phps-mode-test-with-buffer
+ "<?php\nif (true)\n echo 'Something';\nelse if\n echo 'Something
else';\necho true;\n"
+ "Inline control structures if else if"
+ (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (0 0)) (5 (1 0)) (6 (0
0))) (phps-mode-test-functions--hash-to-list
(phps-mode-functions-get-lines-indent)))))
+
+ (phps-mode-test-with-buffer
+ "<?php\nwhile (true)\n echo 'Something';"
+ "Inline control structures"
+ (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)))
(phps-mode-test-functions--hash-to-list
(phps-mode-functions-get-lines-indent)))))
+
+ )
+
(defun phps-mode-test-functions-get-lines-indent-alternative-if ()
"Test for alternative if indentations."
@@ -157,15 +172,15 @@
(should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (0 0)) (5 (1 0)) (6 (0
0)) (7 (1 0)) (8 (1 0)) (9 (0 0)) (10 (0 0)))
(phps-mode-test-functions--hash-to-list
(phps-mode-functions-get-lines-indent)))))
(phps-mode-test-with-buffer
- "<?php\nif (true):\n echo 'Something';\nelseif (true\n && true):\n
echo 'Something';\nelse:\n echo 'Something else';\n echo 'Something else
again';\nendif;\necho true;\n"
- "Alternative control structures with multi-line elseif 1"
- (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (0 0)) (5 (1 0)) (6 (1
0)) (7 (0 0)) (8 (1 0)) (9 (1 0)) (10 (0 0)) (11 (0 0)))
(phps-mode-test-functions--hash-to-list
(phps-mode-functions-get-lines-indent)))))
-
- (phps-mode-test-with-buffer
"<?php\nif (true):\n echo 'Something';\nelseif (true\n && true\n):\n
echo 'Something';\nelse:\n echo 'Something else';\n echo 'Something
else again';\nendif;\necho true;\n"
"Alternative control structures with multi-line elseif 1"
(should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (0 0)) (5 (1 0)) (6 (0
0)) (7 (1 0)) (8 (0 0)) (9 (1 0)) (10 (1 0)) (11 (0 0)) (12 (0 0)))
(phps-mode-test-functions--hash-to-list
(phps-mode-functions-get-lines-indent)))))
+ (phps-mode-test-with-buffer
+ "<?php\nif (true):\n echo 'Something';\nelseif (true\n && true):\n
echo 'Something';\nelse:\n echo 'Something else';\n echo 'Something else
again';\nendif;\necho true;\n"
+ "Alternative control structures with multi-line elseif 2"
+ (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (0 0)) (5 (1 0)) (6 (1
0)) (7 (0 0)) (8 (1 0)) (9 (1 0)) (10 (0 0)) (11 (0 0)))
(phps-mode-test-functions--hash-to-list
(phps-mode-functions-get-lines-indent)))))
+
)
(defun phps-mode-test-function-get-lines-indent-classes ()
@@ -503,8 +518,9 @@
(setq phps-mode-functions-verbose t)
(phps-mode-test-functions-get-lines-lindent-if)
(phps-mode-test-function-get-lines-indent-classes)
- (phps-mode-test-functions-get-lines-indent)
+ (phps-mode-test-functions-get-lines-indent-inline-if)
(phps-mode-test-functions-get-lines-indent-alternative-if)
+ (phps-mode-test-functions-get-lines-indent)
(phps-mode-test-functions-indent-line))
(phps-mode-test-functions)
- [elpa] externals/phps-mode e8658c8 192/405: New indentation algorithm now passes class unit tests, started on indentation document, (continued)
- [elpa] externals/phps-mode e8658c8 192/405: New indentation algorithm now passes class unit tests, started on indentation document, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode ba9d8a0 199/405: More organization of tests, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 86404c8 207/405: New algorithm now passes tests for alternative control structure, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 8b4c188 188/405: Now able to detect indent change of scope with same nesting level, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 91715f6 217/405: Added another test for multi-line function arguments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 75c4af4 214/405: Fixed indentation for lines wrapped in scripting open/close, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode d754163 221/405: More work on multi-line token-less lines, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 8947e6e 202/405: Conceptual work on indentation-calculation for multi-line assignment, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode fc2e8b7 212/405: Added logic to indent token-less lines as well, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 8707777 237/405: Updated README, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 2d4646f 203/405: Work on inline control structure indentation,
Stefan Monnier <=
- [elpa] externals/phps-mode 202827b 198/405: New indentation algorithm now supports inline control structures, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode b2a5700 218/405: Fixed indentation for multi-line optional function arguments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f7186a1 239/405: Preparations for hierarchical imenu, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 43bf350 216/405: Added tests for token-less lines, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 0de10fa 235/405: Passes first test for imenu, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f791cee 234/405: Started on unit tests for imenu-index generation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode b56a15c 233/405: Removed debugging output, updated todo items, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 73784d0 225/405: Cleaned up tests for indentation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f7e7c74 219/405: Work on indentation for switch-case, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode c2af31e 240/405: Imenu support now shows hierarchical information, Stefan Monnier, 2019/07/13