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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/phps-mode 2a1ed52 196/405: Optimized algorithm for inde


From: Stefan Monnier
Subject: [elpa] externals/phps-mode 2a1ed52 196/405: Optimized algorithm for indentation
Date: Sat, 13 Jul 2019 10:00:12 -0400 (EDT)

branch: externals/phps-mode
commit 2a1ed52f4ce86ca3d1059c559fa50632a49ca375
Author: Christian Johansson <address@hidden>
Commit: Christian Johansson <address@hidden>

    Optimized algorithm for indentation
---
 docs/indentation-algorithm.md | 44 +++++++++++++++++++++---------
 phps-mode-functions.el        | 63 +++++++++++++++++--------------------------
 2 files changed, 56 insertions(+), 51 deletions(-)

diff --git a/docs/indentation-algorithm.md b/docs/indentation-algorithm.md
index 1c59e8e..89147ea 100644
--- a/docs/indentation-algorithm.md
+++ b/docs/indentation-algorithm.md
@@ -20,20 +20,16 @@ foreach token in buffer:
 
     calculate nesting-end;
 
-    if nesting-stack AND nesting-end <= nesting-stack-end:
+    if nesting-stack AND nesting-end <= nesting-stack-start: // #decrease
         pop stack;
-        indentation--;
+        indent--;
     endif;
 
     if we reached end of a line:
-        if nesting-stack AND nesting-end <= nesting-stack-start:
-            pop stack;
-            indent--;
-        endif;
 
-        save line indent;
+        save line indent; // #save
 
-        if nesting-end > 0 AND (!nesting-stack OR nesting-end > 
nesting-stack-end):
+        if nesting-end > 0 AND (!nesting-stack OR nesting-end > 
nesting-stack-end): // #increase
             if !nesting-stack:
                 nesting-stack-end = 0;
             endif;
@@ -49,9 +45,33 @@ endforeach;
 ## Examples
 
 ```php
-If (function( <- (1)
-    false)
-) { <- (3, 1)
+if (function(          // #save indent: 0, #increase push (0 2) indent: 1
+    false)                     // #save indent: 1
+) {                            // #decrease pop (0 2) indent: 0, #save indent: 
0, #increase push (0 1) indent: 1
+    echo true;         // #save indent: 1
+}                                      // #decrease pop (0 1) indent: 0, #save 
indent: 0
+```
+
+## Inline control structure for if-else
+
+```php
+if (true)
+    echo true;
+else
+    echo false;
+```
+
+## Alternative control structure for if-else 2
+
+```php
+if (true &&
+    true
+):
     echo true;
-} <- (3)
+elseif (true
+    || false):
+    echo 'another';
+else:
+    echo false;
+endif;
 ```
diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index ddf0270..746608f 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -280,7 +280,6 @@
                 (when (and in-inline-control-structure
                            (string= token ";")
                            (not special-control-structure-started-this-line))
-                  (setq column-level (1- column-level))
                   (setq line-contained-nesting-decrease t)
                   (setq inline-control-structure-level (1- 
inline-control-structure-level))
                   (setq in-inline-control-structure nil))
@@ -345,6 +344,29 @@
                 )
 
               (when token
+                
+                ;; Calculate nesting
+                (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))
+
+                ;; Has nesting increased?
+                (when (and nesting-stack
+                           (<= nesting-end (car (car nesting-stack))))
+
+                  (when phps-mode-functions-verbose
+                    ;; (message "\nPopping %s from nesting-stack since %s is 
lesser or equal to %s, next value is: %s\n" (car nesting-stack) nesting-end 
(car (car nesting-stack)) (nth 1 nesting-stack))
+                    )
+                  (pop nesting-stack)
+
+                  ;; Decrement column
+                  (if allow-custom-column-decrement
+                      (progn
+                        (setq column-level (- column-level (- nesting-start 
nesting-end)))
+                        (setq allow-custom-column-increment nil))
+                    (setq column-level (1- column-level)))
+
+                  ;; Prevent negative column-values
+                  (when (< column-level 0)
+                    (setq column-level 0)))
 
                 ;; 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)
@@ -352,30 +374,6 @@
                     ;; Line logic
                     (progn
 
-                      ;; Calculate nesting
-                      (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))
-
-                      ;; Has nesting increased?
-                      (when (and nesting-stack
-                                 (<= nesting-end (car (car nesting-stack))))
-
-                        (when phps-mode-functions-verbose
-                          ;; (message "\nPopping %s from nesting-stack since 
%s is lesser or equal to %s, next value is: %s\n" (car nesting-stack) 
nesting-end (car (car nesting-stack)) (nth 1 nesting-stack))
-                          )
-                        (pop nesting-stack)
-
-                        ;; Decrement column
-                        (if allow-custom-column-decrement
-                            (progn
-                              (setq column-level (- column-level (- 
nesting-start nesting-end)))
-                              (setq allow-custom-column-increment nil))
-                          (setq column-level (1- column-level)))
-
-                        ;; Prevent negative column-values
-                        (when (< column-level 0)
-                          (setq column-level 0)))
-
-
                       ;; ;; Start indentation might differ from ending 
indentation in cases like } else {
                       (setq column-level-start column-level)
 
@@ -446,7 +444,7 @@
                             (setq column-level (1+ column-level)))
 
                           (when phps-mode-functions-verbose
-                            ;; (message "\nPushing (%s %s) to nesting-stack 
since %s is greater than %s or stack is empty" nesting-start nesting-end 
nesting-end (car (cdr (car nesting-stack))))
+                            (message "\nPushing (%s %s %s) to nesting-stack 
since %s is greater than %s or stack is empty" nesting-start nesting-end token 
nesting-end (car (cdr (car nesting-stack))))
                             )
                           (push `(,nesting-stack-end ,nesting-end ,token) 
nesting-stack)
                           (when phps-mode-functions-verbose
@@ -483,19 +481,6 @@
                   ;; Current token is not first
                   (setq first-token-on-line nil)
 
-                  ;; Calculate nesting
-                  (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))
-
-                  ;; Is current nesting-level equal or below stack-value? (#0)
-                  (when (and nesting-stack
-                             (<= nesting-end (car (car nesting-stack))))
-                    (setq column-level (1- column-level))
-                    (when phps-mode-functions-verbose
-                      ;; (message "\nPopping %s from nesting-stack since %s is 
lesser somewhere on line, next is: %s, new column is: %s, new stack-value is: 
%s\n" (car nesting-stack) nesting-end (nth 1 nesting-stack) column-level (nth 1 
nesting-stack))
-                      )
-                    (pop nesting-stack)
-                    (setq changed-nesting-stack-in-line t))
-
                   (when (> token-end-line-number token-start-line-number)
                     ;; (message "Token not first on line %s starts at %s and 
ends at %s" token token-start-line-number token-end-line-number)
                     (when (equal token 'T_DOC_COMMENT)



reply via email to

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