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

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

[elpa] externals/phps-mode afc0543: Improved support for indentation wit


From: Christian Johansson
Subject: [elpa] externals/phps-mode afc0543: Improved support for indentation with mixed PHP and INLINE HTML contents
Date: Thu, 26 Sep 2019 06:14:24 -0400 (EDT)

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

    Improved support for indentation with mixed PHP and INLINE HTML contents
---
 README.md                   |  3 +++
 phps-mode-functions.el      | 58 ++++++++++++++++++++++++++++++++++-----------
 phps-mode-test-functions.el | 11 +++++++--
 phps-mode.el                |  4 ++--
 4 files changed, 58 insertions(+), 18 deletions(-)

diff --git a/README.md b/README.md
index 6e3c254..4f5fe70 100644
--- a/README.md
+++ b/README.md
@@ -31,6 +31,9 @@ This mode does not require PHP installed on your computer 
because it has a built
 * `C-c /` Comment region
 * `C-c DEL` Uncomment region
 * `C-c C-r` Rescan buffer
+* `C-c C-f` Format buffer
+* `C-c C-r` Lex buffer
+* `C-c C-p` Process buffer
 
 ## Installation
 
diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index 92b04d6..ea17a50 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -252,10 +252,12 @@
               (in-heredoc-ended-this-line nil)
               (in-inline-control-structure nil)
               (inline-html-indent 0)
+              (inline-html-indent-start 0)
               (inline-html-tag-level 0)
               (inline-html-curly-bracket-level 0)
               (inline-html-square-bracket-level 0)
               (inline-html-round-bracket-level 0)
+              (inline-html-is-whitespace nil)
               (first-token-is-inline-html nil)
               (after-special-control-structure nil)
               (after-special-control-structure-token nil)
@@ -278,7 +280,7 @@
               (nesting-start 0)
               (nesting-end 0)
               (last-line-number 0)
