branch: externals/phps-mode commit f93765d62f6a2f500ec417da15716705d4e39b7e Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Improved code structure of indentation were nesting start and end equals --- phps-mode-functions.el | 72 ++++++++++++++++++++++++++++++--------------- phps-mode-test-functions.el | 8 ++++- 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/phps-mode-functions.el b/phps-mode-functions.el index 538686a..d4b833f 100644 --- a/phps-mode-functions.el +++ b/phps-mode-functions.el @@ -38,12 +38,15 @@ ;; TODO Add support for automatic parenthesis, bracket, square-bracket, single-quote and double-quote encapsulations +;; Set indent for white-space lines as well (defun phps-mode-functions-get-lines-indent () "Get the column and tuning indentation-numbers for each line in buffer that contain tokens." (if (boundp 'phps-mode-lexer-tokens) (save-excursion (goto-char (point-min)) - (let ((in-heredoc nil) + (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-inline-control-structure nil) (after-special-control-structure nil) (after-special-control-structure-token nil) @@ -57,6 +60,7 @@ (alternative-control-structure-level 0) (inline-control-structure-level 0) (column-level 0) + (column-level-start 0) (tuning-level 0) (nesting-start 0) (nesting-end 0) @@ -65,6 +69,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) (token-number 1) (allow-custom-column-increment nil) (allow-custom-column-decrement nil) @@ -76,7 +82,8 @@ (token nil) (token-start-line-number 0) (token-end-line-number) - (tokens (nreverse phps-mode-lexer-tokens))) + (tokens (nreverse phps-mode-lexer-tokens)) + (nesting-stack '())) (push `(END_PARSE ,(point-max) . ,(point-max)) tokens) @@ -129,6 +136,13 @@ (when (string= token "}") (setq curly-bracket-level (1- curly-bracket-level)) + ;; Keep track of in scripting + (when (or (equal token 'T_OPEN_TAG) + (equal token 'T_OPEN_TAG_WITH_ECHO)) + (setq in-scripting t)) + (when (equal token 'T_CLOSE_TAG) + (setq in-scripting nil)) + ;; Decrease switch curly stack if any (when (and switch-curly-stack (= curly-bracket-level (car switch-curly-stack))) @@ -276,16 +290,28 @@ (setq in-class-declaration t) (setq in-class-declaration-level 1)))) - ;; Are we on a new line or is it the last token of the buffer? (when token - ;; Line logic + ;; Are we on a new line or is it the last token of the buffer? (if (> next-token-start-line-number token-start-line-number) + + ;; Line logic (progn ;; 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) @@ -300,27 +326,30 @@ (when (< column-level 0) (setq column-level 0))) - ;; Is line ending indentation equal to line beginning indentation and did we have a change of scope? - (when (and (= nesting-end nesting-start) - (not (and first-token-is-nesting-increase - first-token-is-nesting-decrease))) + (setq column-level-start column-level) + (when (= nesting-end nesting-start) (when (and first-token-is-nesting-decrease - (> column-level 0)) - (setq column-level (1- column-level))) - (when first-token-is-nesting-increase - (setq column-level (1+ column-level)))) - - ;; (message "Process line ending. nesting: %s-%s, line-number: %s-%s, indent: %s.%s, token: %s" nesting-start nesting-end token-start-line-number token-end-line-number column-level tuning-level token) + (not first-token-is-nesting-increase) + (> column-level-start 0)) + (setq column-level-start (1- column-level-start))) + (when (and first-token-is-nesting-increase + (not first-token-is-nesting-decrease)) + (setq column-level-start (1+ column-level-start)))) + + + (message "Process line ending. nesting: %s-%s, line-number: %s-%s, indent: %s.%s, token: %s" nesting-start nesting-end token-start-line-number token-end-line-number column-level-start tuning-level token) + ;; (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 next-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 (> token-start-line-number 0) - (puthash token-start-line-number `(,column-level ,tuning-level) line-indents)) - - ;; Does last token span several lines? + (puthash token-start-line-number `(,column-level-start ,tuning-level) line-indents)) + ;; Does token span over several lines? (when (> token-end-line-number token-start-line-number) ;; (message "Token %s starts at %s and ends at %s indent %s %s" next-token token-start-line-number token-end-line-number column-level tuning-level) + + ;; Indent doc-comment lines with 1 tuning (when (equal token 'T_DOC_COMMENT) (setq tuning-level 1)) @@ -329,14 +358,9 @@ (puthash (- token-end-line-number token-line-number-diff) `(,column-level ,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)))) - (setq tuning-level 0)) - ;; Is line ending indentation equal to line beginning indentation and did we have a change of scope? - (when (= nesting-end nesting-start) - (when first-token-is-nesting-decrease - (setq column-level (1+ column-level))) - (when first-token-is-nesting-increase - (setq column-level (1- column-level)))) + ;; Rest tuning-level used for comments + (setq tuning-level 0)) ;; Is line ending indentation higher than line beginning indentation? (when (> nesting-end nesting-start) diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el index f90e663..8147892 100644 --- a/phps-mode-test-functions.el +++ b/phps-mode-test-functions.el @@ -59,7 +59,7 @@ (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" + "<?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))))) @@ -186,6 +186,12 @@ (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\n// Can we load configuration?\nif ($configuration::load(\n self::getParameter(self::PARAMETER_CONFIGURATION_INTERNAL_FILENAME),\n self::getParameter(self::PARAMETER_CONFIGURATION_EXTERNAL_FILENAME),\n self::getParameter(self::PARAMETER_STRUCTURE_INTERNAL_FILENAME),\n self::getParameter(self::PARAMETER_STRUCTURE_EXTERNAL_FILENAME)\n)) {\n echo 'was here';\n}\n" + "If expression spanning multiple lines" + ;; (message "Tokens: %s" phps-mode-lexer-tokens) + (should (equal '((1 (0 0)) (2 (0 0)) (3 (0 0)) (4 (1 0)) (5 (1 0)) (6 (1 0)) (7 (1 0)) (8 (0 0)) (9 (1 0)) (10 (0 0))) (phps-mode-test-functions--hash-to-list (phps-mode-functions-get-lines-indent))))) + + (phps-mode-test-with-buffer "<?php\n// Can we load configuration?\nif ($configuration::load(\n self::getParameter(self::PARAMETER_CONFIGURATION_INTERNAL_FILENAME),\n self::getParameter(self::PARAMETER_CONFIGURATION_EXTERNAL_FILENAME),\n self::getParameter(self::PARAMETER_STRUCTURE_INTERNAL_FILENAME),\n self::getParameter(self::PARAMETER_STRUCTURE_EXTERNAL_FILENAME))\n) {\n echo 'was here';\n}\n" "If expression spanning multiple lines" ;; (message "Tokens: %s" phps-mode-lexer-tokens)