branch: externals/phps-mode commit 79e61f4903fd03b421348056ef84303a3e594670 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Fixed indentation with assignment lines on only one line --- phps-mode-functions.el | 15 ++++++++++----- phps-mode-test-functions.el | 20 +++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/phps-mode-functions.el b/phps-mode-functions.el index 2de738d..72e268c 100644 --- a/phps-mode-functions.el +++ b/phps-mode-functions.el @@ -77,6 +77,7 @@ (allow-custom-column-decrement nil) (in-assignment nil) (in-assignment-level 0) + (in-assignment-started-this-line nil) (in-class-declaration nil) (in-class-declaration-level 0)) @@ -124,14 +125,14 @@ (when first-token-is-nesting-increase (setq column-level (1+ column-level)))) - (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 last-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-level in-assignment-level in-class-declaration-level) + ;; (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 last-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) ;; Put indent-level to hash-table (when (> last-line-number 0) (puthash last-line-number `(,column-level ,tuning-level) line-indents)) (when (> token-end-line-number token-start-line-number) - (message "Token %s starts at %s and ends at %s" token token-start-line-number token-end-line-number) + ;; (message "Token %s starts at %s and ends at %s" token token-start-line-number token-end-line-number) (when (equal token 'T_DOC_COMMENT) (setq tuning-level 1)) @@ -167,7 +168,8 @@ (setq first-token-is-nesting-increase nil) (setq first-token-is-nesting-decrease nil) (setq in-assignment-level 0) - (setq in-class-declaration-level 0))) + (setq in-class-declaration-level 0) + (setq in-assignment-started-this-line nil))) (setq first-token-on-line nil) (when (> token-end-line-number token-start-line-number) ;; (message "Token not first on line %s starts at %s and ends at %s" token token-start-line-number token-end-line-number) @@ -314,17 +316,20 @@ (if (string= token ";") (progn (setq in-assignment nil) + (when in-assignment-started-this-line + (setq in-assignment-level 0)) ;; (message "Assignment ended at semi-colon") ) (when (and first-token-on-line (not in-heredoc)) (setq in-assignment-level 1) - (message "In assignment on new-line at %s" token) + ;; (message "In assignment on new-line at %s" token) )) (when (and (not after-special-control-structure) (string= token "=")) ;; (message "Started assignment") (setq in-assignment t) + (setq in-assignment-started-this-line t) (setq in-assignment-level 1))) ;; Did we encounter a token that supports extra special alternative control structures? @@ -393,7 +398,7 @@ (when first-token-is-nesting-increase (setq column-level (1+ column-level)))) - (message "last token at %s %s.%s (%s - %s) = %s %s %s %s %s [%s %s] %s %s" last-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-level in-class-declaration-level) + ;; (message "last token at %s %s.%s (%s - %s) = %s %s %s %s %s [%s %s] %s %s" last-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-level in-class-declaration-level) ;; Put indent-level to hash-table (puthash last-line-number `(,column-level ,tuning-level) line-indents)) diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el index 27763fb..57af0eb 100644 --- a/phps-mode-test-functions.el +++ b/phps-mode-test-functions.el @@ -59,6 +59,12 @@ (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (2 0)) (5 (1 0)) (6 (2 0)) (7 (1 0)) (8 (2 0)) (9 (2 0)) (10 (1 0)) (11 (0 0))) (phps-mode-test-functions--hash-to-list (phps-mode-functions-get-lines-indent))))) (phps-mode-test-with-buffer + "<?php\nif (myFirstCondition()) {\n $this->var = 'abc123';\n } else {\n $this->var = 'def456';\n}\n" + "Regular else expression indent calculation" + ;; (message "Tokens %s point %s" phps-mode-lexer-tokens (point)) + (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';\nelseif (true):\n echo 'Something';\nelse:\n echo 'Something else';\n echo 'Something else again';\nendif;\necho true;\n" "Alternative control structures" (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))))) @@ -90,6 +96,12 @@ (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (1 0)) (5 (1 0)) (6 (1 0)) (7 (1 0)) (8 (1 0))) (phps-mode-test-functions--hash-to-list (phps-mode-functions-get-lines-indent))))) (phps-mode-test-with-buffer + "<?php\n$variable = array(\n 'random4');\n$variable = true;\n" + "Array assignment on only two lines" + ;; (message "Tokens: %s" phps-mode-lexer-tokens) + (should (equal '((1 (0 0)) (2 (0 0)) (3 (0 0)) (4 (0 0)) ) (phps-mode-test-functions--hash-to-list (phps-mode-functions-get-lines-indent))))) + + (phps-mode-test-with-buffer "<?php\n$str = <<<'EOD'\nExample of string\nspanning multiple lines\nusing nowdoc syntax.\nEOD;\n" "Multi-line NOWDOC string" ;; (message "Tokens: %s" phps-mode-lexer-tokens) @@ -122,7 +134,7 @@ (phps-mode-test-with-buffer "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title2;\n\n} ?></title><body>Bla bla</body></html>" "Mixed HTML/PHP 2" - (message "Tokens: %s" phps-mode-lexer-tokens) + ;; (message "Tokens: %s" phps-mode-lexer-tokens) (should (equal '((1 (0 0)) (2 (1 0)) (3 (2 0)) (5 (1 0))) (phps-mode-test-functions--hash-to-list (phps-mode-functions-get-lines-indent))))) ;; NOTE Maybe concatenated strings spanning multiple lines outside assignments should have indentation? @@ -234,16 +246,14 @@ (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) (should (equal buffer-contents "<?php\n/**\n* My first line\n* My second line\n **/\n")))) - ;; TODO Fix this and also indentation for class implements - (phps-mode-test-with-buffer - "<?php\n$variable = array(\n'random4');\n$variable = true;\n" + "<?php\n$variable = array(\n 'random4');\n$variable = true;\n" "Round bracket test 1" (goto-char 30) (phps-mode-functions-indent-line) ;; (message "Tokens %s point %s" phps-mode-lexer-tokens (point)) (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal buffer-contents "<?php\n$variable = array(\n 'random4');\n$variable = true;\n")))) + (should (equal buffer-contents "<?php\n$variable = array(\n'random4');\n$variable = true;\n")))) (phps-mode-test-with-buffer "<?php\nadd_filter(\n\"views_{$screen->id}\",'__return_empty_array'\n);"