-              (first-token-on-line nil)
+              (first-token-on-line t)
               (line-indents (make-hash-table :test 'equal))
               (first-token-is-nesting-decrease nil)
               (token-number 1)
@@ -487,6 +489,9 @@
                 ;; Handle INLINE_HTML blocks
                 (when (equal token 'T_INLINE_HTML)
 
+                  ;; Flag whether inline-html is whitespace or not
+                  (setq inline-html-is-whitespace (string= (string-trim 
(substring string (1- token-start) (1- token-end))) ""))
+
                   (when first-token-on-line
                     (setq first-token-is-inline-html t))
 
@@ -502,9 +507,15 @@
                     (setq inline-html-square-bracket-level (nth 4 
inline-html-indents))
                     (setq inline-html-round-bracket-level (nth 5 
inline-html-indents))
 
-                    ;; Does token span several lines and is it not only 
white-space?
-                    (when (> token-end-line-number token-start-line-number)
-                      (unless (string= (string-trim (substring string (1- 
token-start) (1- token-end))) "")
+                    (phps-mode-debug-message
+                     (message "First token is inline html: %s" 
first-token-is-inline-html))
+
+                    ;; Does inline html span several lines or starts a new 
line?
+                    (when (or (> token-end-line-number token-start-line-number)
+                              first-token-is-inline-html)
+
+                      ;; Token does not only contain white-space?
+                      (unless inline-html-is-whitespace
                         (let ((token-line-number-diff token-start-line-number))
                           ;; Iterate lines here and add indents
                           (dolist (item (nth 0 inline-html-indents))
@@ -513,7 +524,7 @@
                                       first-token-is-inline-html)
                               (puthash token-line-number-diff (list item 0) 
line-indents)
                               (phps-mode-debug-message
-                                (message "Putting indent at line %s to %s from 
inline HTML" token-line-number-diff item)))
+                               (message "Putting indent at line %s to %s from 
inline HTML" token-line-number-diff item)))
                             (setq token-line-number-diff (1+ 
token-line-number-diff))))))))
 
                 ;; Keep track of when we are inside a class definition
@@ -862,6 +873,7 @@
 
                   (push alternative-control-structure-level 
switch-case-alternative-stack)))
 
+              ;; Do we have one token look-ahead?
               (when token
 
                 (phps-mode-debug-message (message "Processing token: %s" 
token))
@@ -930,8 +942,6 @@
 
                 ;; 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)
-
-                    ;; Line logic
                     (progn
 
 
@@ -943,7 +953,6 @@
                         (setq column-level-start temp-pre-indent)
                         (setq temp-pre-indent nil))
 
-
                       ;; HEREDOC lines should have zero indent
                       (when (or (and in-heredoc
                                      (not in-heredoc-started-this-line))
@@ -952,13 +961,20 @@
 
                       ;; Inline HTML should have zero indent
                       (when first-token-is-inline-html
-                        (setq column-level-start inline-html-indent))
+                        (phps-mode-debug-message
+                         (message "Setting column-level to inline HTML indent: 
%s" inline-html-indent-start))
+                        (setq column-level-start inline-html-indent-start))
 
                       ;; Save line indent
                       (phps-mode-debug-message
                         (message "Process line ending. nesting: %s-%s, 
line-number: %s-%s,     indent: %s.%s,  token: %s" nesting-start nesting-end 
token-start-line-number token-end-line-number column-level-start tuning-level 
token))
 
-                      (when (> token-start-line-number 0)
+                      (when (and (> token-start-line-number 0)
+                                 (or
+                                  (not first-token-is-inline-html)
+                                  inline-html-is-whitespace))
+                        (phps-mode-debug-message
+                         (message "Putting indent on line %s to %s at #C" 
token-start-line-number column-level-start))
                         (puthash token-start-line-number `(,column-level-start 
,tuning-level) line-indents))
 
                       ;; Support trailing indent decrements
@@ -966,7 +982,6 @@
                         (setq column-level temp-post-indent)
                         (setq temp-post-indent nil))
 
-
                       ;; Increase indentation
                       (when (and (> nesting-end 0)
                                  (or (not nesting-stack)
@@ -1006,6 +1021,8 @@
 
                           (let ((token-line-number-diff (1- (- 
token-end-line-number token-start-line-number))))
                             (while (>= token-line-number-diff 0)
+                              (phps-mode-debug-message
+                               (message "Putting indent on line %s to %s at 
#A" (- token-end-line-number token-line-number-diff) column-level-end))
                               (puthash (- token-end-line-number 
token-line-number-diff) `(,column-level-end ,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))))
@@ -1021,9 +1038,10 @@
                           (message "\nDetected token-less lines between %s and 
%s, should have indent: %s\n" token-end-line-number 
next-token-start-line-number column-level))
 
                         (let ((token-line-number-diff (1- (- 
next-token-start-line-number token-end-line-number))))
-                          (while (>= token-line-number-diff 0)
+                          (while (> token-line-number-diff 0)
+                            (phps-mode-debug-message
+                             (message "Putting indent at line %s indent %s at 
#B" (- next-token-start-line-number token-line-number-diff) column-level))
                             (puthash (- next-token-start-line-number 
token-line-number-diff) `(,column-level ,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)))))
 
 
@@ -1032,13 +1050,23 @@
 
                       ;; Set initial values for tracking first token
                       (when (> token-start-line-number last-line-number)
+                        (setq inline-html-indent-start inline-html-indent)
                         (setq first-token-on-line t)
                         (setq first-token-is-nesting-decrease nil)
                         (setq first-token-is-inline-html nil)
                         (setq in-class-declaration-level 0)
                         (setq class-declaration-started-this-line nil)
                         (setq in-heredoc-started-this-line nil)
-                        (setq special-control-structure-started-this-line 
nil)))
+                        (setq special-control-structure-started-this-line nil)
+
+                        ;; When line ends with multi-line inline-html flag 
first token as inline-html
+                        (when (and
+                               (equal token 'T_INLINE_HTML)
+                               (not inline-html-is-whitespace)
+                               (> token-end-line-number 
token-start-line-number))
+                          (phps-mode-debug-message
+                           (message "Setting first-token-is-inline-html to 
true since last token on line is inline-html and spans several lines"))
+                          (setq first-token-is-inline-html t))))
 
                   ;; Current token is not first if it's not <?php or <?=
                   (unless (or (equal token 'T_OPEN_TAG)
@@ -1052,6 +1080,8 @@
 
                     (let ((token-line-number-diff (1- (- token-end-line-number 
token-start-line-number))))
                       (while (>= token-line-number-diff 0)
+                        (phps-mode-debug-message
+                         (message "Putting indent on line %s to %s at #E" (- 
token-end-line-number token-line-number-diff) column-level))
                         (puthash (- token-end-line-number 
token-line-number-diff) `(,column-level ,tuning-level) line-indents)
                         (setq token-line-number-diff (1- 
token-line-number-diff))))
                     (setq tuning-level 0))))
diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el
index af03831..2abc3d4 100644
--- a/phps-mode-test-functions.el
+++ b/phps-mode-test-functions.el
@@ -274,7 +274,7 @@
     "<html>\n    <head>\n        <?php echo $title; ?>\n    </head>\n    
<body>\n    <?php\n\n    if ($myTest) {\n        doSomething();\n    }\n\n    
?>\n    </body>\n</html>"
     "A mixed HTML and PHP file."
     ;; (message "Indent: %s" (phps-mode-test-hash-to-list 
(phps-mode-functions-get-lines-indent)))
-    (should (equal '((1 (0 0)) (2 (1 0)) (3 (0 0)) (4 (1 0)) (5 (1 0)) (6 (0 
0)) (7 (0 0)) (8 (0 0)) (9 (1 0)) (10 (0 0)) (11 (0 0)) (12 (0 0)) (13 (1 0)) 
(14 (0 0))) (phps-mode-test-hash-to-list 
(phps-mode-functions-get-lines-indent)))))
+    (should (equal '((1 (0 0)) (2 (1 0)) (3 (2 0)) (4 (1 0)) (5 (1 0)) (6 (2 
0)) (7 (0 0)) (8 (0 0)) (9 (1 0)) (10 (0 0)) (11 (0 0)) (12 (0 0)) (13 (1 0)) 
(14 (0 0))) (phps-mode-test-hash-to-list 
(phps-mode-functions-get-lines-indent)))))
    )
 
   )
