branch: externals/phps-mode commit 9031c3a86d989a5abe8edafd560341f320ca3fa0 Merge: 208a788 cf7dc07 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Merge branch 'master' of https://github.com/cjohansson/emacs-phps-mode --- docs/indentation-algorithm.md | 15 +++++++++++++++ phps-mode-functions.el | 29 +++++++++++++++++++---------- phps-mode-test-functions.el | 11 +++++++++-- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/docs/indentation-algorithm.md b/docs/indentation-algorithm.md index 42667c7..9d41b0c 100644 --- a/docs/indentation-algorithm.md +++ b/docs/indentation-algorithm.md @@ -18,6 +18,14 @@ Here follows pseudo-code for a algorithm that calculates indentation for each li ```php foreach token in buffer: + if token = T_START_HEREDOC: + in-heredoc = true; + in-heredoc-started-this-line = true; + elseif token == T_END_HEREDOC: + in-heredoc = false; + in-heredoc-ended-this-line = true; + endif; + calculate nesting-end; if nesting-stack AND nesting-end <= nesting-stack-start: // #decrease @@ -43,6 +51,10 @@ foreach token in buffer: indent-start = temp-pre-indent; endif; + if (in-heredoc AND !in-heredoc-started-this-line) OR in-heredoc-ended-this-line: + indent-start = 0; + endif; + save line indent-start; // #save if temp-post-indent: #temp-post-indent @@ -57,6 +69,9 @@ foreach token in buffer: push (nesting-stack-end nesting-end) to stack; indent++; endif; + + in-heredoc-started-this-line = false; + in-heredoc-ended-this-line = false; endif; endforeach; diff --git a/phps-mode-functions.el b/phps-mode-functions.el index 2f9974c..64545f0 100644 --- a/phps-mode-functions.el +++ b/phps-mode-functions.el @@ -52,6 +52,8 @@ (message "\nCalculation indentation for all lines in buffer:\n\n%s" (buffer-substring-no-properties (point-min) (point-max)))) (let ((in-scripting nil) (in-heredoc nil) + (in-heredoc-started-this-line nil) + (in-heredoc-ended-this-line nil) (in-inline-control-structure nil) (after-special-control-structure nil) (after-special-control-structure-token nil) @@ -312,8 +314,7 @@ (progn (setq in-assignment nil) (setq in-assignment-level 0)) - (when (and first-token-on-line - (not in-heredoc)) + (when first-token-on-line (setq in-assignment-level 1) ;; (message "In assignment on new-line at %s" token) )) @@ -329,12 +330,6 @@ (setq after-extra-special-control-structure t) (setq after-extra-special-control-structure-first-on-line first-token-on-line)) - ;; Keep track of whether we are inside a HEREDOC or NOWDOC - (when (equal token 'T_START_HEREDOC) - (setq in-heredoc t)) - (when (equal token 'T_END_HEREDOC) - (setq in-heredoc nil)) - ) (when token @@ -342,6 +337,14 @@ ;; Calculate nesting (setq nesting-end (+ round-bracket-level square-bracket-level curly-bracket-level alternative-control-structure-level in-assignment-level in-class-declaration-level)) + ;; Keep track of whether we are inside a HEREDOC or NOWDOC + (when (equal token 'T_START_HEREDOC) + (setq in-heredoc t) + (setq in-heredoc-started-this-line t)) + (when (equal token 'T_END_HEREDOC) + (setq in-heredoc nil) + (setq in-heredoc-ended-this-line t)) + ;; Has nesting increased? (when (and nesting-stack (<= nesting-end (car (car nesting-stack)))) @@ -397,6 +400,11 @@ (setq temp-pre-indent nil)) + ;; HEREDOC lines should have zero indent + (when (or (and in-heredoc + (not in-heredoc-started-this-line)) + in-heredoc-ended-this-line) + (setq column-level-start 0)) ;; Save line indent @@ -422,7 +430,7 @@ (let ((token-line-number-diff (1- (- token-end-line-number token-start-line-number)))) (while (>= token-line-number-diff 0) - (puthash (- token-end-line-number token-line-number-diff) `(,column-level ,tuning-level) line-indents) + (puthash (- token-end-line-number token-line-number-diff) `(,column-level-start ,tuning-level) line-indents) ;; (message "Saved line %s indent %s %s" (- token-end-line-number token-line-number-diff) column-level tuning-level) (setq token-line-number-diff (1- token-line-number-diff)))) @@ -434,7 +442,7 @@ (setq column-level temp-post-indent) (setq temp-post-indent nil)) - ;; Decrease indentation + ;; Increase indentation (when (and (> nesting-end 0) (or (not nesting-stack) (> nesting-end (car (cdr (car nesting-stack)))))) @@ -482,6 +490,7 @@ (setq in-assignment-started-this-line nil) (setq changed-nesting-stack-in-line nil) (setq class-declaration-started-this-line nil) + (setq in-heredoc-started-this-line nil) (setq special-control-structure-started-this-line nil))) ;; Current token is not first diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el index 25fb65a..9cf29e5 100644 --- a/phps-mode-test-functions.el +++ b/phps-mode-test-functions.el @@ -57,6 +57,13 @@ "Round and square bracket expressions" (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (1 0)) (5 (2 0)) (6 (1 0)) (7 (1 0)) (8 (0 0))) (phps-mode-test-functions--hash-to-list (phps-mode-functions-get-lines-indent))))) + (phps-mode-test-with-buffer + "<?php\nvar_dump(array(<<<EOD\nfoobar!\nEOD\n));\n?>" + "HEREDOC in arguments example" + ;; (message "Tokens: %s" phps-mode-lexer-tokens) + (should (equal '((1 (0 0)) (2 (0 0)) (3 (0 0)) (4 (0 0)) (5 (0 0)) (6 (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" @@ -67,13 +74,13 @@ "<?php\n$var = \"A line\nmore text here\nlast line here\";" "Multi-line double-quoted string" ;; (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))))) + (should (equal '((1 (0 0)) (2 (1 0)) (3 (1 0)) (4 (1 0))) (phps-mode-test-functions--hash-to-list (phps-mode-functions-get-lines-indent))))) (phps-mode-test-with-buffer "<?php\n$var = 'A line\nmore text here\nlast line here';" "Multi-line single-quoted string" ;; (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))))) + (should (equal '((1 (0 0)) (2 (1 0)) (3 (1 0)) (4 (1 0))) (phps-mode-test-functions--hash-to-list (phps-mode-functions-get-lines-indent))))) (phps-mode-test-with-buffer "<?php\necho \"A line\" .\n \"more text here\" .\n \"last line here\";"