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

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

[elpa] externals/phps-mode e37c1cf 150/405: New indention calculation wo


From: Stefan Monnier
Subject: [elpa] externals/phps-mode e37c1cf 150/405: New indention calculation works with doc-comments
Date: Sat, 13 Jul 2019 10:00:02 -0400 (EDT)

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

    New indention calculation works with doc-comments
---
 phps-mode-functions.el      |  33 ++++----
 phps-mode-test-functions.el | 192 ++------------------------------------------
 2 files changed, 23 insertions(+), 202 deletions(-)

diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index 64d93af..290d643 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -75,10 +75,11 @@
             (let* ((token (car item))
                    (token-start (car (cdr item)))
                    (token-end (cdr (cdr item)))
-                   (token-line-number (line-number-at-pos token-start t)))
+                   (token-start-line-number (line-number-at-pos token-start t))
+                   (token-end-line-number (line-number-at-pos token-end t)))
 
-              ;; Are we on a new line?
-              (if (or (> token-line-number last-line-number)
+              ;; Are we on a new line or are we are last token?
+              (if (or (> token-start-line-number last-line-number)
                       (= token-number last-token-number))
                   (progn
 
@@ -98,17 +99,22 @@
                       (when first-token-is-nesting-increase
                         (setq column-level (1+ column-level))))
                     
-                    ;; Increase indent with one space inside doc-comment
-                    (if in-doc-comment
-                        (setq tuning-level 1)
-                      (setq tuning-level 0))
-
                     (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)
+                      (when (equal token 'T_DOC_COMMENT)
+                        (setq tuning-level 1))
+                      (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)
+                          (setq token-line-number-diff (1- 
token-line-number-diff))))
+                      (setq tuning-level 0))
+
                     ;; 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
@@ -247,13 +253,6 @@
               (when (equal token 'T_CLOSE_TAG)
                 (setq in-scripting nil))
 
-              ;; Keep track of whether we are inside a doc-comment
-              (when (equal token 'T_DOC_COMMENT)
-                (setq in-doc-comment token-end))
-              (when (and in-doc-comment
-                         (> token-start in-doc-comment))
-                (setq in-doc-comment nil))
-
               ;; Keep track of whether we are inside a HEREDOC or NOWDOC
               (when (equal token 'T_START_HEREDOC)
                 (setq in-heredoc t))
@@ -261,10 +260,10 @@
                 (setq in-heredoc nil))
 
               ;; Are we on a new line?
-              (when (> token-line-number last-line-number)
+              (when (> token-start-line-number last-line-number)
 
                 ;; Update last line number
-                (setq last-line-number token-line-number))
+                (setq last-line-number token-start-line-number))
 
               (setq token-number (1+ token-number))))
 
diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el
index 549d727..c9489ac 100644
--- a/phps-mode-test-functions.el
+++ b/phps-mode-test-functions.el
@@ -43,20 +43,20 @@
      (lambda (k v)
        (push (list k v) result))
      hash-table)
-    (nreverse result)))
+    (sort (nreverse result) (lambda (a b) (< (car a) (car b))))))
 
 (defun phps-mode-test-functions-get-lines-indent ()
   "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 {\n        echo $title2;\n        echo 
$title3;\n    }\n} ?></title><body>Bla bla</body></html>"
+   "<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>"
    "Mixed HTML/PHP"
-   (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (2 0)) (5 (1 0)) (6 (2 
0)) (7 (2 0)) (8 (1 0)) (9 (0 0))) (phps-mode-test-functions--hash-to-list 
(phps-mode-functions-get-lines-indent)))))
+   (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)))))
 
   (phps-mode-test-with-buffer
-   "<?php\nif (true):\n    echo 'Something';\nelse:\n    echo 'Something 
else';\n    'Something else again';\nendif;\necho true;\n"
+   "<?php\nif (true):\n    echo 'Something';\nelseif (true):\n    echo 
'Something';\nelse:\n    echo 'Something else';\n    echo 'Something else 
again';\nendif;\necho true;\n"
    "Alternative control structures"
-   (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (0 0)) (5 (1 0)) (6 (1 
0)) (7 (0 0)) (8 (0 0))) (phps-mode-test-functions--hash-to-list 
(phps-mode-functions-get-lines-indent)))))
+   (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (0 0)) (5 (1 0)) (6 (0 
0)) (7 (1 0)) (8 (1 0)) (9 (0 0)) (10 (0 0))) 
(phps-mode-test-functions--hash-to-list 
(phps-mode-functions-get-lines-indent)))))
 
   (phps-mode-test-with-buffer
    "<?php\nif (true)\n    echo 'Something';\nelse\n    echo 'Something 
else';\necho true;\n"
@@ -64,9 +64,9 @@
    (should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (0 0)) (5 (1 0)) (6 (0 
0))) (phps-mode-test-functions--hash-to-list 
(phps-mode-functions-get-lines-indent)))))
 
   (phps-mode-test-with-buffer
-   "<?php\n/**\n* Bla\n*/"
+   "<?php\n/**\n * Bla\n */"
    "DOC-COMMENT"
-   (should (equal '((1 (0 0)) (2 (0 0)) (3 (0 1))) 
(phps-mode-test-functions--hash-to-list 
(phps-mode-functions-get-lines-indent)))))
+   (should (equal '((1 (0 0)) (2 (0 0)) (3 (0 1)) (4 (0 1))) 
(phps-mode-test-functions--hash-to-list 
(phps-mode-functions-get-lines-indent)))))
 
   ;; TODO round and square bracket expressions
 