@@ -844,6 +844,13 @@
    (let ((buffer-contents (buffer-substring-no-properties (point-min) 
(point-max))))
      (should (equal buffer-contents "<?php\nif (empty($this->var)):\n    
$this->var = 'abc123';\nendif;"))))
 
+  (phps-mode-test-with-buffer
+   "<html>\n<head>\n<title><?php echo $title; 
?></title>\n</head>\n<body>\n<div class=\"contents\"><?php echo $body; 
?></div>\n</body>\n</html>"
+   "A mixed HTML and PHP file, each PHP command is inside HTML markup"
+   (indent-region (point-min) (point-max))
+   (let ((buffer-contents (buffer-substring-no-properties (point-min) 
(point-max))))
+     (should (equal buffer-contents "<html>\n    <head>\n        <title><?php 
echo $title; ?></title>\n    </head>\n    <body>\n        <div 
class=\"contents\"><?php echo $body; ?></div>\n    </body>\n</html>"))))
+
   )
 
 (defun phps-mode-test-functions-imenu ()
@@ -1002,7 +1009,7 @@
                    0
                    0
                    ))))
-  
+
   )
 
 (defun phps-mode-test-functions ()
diff --git a/phps-mode.el b/phps-mode.el
index 67a3faf..943ad11 100644
--- a/phps-mode.el
+++ b/phps-mode.el
@@ -5,8 +5,8 @@
 ;; Author: Christian Johansson <address@hidden>
 ;; Maintainer: Christian Johansson <address@hidden>
 ;; Created: 3 Mar 2018
-;; Modified: 16 Sep 2019
-;; Version: 0.3.1
+;; Modified: 26 Sep 2019
+;; Version: 0.3.2
 ;; Keywords: tools, convenience
 ;; URL: https://github.com/cjohansson/emacs-phps-mode
 



reply via email to

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