branch: externals/phps-mode commit 874701d380036ae3426505b31f2a7e80311f7eac Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Work on alternative and inline control structure syntax with new algorithm --- phps-mode-functions.el | 41 +++++++++++++++++++++++++++++++---------- phps-mode-test-functions.el | 26 +++++++++++++++++++++----- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/phps-mode-functions.el b/phps-mode-functions.el index 165d6ea..ddf0270 100644 --- a/phps-mode-functions.el +++ b/phps-mode-functions.el @@ -91,7 +91,8 @@ (nesting-stack nil) (changed-nesting-stack-in-line nil) (after-class-declaration nil) - (class-declaration-started-this-line nil)) + (class-declaration-started-this-line nil) + (special-control-structure-started-this-line nil)) (push `(END_PARSE ,(point-max) . ,(point-max)) tokens) @@ -149,13 +150,10 @@ (pop nesting-stack)) (when first-token-on-line - (setq after-class-declaration t) (setq first-token-is-nesting-increase nil) (setq first-token-is-nesting-decrease t)) - (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)) - ) (when first-token-on-line (setq in-class-declaration-level 1))) @@ -226,7 +224,9 @@ ;; 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)) @@ -243,18 +243,26 @@ (setq line-contained-nesting-increase t) (when after-special-control-structure-first-on-line (setq first-token-is-nesting-increase t)))) + + ;; 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 + + (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)))) + (setq in-inline-control-structure t)) + + (when (not special-control-structure-started-this-line) + (setq column-level (1+ column-level))) + )) (setq after-special-control-structure nil) (setq after-special-control-structure-token nil) @@ -270,7 +278,9 @@ ;; Did we reach a semicolon inside a inline block? Close the inline block (when (and in-inline-control-structure - (string= token ";")) + (string= token ";") + (not special-control-structure-started-this-line)) + (setq column-level (1- column-level)) (setq line-contained-nesting-decrease t) (setq inline-control-structure-level (1- inline-control-structure-level)) (setq in-inline-control-structure nil)) @@ -286,7 +296,18 @@ (equal token 'T_DEFAULT)) (setq after-special-control-structure-first-on-line first-token-on-line) (setq after-special-control-structure round-bracket-level) - (setq after-special-control-structure-token token)) + (setq after-special-control-structure-token token) + (setq special-control-structure-started-this-line t) + + (when (and (or (equal token 'T_ELSE) + (equal token 'T_ELSEIF) + (equal token 'T_DEFAULT)) + nesting-stack + (string= (car (cdr (cdr (car nesting-stack)))) ":")) + (setq column-level (1- column-level)) + (pop nesting-stack)) + + ) ;; Keep track of assignments (if in-assignment @@ -410,7 +431,6 @@ ;; Has nesting decreased? - ;; If nesting-end > 0 AND (!nesting-stack OR nesting-end > nesting-stack-end), push stack, increase indent (when (and (> nesting-end 0) (or (not nesting-stack) (> nesting-end (car (cdr (car nesting-stack)))))) @@ -428,7 +448,7 @@ (when phps-mode-functions-verbose ;; (message "\nPushing (%s %s) to nesting-stack since %s is greater than %s or stack is empty" nesting-start nesting-end nesting-end (car (cdr (car nesting-stack)))) ) - (push `(,nesting-stack-end ,nesting-end) nesting-stack) + (push `(,nesting-stack-end ,nesting-end ,token) nesting-stack) (when phps-mode-functions-verbose ;; (message "New stack %s, start: %s end: %s\n" nesting-stack (car (car nesting-stack)) (car (cdr (car nesting-stack)))) ))) @@ -457,7 +477,8 @@ (setq line-contained-nesting-decrease nil) (setq in-assignment-started-this-line nil) (setq changed-nesting-stack-in-line nil) - (setq class-declaration-started-this-line nil))) + (setq class-declaration-started-this-line nil) + (setq special-control-structure-started-this-line nil))) ;; Current token is not first (setq first-token-on-line nil) diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el index 16bb3f4..ebc2f8f 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';\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))))) - - (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))))) @@ -153,6 +148,26 @@ ) +(defun phps-mode-test-functions-get-lines-indent-alternative-if () + "Test for alternative if indentations." + + (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))))) + + (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))))) + + ) + (defun phps-mode-test-function-get-lines-indent-classes () "Test for class indent." @@ -489,6 +504,7 @@ (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-alternative-if) (phps-mode-test-functions-indent-line)) (phps-mode-test-functions)