[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode 6b5a5c3 155/405: More work on calculating ind
From: |
Stefan Monnier |
Subject: |
[elpa] externals/phps-mode 6b5a5c3 155/405: More work on calculating indentations |
Date: |
Sat, 13 Jul 2019 10:00:03 -0400 (EDT) |
branch: externals/phps-mode
commit 6b5a5c37accf351aa3fc26326974a86a253d03d2
Author: Christian Johansson <address@hidden>
Commit: Christian Johansson <address@hidden>
More work on calculating indentations
---
phps-mode-functions.el | 74 ++++++++++++++++++++++++++++++++++++---------
phps-mode-test-functions.el | 8 ++++-
2 files changed, 66 insertions(+), 16 deletions(-)
diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index a9c1b70..4d701c0 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -38,8 +38,6 @@
;; TODO Add support for automatic parenthesis, bracket, square-bracket,
single-quote and double-quote encapsulations
-;; TODO Support inline function indentation
-
;; TODO Support indentation for multi-line assignments
(defun phps-mode-functions-get-lines-indent ()
@@ -73,7 +71,10 @@
(first-token-is-nesting-increase nil)
(token-number 1)
(last-token-number (length phps-mode-lexer-tokens))
- (last-token nil))
+ (last-token nil)
+ (last-token-was-first-on-new-line nil)
+ (allow-custom-column-increment nil)
+ (allow-custom-column-decrement nil))
;; Iterate through all buffer tokens from beginning to end
(dolist (item phps-mode-lexer-tokens)
@@ -88,6 +89,10 @@
(= token-number last-token-number))
(progn
+ ;; Flag when last token was on a new line
+ (when (= token-number last-token-number)
+ (setq last-token-was-first-on-new-line t))
+
;; 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))
@@ -95,8 +100,12 @@
(when (and (< nesting-end nesting-start)
(> column-level 0))
- ;; Decrease indentation by one
- (setq column-level (1- column-level)))
+ ;; Decrement column
+ (if allow-custom-column-decrement
+ (progn
+ (setq column-level (- nesting-end nesting-start))
+ (setq allow-custom-column-increment nil))
+ (setq column-level (1- column-level))))
;; Is line ending indentation equal to line beginning
indentation and did we have a change of scope?
(when (= nesting-end nesting-start)
@@ -105,14 +114,14 @@
(when first-token-is-nesting-increase
(setq column-level (1+ column-level))))
- (message "new line at %s, %s %s.%s (%s - %s) = %s %s %s %s
%s [%s %s]" token last-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)
+ ;; (message "new line at %s, %s %s.%s (%s - %s) = %s %s %s
%s %s [%s %s]" token last-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)
;; Put indent-level to hash-table
(when (> last-line-number 0)
(puthash last-line-number `(,column-level ,tuning-level)
line-indents))
(when (> token-end-line-number token-start-line-number)
- (message "Token %s starts at %s and ends at %s" token
token-start-line-number token-end-line-number)
+ ;; (message "Token %s starts at %s and ends at %s" token
token-start-line-number token-end-line-number)
(when (equal token 'T_DOC_COMMENT)
(setq tuning-level 1))
(let ((token-line-number-diff (1- (-
token-end-line-number token-start-line-number))))
@@ -131,8 +140,12 @@
;; Is line ending indentation higher than line beginning
indentation?
(when (> nesting-end nesting-start)
- ;; Increase indentation by one
- (setq column-level (1+ column-level)))
+ ;; Increase indentation
+ (if allow-custom-column-increment
+ (progn
+ (setq column-level (- nesting-end nesting-start))
+ (setq allow-custom-column-increment nil))
+ (setq column-level (1+ column-level))))
;; Calculate indentation level at start of line
(setq nesting-start (+ round-bracket-level
square-bracket-level curly-bracket-level alternative-control-structure-level
inline-control-structure-level))
@@ -198,8 +211,6 @@
(when first-token-on-line
(setq first-token-is-nesting-decrease t)))
- ;; TODO Support for else as alternative and inline control
structure
-
(when (and after-special-control-structure
(= after-special-control-structure
round-bracket-level)
(not (string= token ")"))
@@ -210,7 +221,8 @@
(when (equal after-special-control-structure-token
'T_SWITCH)
(setq curly-bracket-level (1+ curly-bracket-level))
- (message "Opening switch, increase curly brackets to %s"
curly-bracket-level)
+ (setq allow-custom-column-increment t)
+ ;; (message "Opening switch, increase curly brackets to
%s" curly-bracket-level)
(push curly-bracket-level switch-curly-stack))
;; Is it the start of an alternative control structure?
@@ -224,7 +236,8 @@
(setq first-token-is-nesting-decrease t)))
(when (equal after-special-control-structure-token
'T_SWITCH)
- (setq alternative-control-structure-level (1+
alternative-control-structure-level)))
+ (setq alternative-control-structure-level (1+
alternative-control-structure-level))
+ (setq allow-custom-column-increment t))
(setq alternative-control-structure-level (1+
alternative-control-structure-level))
(when after-special-control-structure-first-on-line
@@ -234,13 +247,15 @@
(progn
(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)
+ ;; (message "Was inline-control structure %s %s"
after-special-control-structure-token token)
(setq inline-control-structure-level (1+
inline-control-structure-level))
(when after-special-control-structure-first-on-line
(setq first-token-is-nesting-increase t))
(setq in-inline-control-structure t))))
- (setq after-special-control-structure nil))
+ (setq after-special-control-structure nil)
+ (setq after-special-control-structure-token nil)
+ (setq after-special-control-structure-first-on-line nil))
;; Support extra special control structures (CASE)
(when (and after-extra-special-control-structure
@@ -294,6 +309,35 @@
(setq token-number (1+ token-number))))
+ ;; Process line if last token was first on new line
+ (when last-token-was-first-on-new-line
+
+ ;; 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 lesser than line beginning
indentation?
+ (when (and (< nesting-end nesting-start)
+ (> column-level 0))
+
+ ;; Decrement column
+ (if allow-custom-column-decrement
+ (progn
+ (setq column-level (- nesting-end nesting-start))
+ (setq allow-custom-column-increment nil))
+ (setq column-level (1- column-level))))
+
+ ;; 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))))
+
+ ;; (message "last token at %s %s.%s (%s - %s) = %s %s %s %s %s [%s
%s]" last-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)
+
+ ;; Put indent-level to hash-table
+ (puthash last-line-number `(,column-level ,tuning-level)
line-indents))
+
line-indents))
nil))
diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el
index 660f6e6..af254d9 100644
--- a/phps-mode-test-functions.el
+++ b/phps-mode-test-functions.el
@@ -49,7 +49,12 @@
"Test `phps-mode-functions-get-lines-indent' function."
(phps-mode-test-with-buffer
- "<html><head><title><?php\nif ($myCondition) {\n if ($mySeconCondition)
{\n echo $title;\n } else if ($mySecondCondition) {\n echo
$title4;\n } else {\n echo $title2;\n echo $title3;\n }\n}
?></title><body>Bla bla</body></html>"
+ "<?php\nnamespace myNamespace\n{\n class myClass\n {\n public
function myFunction()\n {\n echo 'my statement';\n }\n
}\n}\n"
+ "Regular PHP with namespaces, classes and functions"
+ (should (equal '((1 (0 0)) (2 (0 0)) (3 (0 0)) (4 (1 0)) (5 (1 0)) (6 (2
0)) (7 (2 0)) (8 (3 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
+ "<html><head><title><?php\nif ($myCondition) {\n if ($mySecondCondition)
{\n echo $title;\n } else if ($mySecondCondition) {\n echo
$title4;\n } else {\n echo $title2;\n echo $title3;\n }\n}
?></title><body>Bla bla</body></html>"
"Mixed HTML/PHP"
(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)))))
@@ -76,6 +81,7 @@
(phps-mode-test-with-buffer
"<?php\nswitch ($condition) {\n case true:\n echo 'here';\n
echo 'here 2';\n case false:\n echo 'here 4';\n default:\n
echo 'here 3';\n}\n"
"Switch, case, default"
+ (message "tokens %s" phps-mode-lexer-tokens)
(should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (2 0)) (5 (2 0)) (6 (1
0)) (7 (2 0)) (8 (1 0)) (9 (2 0)) (10 (0 0)))
(phps-mode-test-functions--hash-to-list
(phps-mode-functions-get-lines-indent)))))
;; TODO NOWDOC, HEREDOC
- [elpa] externals/phps-mode e3eedd7 167/405: Assignment indentation passes tests, class declaration remains, (continued)
- [elpa] externals/phps-mode e3eedd7 167/405: Assignment indentation passes tests, class declaration remains, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode e37c1cf 150/405: New indention calculation works with doc-comments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f789468 172/405: Took notes of new indentation tests, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 20e77aa 181/405: Removed debug output, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 321803a 182/405: Line indents are now cleaning on buffer changes, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 38bcabb 176/405: Improved detection of assignment ends in function optional arguments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f1236e2 169/405: Unit tests for indentation calculation for multi-line strings passes, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 105fece 149/405: New indent calculation for inline syntax passes test, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 0a32b17 161/405: Improved calculation for indentation for multi-line assignments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode b2eac4d 154/405: More work on switch case default, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 6b5a5c3 155/405: More work on calculating indentations,
Stefan Monnier <=
- [elpa] externals/phps-mode ce2d4bd 148/405: New indentation calculation function passes alternative, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 3718931 156/405: Basic tests for indentation calculation passes, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 13e9148 159/405: Indentation unit test for assignment is passing, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode b5c64c1 162/405: Added failing tests for HEREDOC and NOWDOC assignments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f283e4b 173/405: Added new failing unit tests for multiple-lines IF and single line class declaration, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 59474ea 180/405: Improved debugging messages, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 9659ab4 174/405: Added failing test for doc-comments are namespace and class, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode c5d8ec6 168/405: Indentation calculation for class implement working, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f93765d 184/405: Improved code structure of indentation were nesting start and end equals, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 2af8abd 206/405: Made TODO notes in algorithm, Stefan Monnier, 2019/07/13