@@ -303,189 +303,11 @@
 
   )
 
-;; TODO Remove this functions
-(defun phps-mode-test-functions-get-current-line-data ()
-  "Return information about point in tokens."
-
-  (phps-mode-test-with-buffer
-   "<?php\nNAMESPACE MyNameSpace;\nCLASS MyClass {\n\tpublic function 
__construct() {\n\t\texit;\n\t}\n}\n"
-   (goto-char 35)
-   (should (equal (list (list t 0 0 0 0 0 3 nil) (list t 1 0 0 0 0 6 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<html><head><title><?php echo $title; ?></title><body>Bla 
bla</body></html>"
-   nil
-   (goto-char 15)
-   (should (equal (list (list nil 0 0 0 0 0 nil nil) (list nil 0 0 0 0 0 5 
nil)) (phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<html><head><title><?php echo $title; ?>\n</title><body>Bla 
bla</body></html>"
-   (goto-char 50)
-   (should (equal (list (list nil 0 0 0 0 0 nil nil) (list nil 0 0 0 0 0 nil 
nil)) (phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<html><head><title></title><body>Bla bla</body></html>"
-   nil
-   (goto-char 15)
-   (should (equal (list (list nil 0 0 0 0 0 nil nil) (list nil 0 0 0 0 0 nil 
nil)) (phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<html><head><title><?php echo $title; ?></title><body>Bla 
bla</body></html>"
-   (goto-char 30)
-   (should (equal (list (list nil 0 0 0 0 0 nil nil) (list nil 0 0 0 0 0 5 
nil)) (phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<html><head><title><?php echo $title; ?></title><body>Bla 
bla</body></html>"
-   nil
-   (goto-char 50)
-   (should (equal (list (list nil 0 0 0 0 0 nil nil) (list nil 0 0 0 0 0 5 
nil)) (phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<html><head><title><?php if ($myCondition) { \n   if ($mySeconCondition) { 
echo $title; } } ?></title><body>Bla bla</body></html>"
-   (goto-char 48)
-   (should (equal (list (list t 1 0 0 0 0 5 nil) (list nil 0 0 0 0 0 17 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<html><head><title><?php if ($myCondition) { if ($mySeconCondition) {\n 
echo $title;\n} } ?></title><body>Bla bla</body></html>"
-   nil
-   (goto-char 72)
-   (should (equal (list (list t 2 0 0 0 0 10 nil) (list t 2 0 0 0 0 13 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) 
{\necho $title;\n}\n}\n ?></title><body>Bla bla</body></html>"
-   (goto-char 84)
-   (should (equal (list (list t 2 0 0 0 0 13 nil) (list t 1 0 0 0 0 14 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<html><head><title><?php if ($myCondition) { if ($mySeconCondition) { echo 
$title; } } ?></title><body>Bla bla</body></html>"
-   nil
-   (goto-char 100)
-   (should (equal (list (list nil 0 0 0 0 0 nil nil) (list nil 0 0 0 0 0 17 
nil)) (phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php /**\n * My first line\n * My second line\n **/"
-   (goto-char 20)
-   (should (equal (list (list t 0 0 0 0 0 nil t) (list t 0 0 0 0 0 nil t)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php /**\n * My first line\n * My second line\n **/"
-   nil
-   (goto-char 9)
-   (should (equal (list (list nil 0 0 0 0 0 nil nil) (list t 0 0 0 0 0 1 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php /**\n * My first line\n * My second line\n **/"
-   (goto-char 50)
-   (should (equal (list (list t 0 0 0 0 0 nil t) (list t 0 0 0 0 0 nil t)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\n$variable = array(\n'random4');\n$variable = true;\n"
-   nil
-   (goto-char 29)
-   (should (equal (list (list t 0 1 0 0 0 4 nil) (list t 0 0 0 0 0 7 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\nif (empty(\n$this->var\n) && !empty($this->var)\n) {\n$this->var = 
'abc123';\n}\n"
-   (goto-char 54)
-   (should (equal (list (list t 0 1 0 0 0 16 nil) (list t 1 0 0 0 0 18 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\n$var = [\n    'random' => [\n        'hello',\n    ],\n];\n"
-   nil
-   (goto-char 46)
-   (should (equal (list (list t 0 0 2 0 0 6 nil) (list t 0 0 2 0 0 8 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  ;; INLINE SYNTAX FOR CONTROL STRUCTURES
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition)\n    echo 'was here';\necho 'was here 2';\n"
-   (goto-char 41)
-   (should (equal (list (list t 0 0 0 1 0 4 nil) (list t 0 0 0 0 0 7 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition)\n    echo 'was here';\necho 'was here 2';\n"
-   nil
-   (goto-char 60)
-   (should (equal (list (list t 0 0 0 0 0 7 nil) (list t 0 0 0 0 0 10 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition) echo 'was here'; echo 'was here 2';\n"
-   (goto-char 32)
-   (should (equal (list (list t 0 0 0 0 0 0 nil) (list t 0 0 0 0 0 10 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition) echo 'was here'; echo 'was here 2';\n"
-   nil
-   (goto-char 55)
-   (should (equal (list (list t 0 0 0 0 0 0 nil) (list t 0 0 0 0 0 10 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition)\n    echo 'was here';\nelse\n    echo 'was here 
2';\n"
-   (goto-char 47)
-   (should (equal (list (list t 0 0 0 0 0 7 nil) (list t 0 0 0 0 0 8 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition)\n    echo 'was here';\nelse\n    echo 'was here 
2';\n"
-   nil
-   (goto-char 57)
-   (should (equal (list (list t 0 0 0 1 0 8 nil) (list t 0 0 0 0 0 11 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition)\n    echo 'was here';\nelse\n    echo 'was here 
2';\n"
-   (goto-char 55)
-   (should (equal (list (list t 0 0 0 1 0 8 nil) (list t 0 0 0 0 0 11 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  ;; ALTERNATIVE SYNTAX FOR CONTROL STRUCTURES
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition):\n    echo 'was here';\nendif;\necho 'was here 
2';\n"
-   nil
-   (goto-char 41)
-   (should (equal (list (list t 0 0 0 0 1 5 nil) (list t 0 0 0 0 1 8 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition):\n    echo 'was here';\nendif;\necho 'was here 
3';\n"
-   (goto-char 52)
-   (should (equal (list (list t 0 0 0 0 0 8 nil) (list t 0 0 0 0 0 10 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition):    echo 'was here';\nendif;\necho 'was here 
4';\n"
-   nil
-   (goto-char 32)
-   (should (equal (list (list t 0 0 0 0 0 0 nil) (list t 0 0 0 0 1 8 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition): echo 'was here'; endif; echo 'was here 5';\n"
-   (goto-char 35)
-   (should (equal (list (list t 0 0 0 0 0 0 nil) (list t 0 0 0 0 0 13 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition):\n    echo 'was here';\nelse:\n    echo 'was here 
2';\nendif;\n"
-   nil
-   (goto-char 44)
-   (should (equal (list (list t 0 0 0 0 1 5 nil) (list t 0 0 0 0 1 8 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition):\n    echo 'was here';\nelse:\n    echo 'was here 
2';\nendif;\n"
-   (goto-char 64)
-   (should (equal (list (list t 0 0 0 0 1 10 nil) (list t 0 0 0 0 1 13 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  (phps-mode-test-with-buffer
-   "<?php\nif ($myCondition):\n    echo 'was here';\nelse:\n    echo 'was here 
2';\nendif;\n"
-   nil
-   (goto-char 79)
-   (should (equal (list (list t 0 0 0 0 0 10 nil) (list t 0 0 0 0 0 15 nil)) 
(phps-mode-functions-get-current-line-data))))
-
-  ;; TODO SWITCH, CASE, DEFAULT AS WELL
-  
-  )
-
 ;; TODO Add tests for all examples here: https://www.php-fig.org/psr/psr-2/
 
 (defun phps-mode-test-functions ()
   "Run test for functions."
   (phps-mode-test-functions-get-lines-indent)
-  (phps-mode-test-functions-get-current-line-data)
   (phps-mode-test-functions-indent-line))
 
 (phps-mode-test-functions)



reply via email to

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