emacs-elpa-diffs
[Top][All Lists]
Advanced

[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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]