[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode 96588b5 142/405: More work on new indentation
From: |
Stefan Monnier |
Subject: |
[elpa] externals/phps-mode 96588b5 142/405: More work on new indentation calculation functions |
Date: |
Sat, 13 Jul 2019 10:00:01 -0400 (EDT) |
branch: externals/phps-mode
commit 96588b5bd0d78f1484f3e3e2487c74f92cdca6d7
Author: Christian Johansson <address@hidden>
Commit: Christian Johansson <address@hidden>
More work on new indentation calculation functions
---
phps-mode-functions.el | 85 ++++++++++++++++++++++++---------------------
phps-mode-test-functions.el | 4 +--
2 files changed, 47 insertions(+), 42 deletions(-)
diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index ab547eb..09b55c3 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -40,7 +40,7 @@
;; TODO Support indentation for multi-line assignments
(defun phps-mode-functions-get-lines-indent ()
- "Get the column number and space number for every line in current buffer."
+ "Get the column and tuning indentation-numbers for each line in buffer that
contains tokens."
(if (boundp 'phps-mode-lexer-tokens)
(save-excursion
(beginning-of-line)
@@ -57,14 +57,14 @@
(square-bracket-level 0)
(alternative-control-structure-level 0)
(inline-control-structure-level 0)
- (indent-level 0)
- (adjust-level 0)
- (indent-start 0)
- (indent-end 0)
+ (column-level 0)
+ (tuning-level 0)
+ (nesting-start 0)
+ (nesting-end 0)
(last-line-number 0)
(first-token-on-line nil)
- (first-token-on-line-is-closing-token nil)
- (line-indents (make-hash-table :test 'equal)))
+ (line-indents (make-hash-table :test 'equal))
+ (change-of-scope nil))
;; Iterate through all buffer tokens from beginning to end
(dolist (item phps-mode-lexer-tokens)
@@ -77,80 +77,80 @@
(if (> token-line-number last-line-number)
(progn
- ;; TODO Here populate potential lines in-between last line
and current line with nil
-
- ;; Calculate indentation leven at end of line
- (setq indent-end (+ round-bracket-level
square-bracket-level curly-bracket-level alternative-control-structure-level
inline-control-structure-level))
+ ;; 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))
;; Is line ending indentation higher than line beginning
indentation?
- (when (> indent-end indent-start)
+ (when (> nesting-end nesting-start)
;; Increase indentation by one
- (setq indent-level (1+ indent-level)))
+ (setq column-level (1+ column-level)))
;; Is line ending indentation lesser than line beginning
indentation?
- (when (< indent-end indent-start)
+ (when (< nesting-end nesting-start)
;; Decrease indentation by one
- (setq indent-level (1- indent-level)))
+ (setq column-level (1- column-level)))
+
+ ;; Is line ending indentation equal to line beginning
indentation and did we have a change of scope?
+ (when (and (= nesting-end nesting-start)
+ change-of-scope)
+ (setq column-level (1- column-level)))
+
;; Increase indent with one space inside doc-comment,
HEREDOC or NOWDOC
(if (or in-doc-comment in-heredoc)
- (setq adjust-level 1)
- (setq adjust-level 0))
+ (setq tuning-level 1)
+ (setq tuning-level 0))
- (message "%s, %s = %s %s %s %s %s" token indent-level
round-bracket-level square-bracket-level curly-bracket-level
alternative-control-structure-level inline-control-structure-level)
+ (message "%s, %s = %s %s %s %s %s %s" token column-level
tuning-level round-bracket-level square-bracket-level curly-bracket-level
alternative-control-structure-level inline-control-structure-level)
;; Put indent-level to hash-table
- (puthash last-line-number `(,indent-level ,adjust-level)
line-indents)
+ (when (> last-line-number 0)
+ (puthash last-line-number `(,column-level ,tuning-level)
line-indents))
;; Calculate indentation level at start of line
- (setq indent-start (+ round-bracket-level
square-bracket-level curly-bracket-level alternative-control-structure-level
inline-control-structure-level))
+ (setq nesting-start (+ round-bracket-level
square-bracket-level curly-bracket-level alternative-control-structure-level
inline-control-structure-level))
;; Set initial values for tracking first token
(setq first-token-on-line t)
- (setq first-token-on-line-is-closing-token nil))
+ (setq change-of-scope nil))
(setq first-token-on-line nil))
;; Keep track of round bracket level
(when (string= token "(")
+ (setq change-of-scope t)
(setq round-bracket-level (1+ round-bracket-level)))
(when (string= token ")")
- (setq round-bracket-level (1- round-bracket-level))
- (when first-token-on-line
- (setq first-token-on-line-is-closing-token t)))
+ (setq change-of-scope t)
+ (setq round-bracket-level (1- round-bracket-level)))
;; Keep track of square bracket level
(when (string= token "[")
+ (setq change-of-scope t)
(setq square-bracket-level (1+ square-bracket-level)))
(when (string= token "]")
- (setq square-bracket-level (1- square-bracket-level))
- (when first-token-on-line
- (setq first-token-on-line-is-closing-token t)))
+ (setq change-of-scope t)
+ (setq square-bracket-level (1- square-bracket-level)))
;; Keep track of curly bracket level
(when (or (equal token 'T_CURLY_OPEN)
(equal token 'T_DOLLAR_OPEN_CURLY_BRACES)
(string= token "{"))
+ (setq change-of-scope t)
(setq curly-bracket-level (1+ curly-bracket-level)))
(when (string= token "}")
- (setq curly-bracket-level (1- curly-bracket-level))
- (when first-token-on-line
- (setq first-token-on-line-is-closing-token t)))
+ (setq change-of-scope t)
+ (setq curly-bracket-level (1- curly-bracket-level)))
- ;; Keep track of alternative control structure level
+ ;; Keep track of ending alternative control structure level
(when (or (equal token 'T_ENDIF)
(equal token 'T_ENDWHILE)
(equal token 'T_ENDFOR)
(equal token 'T_ENDFOREACH)
- (equal token 'T_ENDSWITCH)
- (equal token 'T_CASE)
- (equal token 'T_DEFAULT)
- (equal token 'T_ELSE))
- (setq alternative-control-structure-level (1-
alternative-control-structure-level))
- (when first-token-on-line
- (message "Token %s was start of line level %s" token
alternative-control-structure-level)
- (setq first-token-on-line-is-closing-token t)))
+ (equal token 'T_ENDSWITCH))
+ (setq change-of-scope t)
+ (setq alternative-control-structure-level (1-
alternative-control-structure-level)))
;; TODO Support for else as alternative and inline control
structure
@@ -164,7 +164,10 @@
;; Is it the start of an alternative control structure?
(if (string= token ":")
- (setq alternative-control-structure-level (1+
alternative-control-structure-level))
+ (progn
+ (setq change-of-scope t)
+ (setq alternative-control-structure-level (1+
alternative-control-structure-level)))
+ (setq change-of-scope t)
(setq inline-control-structure-level (1+
inline-control-structure-level))
(setq in-inline-control-structure t)))
@@ -173,12 +176,14 @@
;; Support extra special control structures (CASE and DEFAULT)
(when (and after-extra-special-control-structure
(string= token ":"))
+ (setq change-of-scope t)
(setq alternative-control-structure-level (1+
alternative-control-structure-level))
(setq after-extra-special-control-structure nil))
;; Did we reach a semicolon inside a inline block? Close the
inline block
(when (and in-inline-control-structure
(string= token ";"))
+ (setq change-of-scope t)
(setq inline-control-structure-level (1-
inline-control-structure-level))
(setq in-inline-control-structure nil))
diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el
index 1b07ac1..9e613c3 100644
--- a/phps-mode-test-functions.el
+++ b/phps-mode-test-functions.el
@@ -41,9 +41,9 @@
;; Mixed HTML/PHP
(phps-mode-test-with-buffer
- "<html><head><title><?php\nif ($myCondition) {\nif ($mySeconCondition)
{\necho $title;\n\n} ?></title><body>Bla bla</body></html>"
+ "<html><head><title><?php\nif ($myCondition) {\nif ($mySeconCondition)
{\necho $title;\n}\n} ?></title><body>Bla bla</body></html>"
(goto-char 15)
- (should (equal nil (phps-mode-functions-get-lines-indent))))
+ (should (equal #s(hash-table size 65 test equal data (1 (0 0) 2 (0 0) 3 (1
0) 4 (2 0) 5 (1 0) 6 (0 0))) (phps-mode-functions-get-lines-indent))))
(phps-mode-test-with-buffer
"<html><head><title><?php\nif ($myCondition) {\nif ($mySeconCondition)
{\necho $title;\n\n} ?></title><body>Bla bla</body></html>"
- [elpa] externals/phps-mode d380cd4 129/405: Updated readme, (continued)
- [elpa] externals/phps-mode d380cd4 129/405: Updated readme, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 3f08b86 128/405: Updated tracking of alternative control structures, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 52c502c 139/405: Improved logic for doc-comment tracking, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 1472273 136/405: Begun working on new approach to indentation calculation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 9792f70 133/405: Cleaning up code gather point meta information, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode cc947a9 140/405: Added more tests for new indentation function, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode d3e00f2 141/405: Starting on new function for indentation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode c68c7a0 131/405: Fixed issue were first-token-on-line was nil, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode c4ee0c3 132/405: Added more failing tests, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 0b3ced4 145/405: First test for new indentation function passes, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 96588b5 142/405: More work on new indentation calculation functions,
Stefan Monnier <=
- [elpa] externals/phps-mode 9d6af11 157/405: More work on indentations, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode e249124 152/405: Prevent negative indent, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 4a27c44 147/405: Added support for naming unit tests to better find failures, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode b852f5f 160/405: All tests for indentation passes, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode dfb0427 151/405: Added unit test for round and square bracket expressions, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 7b8cea3 163/405: Added failing indentation test for class multiple implements, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode a61ff64 134/405: Renamed function get-point-data to get-current-line-data for better semantics, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode b99da6b 153/405: Starting using new indentation calculation for indentations, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 6559760 166/405: Work on assignments and class declaration indentations, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 2720be8 165/405: Indentation calculation for heredoc and nowdoc assignments passes, Stefan Monnier, 2019/07/13