branch: externals/phps-mode commit 86404c818818244ca161e62c5e220d75078948e8 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
New algorithm now passes tests for alternative control structure --- docs/indentation-algorithm.md | 34 ++++++++++++++++++++++---------- phps-mode-functions.el | 46 ++++++++++++++++++++++++++++++++++--------- phps-mode-test-functions.el | 4 ++-- 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/docs/indentation-algorithm.md b/docs/indentation-algorithm.md index 43e08cf..42667c7 100644 --- a/docs/indentation-algorithm.md +++ b/docs/indentation-algorithm.md @@ -17,24 +17,38 @@ Here follows pseudo-code for a algorithm that calculates indentation for each li ```php foreach token in buffer: - + calculate nesting-end; - + if nesting-stack AND nesting-end <= nesting-stack-start: // #decrease pop stack; - indent--; + + if first-token-on-line-is-nesting-decrease: + indent--; + else: + if !temp-post-indent: + temp-post-indent = indent; + endif; + + temp-post-indent--; + endif; + endif; - - if we reached end of a line: + 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-start; // #save - + + if temp-post-indent: #temp-post-indent + indent = temp-post-indent; + endif; + if nesting-end > 0 AND (!nesting-stack OR nesting-end > nesting-stack-end): // #increase if !nesting-stack: nesting-stack-end = 0; @@ -44,7 +58,7 @@ foreach token in buffer: indent++; endif; endif; - + endforeach; ``` @@ -66,7 +80,7 @@ if (function( // #save indent: 0, #increase push (0 2) indent: 1 ```php // #save indent: 0 if (function( // #save indent: 0, #increase push (0 2) indent: 1 - false)) { // #decrease pop (0 2) post-indent: 0, #save indent: 1, #increase push (0 1) indent: 1 TODO fix ERROR + false)) { // #decrease pop (0 2) temp-post-indent: 0, #save indent: 1, #temp-post-indent indent: 0, #increase push (0 1) indent: 1 echo true; // #save indent: 1 } // #decrease pop (0 1) indent: 0, #save indent: 0 ``` diff --git a/phps-mode-functions.el b/phps-mode-functions.el index c8d60e4..2f9974c 100644 --- a/phps-mode-functions.el +++ b/phps-mode-functions.el @@ -92,7 +92,8 @@ (after-class-declaration nil) (class-declaration-started-this-line nil) (special-control-structure-started-this-line nil) - (temp-pre-indent nil)) + (temp-pre-indent nil) + (temp-post-indent nil)) (push `(END_PARSE ,(point-max) . ,(point-max)) tokens) @@ -294,6 +295,10 @@ (string= (car (cdr (cdr (car nesting-stack)))) ":")) (setq alternative-control-structure-level (1- alternative-control-structure-level)) + + (when first-token-on-line + (setq first-token-is-nesting-decrease t)) + (when phps-mode-functions-verbose (message "\nDecreasing alternative control structure nesting at %s to %s\n" token alternative-control-structure-level)) ) @@ -346,16 +351,35 @@ ) (pop nesting-stack) - ;; Decrement column - (if allow-custom-column-decrement + (if first-token-is-nesting-decrease + (progn - (setq column-level (- column-level (- nesting-start nesting-end))) - (setq allow-custom-column-increment nil)) - (setq column-level (1- column-level))) + ;; Decrement column + (if allow-custom-column-decrement + (progn + (setq column-level (- column-level (- nesting-start nesting-end))) + (setq allow-custom-column-increment nil)) + (setq column-level (1- column-level))) + + ;; Prevent negative column-values + (when (< column-level 0) + (setq column-level 0))) + + (when (not temp-post-indent) + (setq temp-post-indent column-level)) + + ;; Decrement column + (if allow-custom-column-decrement + (progn + (setq temp-post-indent (- temp-post-indent (- nesting-start nesting-end))) + (setq allow-custom-column-increment nil)) + (setq temp-post-indent (1- temp-post-indent))) + + ;; Prevent negative column-values + (when (< temp-post-indent 0) + (setq temp-post-indent 0)) - ;; Prevent negative column-values - (when (< column-level 0) - (setq column-level 0))) + )) ;; Are we on a new line or is it the last token of the buffer? (if (> next-token-start-line-number token-start-line-number) @@ -405,6 +429,10 @@ ;; Rest tuning-level used for comments (setq tuning-level 0)) + ;; Support trailing indent decrements + (when temp-post-indent + (setq column-level temp-post-indent) + (setq temp-post-indent nil)) ;; Decrease indentation (when (and (> nesting-end 0) diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el index f21aae3..c5c3363 100644 --- a/phps-mode-test-functions.el +++ b/phps-mode-test-functions.el @@ -156,13 +156,13 @@ (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" + "<?php\nif (true)\n echo 'Something';\nelse if (true)\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" + "Inline control structures while" (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0))) (phps-mode-test-functions--hash-to-list (phps-mode-functions-get-lines-indent))))) )