branch: externals/phps-mode commit 202827b1e6dbbd6614611572cd73118e020a77fc Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
New indentation algorithm now supports inline control structures --- docs/indentation-algorithm.md | 12 +++-- phps-mode-functions.el | 117 +++++++++++++++++------------------------- phps-mode-test-functions.el | 28 +++++++--- 3 files changed, 78 insertions(+), 79 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..a4adf26 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,7 +334,7 @@ (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 @@ -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)))))) @@ -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..3ea3d6b 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." @@ -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)