branch: externals/phps-mode commit c57fb5b22f8fdfb3efa2a11a97a59ad1e6ab7851 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Improved indentation of trailing opening brace --- phps-functions.el | 19 ++++++++++++++++++- phps-lexer.el | 2 +- phps-test-functions.el | 10 ++++++++-- phps-test-parser.el | 4 ++-- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/phps-functions.el b/phps-functions.el index 6c4a67b..c792c0a 100644 --- a/phps-functions.el +++ b/phps-functions.el @@ -69,13 +69,23 @@ (> indent-start indent-end)) (let ((token-number start-token-number) (valid-tokens t) + (last-token-is-open-brace nil) (tokens phps-mode/lexer-tokens)) ;; (message "token start %s, token end %s" start-token-number end-token-number) + + ;; Interate tokens in line and check if all are valid (while (and valid-tokens (<= token-number end-token-number)) (let ((token (car (nth token-number tokens))) (token-start (car (cdr (nth token-number tokens)))) (token-end (cdr (cdr (nth token-number tokens))))) + + ;; Check if last token is open brace + (when (and (= token-number end-token-number) + (string= token "{")) + (setq last-token-is-open-brace t)) + + ;; Check if current token is not one of the valid tokens (when (and valid-tokens (or (>= token-start (point)) (>= token-end (point))) @@ -90,9 +100,16 @@ (eq token 'T_CLOSE_TAG)))) ;; (message "Token %s - %s in %s was invalid" token token-number tokens) (setq valid-tokens nil))) + (setq token-number (+ token-number 1))) + (when valid-tokens ;; (message "Tokens was valid, decreasing indent %s - %s" (line-beginning-position) (line-end-position)) + + ;; If last token is a opening brace indent line one lesser column + (when last-token-is-open-brace + (setq indent-end (- indent-end 1))) + (setq indent-level (- indent-level (- indent-start indent-end)))))) (when in-doc-comment @@ -122,7 +139,7 @@ (boundp 'phps-mode/idle-interval)) ;; (run-with-idle-timer phps-mode/idle-interval nil #'phps-mode/lex--RUN) ;; TODO Maybe use incremental lexer once it's working - (run-with-idle-timer phps-mode/idle-interval nil #'phps-mode/run-incremental-lex) + (run-with-idle-timer phps-mode/idle-interval nil #'phps-mode/run-incremental-lex) ) (when (or (not phps-mode/buffer-changes--start) (< start phps-mode/buffer-changes--start)) diff --git a/phps-lexer.el b/phps-lexer.el index 2baabc1..70359c0 100644 --- a/phps-lexer.el +++ b/phps-lexer.el @@ -1337,7 +1337,7 @@ ANY_CHAR' ;; TODO Here clear all tokens after previous-token-start and add new tokens to stack )) - (display-warning "phps-mode" (format "Found no state to rewind to for %s in stack %s" change-start states)) + ;; (display-warning "phps-mode" (format "Found no state to rewind to for %s in stack %s, buffer point max: %s" change-start states (point-max))) (phps-mode/lex--RUN))) (setq phps-mode/buffer-changes--start nil))) diff --git a/phps-test-functions.el b/phps-test-functions.el index b4ef345..c24b35b 100644 --- a/phps-test-functions.el +++ b/phps-test-functions.el @@ -54,7 +54,6 @@ (phps-mode/with-test-buffer "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title1;\n} ?></title><body>Bla bla</body></html>" - (goto-char 75) (phps-mode/indent-line) (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) @@ -62,7 +61,6 @@ (phps-mode/with-test-buffer "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title2;\n\n} ?></title><body>Bla bla</body></html>" - (goto-char 98) (phps-mode/indent-line) (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) @@ -131,6 +129,14 @@ (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) (should (equal buffer-contents "<?php\nadd_filter(\n \"views_{$screen->id}\",'__return_empty_array'\n);")))) + (phps-mode/with-test-buffer + "<?php\nif (random_expression(\ntrue\n)) {\nsome_logic_here();\n}" + (goto-char 36) + (phps-mode/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\nif (random_expression(\ntrue\n)) {\nsome_logic_here();\n}")))) + ) (defun phps-mode/test-functions--get-point-data () diff --git a/phps-test-parser.el b/phps-test-parser.el index 1aab194..a8fef94 100644 --- a/phps-test-parser.el +++ b/phps-test-parser.el @@ -39,10 +39,10 @@ (defun phps-test-parser() "Run test for lexer." - (message "-- Running all tests for parser... --\n") + ;; (message "-- Running all tests for parser... --\n") ;; (setq debug-on-error t) - (message "\n-- Ran all tests for parser. --") + ;; (message "\n-- Ran all tests for parser. --") ) (phps-test-parser)