branch: externals/phpinspect
commit 64b400a852c79521f943021b00b125fea8264daf
Author: Hugo Thunnissen <de...@hugot.nl>
Commit: Hugo Thunnissen <de...@hugot.nl>

    Require region to be whitespace-only before skipping over it while parsing
---
 phpinspect-parser.el           | 27 ++++++++++++++----
 test/phpinspect-test.el        |  1 +
 test/test-buffer-indexation.el |  2 +-
 test/test-buffer-parsing.el    | 65 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 89 insertions(+), 6 deletions(-)

diff --git a/phpinspect-parser.el b/phpinspect-parser.el
index 07c617a456..97a8665fb6 100644
--- a/phpinspect-parser.el
+++ b/phpinspect-parser.el
@@ -145,6 +145,7 @@ text at point and returns the resulting token."
     (catch 'phpinspect--return
       ;; Use while loop instead of recursion for better performance.
       (while t
+        ;;(message "looping")
         ;; Set point-offset-base for more efficient execution of
         ;; `phpinspect-meta-start' and related functions.
         (dlet ((phpinspect-meta--point-offset-base original-point))
@@ -180,27 +181,43 @@ text at point and returns the resulting token."
               ;; Override point-offset-base again, but this time for
               ;; right-sibling
               (dlet ((phpinspect-meta--point-offset-base nil))
+                ;; Check if token is really eligible to recycle
                 (if-let (((not (and delimiter-predicate (funcall 
delimiter-predicate token))))
-
                          (right-sibling right-sibling)
                          ((setq phpinspect-meta--point-offset-base
                                 (+ original-point (- 
(phpinspect-meta-parent-offset right-sibling)
                                                      parent-offset))))
-                         ((not (phpi-change-tainted-region-p
-                                change current-end-position (+ delta 
(phpinspect-meta-start right-sibling)))))
+                         (right-sibling-start-position (+ delta 
(phpinspect-meta-start right-sibling)))
+
+                         ;; Skipped-over region was not tainted (no edits have 
taken place in the region)
+                         ((not (phpi-change-tainted-region-p change 
current-end-position right-sibling-start-position)))
+
+                         ;; Skipped-over region was only whitespace (if it
+                         ;; wasn't, some unparsed character sequence that might
+                         ;; now have meaning is in the region, and we don't 
want
+                         ;; to disregard that.)
+                         ((not (> (- right-sibling-start-position 
current-end-position)
+                                  (length (phpinspect-meta-whitespace-before 
right-sibling)))))
+                         ;; Skip over whitespace
                          ((progn
                             (goto-char (+ delta (phpinspect-meta-start 
right-sibling)))
                             (phpinspect-pctx-register-whitespace context 
(phpinspect-meta-whitespace-before right-sibling))
-
+                            ;; (message "point: %d, Looking at: '%s'"
+                            ;;          (point) (buffer-substring (point) (min 
(+ (point) 10) (point-max))))
                             (if continue-condition (funcall 
continue-condition) t))))
+                    (progn
                       ;;(message "using sibling %s" (phpinspect-meta-string 
right-sibling))
                       ;; There was a right sibling and it is eligible for
                       ;; re-use. Set token-meta and "recurse".
                       (setq first-iteration nil
                             token-meta right-sibling
                             point (+ delta (phpinspect-meta-start 
right-sibling))
-                            original-point (phpinspect-meta-start 
right-sibling))
+                            original-point (phpinspect-meta-start 
right-sibling)))
+
+                  ;;(message "EXITING")
 
+                  ;; Go to end of last recycled token
+                  (goto-char current-end-position)
                   ;; No eligible right sibling, break.
                   (throw 'phpinspect--return tokens-rear))))))))))
 
diff --git a/test/phpinspect-test.el b/test/phpinspect-test.el
index 423f552d97..8f88a3779a 100644
--- a/test/phpinspect-test.el
+++ b/test/phpinspect-test.el
@@ -323,6 +323,7 @@ class FlufferUpper
 (load-file (concat phpinspect-test-directory "/test-project.el"))
 (load-file (concat phpinspect-test-directory "/test-buffer.el"))
 (load-file (concat phpinspect-test-directory "/test-buffer-indexation.el"))
+(load-file (concat phpinspect-test-directory "/test-buffer-parsing.el"))
 (load-file (concat phpinspect-test-directory "/test-index.el"))
 (load-file (concat phpinspect-test-directory "/test-typedef.el"))
 (load-file (concat phpinspect-test-directory "/test-type.el"))
diff --git a/test/test-buffer-indexation.el b/test/test-buffer-indexation.el
index 1e4cb23366..ef06db4fee 100644
--- a/test/test-buffer-indexation.el
+++ b/test/test-buffer-indexation.el
@@ -1,6 +1,6 @@
 ;;; test-buffer-indexation.el --- Tests for buffer indexation  -*- 
lexical-binding: t; -*-
 
-;; Copyright (C) 2024  Hugo Thunnissen
+;; Copyright (C) 2021-2023 Free Software Foundation, Inc.
 
 ;; Author: Hugo Thunnissen <de...@hugot.nl>
 ;; Keywords:
diff --git a/test/test-buffer-parsing.el b/test/test-buffer-parsing.el
new file mode 100644
index 0000000000..3af3ed1457
--- /dev/null
+++ b/test/test-buffer-parsing.el
@@ -0,0 +1,65 @@
+;;; test-buffer-parsing.el --- tests for parsing of buffers  -*- 
lexical-binding: t; -*-
+
+;; Copyright (C) 2025 Free Software Foundation, Inc.
+
+;; Author: Hugo Thunnissen <de...@hugot.nl>
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;
+
+;;; Code:
+
+
+(require 'phpinspect-buffer)
+(require 'phpinspect-test-env
+         (expand-file-name "phpinspect-test-env.el"
+                           (file-name-directory (macroexp-file-name))))
+
+(ert-deftest phpinspect-parse-nested-function-incrementally ()
+  (with-temp-buffer
+    (let ((buffer (phpinspect-claim-buffer (current-buffer) 
(phpinspect--make-dummy-project))))
+      (insert "class C { private ?\\DateTime $d; public function __construct() 
{
+
+
+        return $foo()->bar->baz;
+    }
+
+    // FIXME: Make this test succeed with \"public\" uncommented
+    /* public */ function fooBar(RealTime $rt) {
+        // stuff
+    }
+}")
+      (should (equal (phpinspect-parse-string (buffer-string)) 
(phpinspect-buffer-parse buffer)))
+
+      ;; goto first line of method block
+      (goto-char 66)
+      (insert "$foo")
+      (insert " ")
+      (insert "= ")
+      (insert "function (): {\n\n}")
+      (insert ";")
+
+      (should (equal (phpinspect-parse-string (buffer-string)) 
(phpinspect-buffer-parse buffer)))
+
+      (goto-char 86)
+      (insert " ")
+      (insert "use ")
+      (insert "(")
+      (should (equal (phpinspect-parse-string (buffer-string)) 
(phpinspect-buffer-parse buffer)))
+      (insert "$d")
+      (insert ")")
+      (should (equal (phpinspect-parse-string (buffer-string)) 
(phpinspect-buffer-parse buffer))))))

Reply via email to