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

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

[elpa] externals/phps-mode c678fc1 209/405: New algorithm handles HEREDO


From: Stefan Monnier
Subject: [elpa] externals/phps-mode c678fc1 209/405: New algorithm handles HEREDOC indentation
Date: Sat, 13 Jul 2019 10:00:14 -0400 (EDT)

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

    New algorithm handles HEREDOC indentation
---
 docs/indentation-algorithm.md | 15 +++++++++++++++
 phps-mode-functions.el        | 29 +++++++++++++++++++----------
 phps-mode-test-functions.el   |  7 +++++++
 3 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/docs/indentation-algorithm.md b/docs/indentation-algorithm.md
index 42667c7..9d41b0c 100644
--- a/docs/indentation-algorithm.md
+++ b/docs/indentation-algorithm.md
@@ -18,6 +18,14 @@ Here follows pseudo-code for a algorithm that calculates 
indentation for each li
 ```php
 foreach token in buffer:
     
+    if token = T_START_HEREDOC:
+        in-heredoc = true;
+        in-heredoc-started-this-line = true;
+    elseif token == T_END_HEREDOC:
+        in-heredoc = false;
+        in-heredoc-ended-this-line = true;
+    endif;
+    
     calculate nesting-end;
     
     if nesting-stack AND nesting-end <= nesting-stack-start: // #decrease
@@ -43,6 +51,10 @@ foreach token in buffer:
             indent-start = temp-pre-indent;
         endif;
         
+        if (in-heredoc AND !in-heredoc-started-this-line) OR 
in-heredoc-ended-this-line:
+            indent-start = 0;
+        endif;
+        
         save line indent-start; // #save
         
         if temp-post-indent: #temp-post-indent
@@ -57,6 +69,9 @@ foreach token in buffer:
             push (nesting-stack-end nesting-end) to stack;
             indent++;
         endif;
+        
+        in-heredoc-started-this-line = false;
+        in-heredoc-ended-this-line = false;
     endif;
     
 endforeach;
diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index 2f9974c..64545f0 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -52,6 +52,8 @@
           (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-heredoc-started-this-line nil)
+              (in-heredoc-ended-this-line nil)
               (in-inline-control-structure nil)
               (after-special-control-structure nil)
               (after-special-control-structure-token nil)
@@ -312,8 +314,7 @@
                         (progn
                           (setq in-assignment nil)
                             (setq in-assignment-level 0))
-                      (when (and first-token-on-line
-                                 (not in-heredoc))
+                      (when first-token-on-line
                         (setq in-assignment-level 1)
                         ;; (message "In assignment on new-line at %s" token)
                         ))
@@ -329,12 +330,6 @@
                   (setq after-extra-special-control-structure t)
                   (setq after-extra-special-control-structure-first-on-line 
first-token-on-line))
 
-                ;; Keep track of whether we are inside a HEREDOC or NOWDOC
-                (when (equal token 'T_START_HEREDOC)
-                  (setq in-heredoc t))
-                (when (equal token 'T_END_HEREDOC)
-                  (setq in-heredoc nil))
-
                 )
 
               (when token
@@ -342,6 +337,14 @@
                 ;; Calculate nesting
                 (setq nesting-end (+ round-bracket-level square-bracket-level 
curly-bracket-level alternative-control-structure-level in-assignment-level 
in-class-declaration-level))
 
+                ;; Keep track of whether we are inside a HEREDOC or NOWDOC
+                (when (equal token 'T_START_HEREDOC)
+                  (setq in-heredoc t)
+                  (setq in-heredoc-started-this-line t))
+                (when (equal token 'T_END_HEREDOC)
+                  (setq in-heredoc nil)
+                  (setq in-heredoc-ended-this-line t))
+
                 ;; Has nesting increased?
                 (when (and nesting-stack
                            (<= nesting-end (car (car nesting-stack))))
@@ -397,6 +400,11 @@
                         (setq temp-pre-indent nil))
 
 
+                      ;; HEREDOC lines should have zero indent
+                      (when (or (and in-heredoc
+                                     (not in-heredoc-started-this-line))
+                                in-heredoc-ended-this-line)
+                        (setq column-level-start 0))
 
                       ;; Save line indent
                       
@@ -422,7 +430,7 @@
 
                         (let ((token-line-number-diff (1- (- 
token-end-line-number token-start-line-number))))
                           (while (>= token-line-number-diff 0)
-                            (puthash (- token-end-line-number 
token-line-number-diff) `(,column-level ,tuning-level) line-indents)
+                            (puthash (- token-end-line-number 
token-line-number-diff) `(,column-level-start ,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))))
 
@@ -434,7 +442,7 @@
                         (setq column-level temp-post-indent)
                         (setq temp-post-indent nil))
 
-                      ;; Decrease indentation
+                      ;; Increase indentation
                       (when (and (> nesting-end 0)
                                  (or (not nesting-stack)
                                      (> nesting-end (car (cdr (car 
nesting-stack))))))
@@ -482,6 +490,7 @@
                         (setq in-assignment-started-this-line nil)
                         (setq changed-nesting-stack-in-line nil)
                         (setq class-declaration-started-this-line nil)
+                        (setq in-heredoc-started-this-line nil)
                         (setq special-control-structure-started-this-line 
nil)))
 
                   ;; Current token is not first
diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el
index c5c3363..c94606b 100644
--- a/phps-mode-test-functions.el
+++ b/phps-mode-test-functions.el
@@ -57,6 +57,13 @@
    "Round and square bracket expressions"
    (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (1 0)) (5 (2 0)) (6 (1 
0)) (7 (1 0)) (8 (0 0))) (phps-mode-test-functions--hash-to-list 
(phps-mode-functions-get-lines-indent)))))
 
+    (phps-mode-test-with-buffer
+   "<?php\nvar_dump(array(<<<EOD\nfoobar!\nEOD\n));\n?>"
+   "HEREDOC in arguments example"
+   ;; (message "Tokens: %s" phps-mode-lexer-tokens)
+   (should (equal '((1 (0 0)) (2 (0 0)) (3 (0 0)) (4 (0 0)) (5 (0 0)) (6 (0 
0))) (phps-mode-test-functions--hash-to-list 
(phps-mode-functions-get-lines-indent)))))
+
+
   (phps-mode-test-with-buffer
    "<?php\n$str = <<<'EOD'\nExample of string\nspanning multiple lines\nusing 
nowdoc syntax.\nEOD;\n"
    "Multi-line NOWDOC string"



reply via email to

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