branch: externals/phps-mode commit 8b4c18803f8f61f08749e0557d8e2aa57b6806fc Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Now able to detect indent change of scope with same nesting level --- phps-mode-functions.el | 30 ++++++++++++++++-------------- phps-mode-test-functions.el | 12 +++++++++--- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/phps-mode-functions.el b/phps-mode-functions.el index 2693b4e..87959ee 100644 --- a/phps-mode-functions.el +++ b/phps-mode-functions.el @@ -74,8 +74,8 @@ (line-indents (make-hash-table :test 'equal)) (first-token-is-nesting-decrease nil) (first-token-is-nesting-increase nil) - (last-token-is-nesting-increase nil) - (last-token-is-nesting-decrease nil) + (line-contained-nesting-decrease nil) + (line-contained-nesting-increase nil) (token-number 1) (allow-custom-column-increment nil) (allow-custom-column-decrement nil) @@ -114,6 +114,7 @@ ;; Keep track of round bracket level (when (string= token "(") (setq round-bracket-level (1+ round-bracket-level)) + (setq line-contained-nesting-increase t) (when first-token-on-line (setq first-token-is-nesting-increase t))) (when (string= token ")") @@ -124,10 +125,12 @@ ;; Keep track of square bracket level (when (string= token "[") (setq square-bracket-level (1+ square-bracket-level)) + (setq line-contained-nesting-increase t) (when first-token-on-line (setq first-token-is-nesting-increase t))) (when (string= token "]") (setq square-bracket-level (1- square-bracket-level)) + (setq line-contained-nesting-decrease t) (when first-token-on-line (setq first-token-is-nesting-decrease t))) @@ -136,9 +139,11 @@ (equal token 'T_DOLLAR_OPEN_CURLY_BRACES) (string= token "{")) (setq curly-bracket-level (1+ curly-bracket-level)) + (setq line-contained-nesting-increase t) (when first-token-on-line (setq first-token-is-nesting-increase t))) (when (string= token "}") + (setq line-contained-nesting-decrease t) (setq curly-bracket-level (1- curly-bracket-level)) ;; Keep track of in scripting @@ -196,6 +201,7 @@ (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))) @@ -204,15 +210,18 @@ (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)))) (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 (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)))) @@ -224,6 +233,7 @@ ;; Support extra special control structures (CASE) (when (and after-extra-special-control-structure (string= token ":")) + (setq line-contained-nesting-increase t) (when after-extra-special-control-structure-first-on-line (setq first-token-is-nesting-decrease t)) (setq after-extra-special-control-structure nil)) @@ -231,6 +241,7 @@ ;; Did we reach a semicolon inside a inline block? Close the inline block (when (and in-inline-control-structure (string= token ";")) + (setq line-contained-nesting-decrease t) (setq inline-control-structure-level (1- inline-control-structure-level)) (setq in-inline-control-structure nil)) @@ -306,17 +317,6 @@ ;; Calculate indentation level at end of line (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)) - ;; Set flags for last token - (setq last-token-is-nesting-increase (or (string= token "{") - (string= token "(") - (string= token "["))) - (setq last-token-is-nesting-decrease (or (string= token "}") - (string= token ")") - (string= token "]"))) - - ;; TODO Should keep stack of nesting-levels and only change columns when nesting exceeds previous - ;; TODO Should only change column-level once below - ;; Is line ending indentation lesser than line beginning indentation? (when (< nesting-end nesting-start) @@ -380,7 +380,7 @@ ;; When nesting decreases but ends with a nesting increase, increase indent by one (when (and (< nesting-end nesting-start) - last-token-is-nesting-increase) + line-contained-nesting-increase) (setq column-level (1+ column-level))) ;; Calculate indentation level at start of line @@ -393,6 +393,8 @@ (setq first-token-is-nesting-decrease nil) (setq in-assignment-level 0) (setq in-class-declaration-level 0) + (setq line-contained-nesting-increase nil) + (setq line-contained-nesting-decrease nil) (setq in-assignment-started-this-line nil))) (setq first-token-on-line nil) diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el index fa7439a..35846a4 100644 --- a/phps-mode-test-functions.el +++ b/phps-mode-test-functions.el @@ -203,14 +203,20 @@ (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (2 0)) (5 (2 0)) (6 (2 0)) (7 (2 0)) (8 (1 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 (myFunction(\n true)\n) {\n echo 'was here';\n}\n" + "<?php\nif (myFunction(true)\n) {\n echo 'was here';\n}\n" "If expression spanning multiple lines 4" ;; (message "Tokens: %s" phps-mode-lexer-tokens) - (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))))) + (should (equal '((1 (0 0)) (2 (0 0)) (3 (0 0)) (4 (1 0)) (5 (0 0))) (phps-mode-test-functions--hash-to-list (phps-mode-functions-get-lines-indent))))) + + (phps-mode-test-with-buffer + "<?php\nif (myFunction(\ntrue)\n) {\n echo 'was here';\n}\n" + "If expression spanning multiple lines 5" + ;; (message "Tokens: %s" phps-mode-lexer-tokens) + (should (equal '((1 (0 0)) (2 (0 0)) (3 (0 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 if (myFunction(\n true)\n ) {\n echo 'was here';\n }\n}\n" - "Nested if expression spanning multiple lines 5" + "Nested if expression spanning multiple lines 6" ;; (message "Tokens: %s" phps-mode-lexer-tokens) (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (2 0)) (5 (1 0)) (6 (2 0)) (7 (1 0)) (8 (0 0))) (phps-mode-test-functions--hash-to-list (phps-mode-functions-get-lines-indent)))))