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

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

[elpa] externals/phpinspect 25ae878cfc 3/6: Implement indexation of func


From: ELPA Syncer
Subject: [elpa] externals/phpinspect 25ae878cfc 3/6: Implement indexation of functions nested within blocks/lists
Date: Tue, 20 Aug 2024 15:58:51 -0400 (EDT)

branch: externals/phpinspect
commit 25ae878cfcdd881f1902e3033f7470730256f8a5
Author: Hugo Thunnissen <devel@hugot.nl>
Commit: Hugo Thunnissen <devel@hugot.nl>

    Implement indexation of functions nested within blocks/lists
    
    - This fixes a bug that caused laravel helper functions not to be indexed,
    unless you were to open them in a buffer (the way in which live buffers are
    indexed is more sophisticated and it already indexed these functions).
---
 phpinspect-index.el            | 25 ++++++++++++++++++-------
 phpinspect-resolve.el          |  4 ----
 phpinspect-token-predicates.el |  5 ++++-
 test/test-index.el             | 26 ++++++++++++++++++++++++++
 4 files changed, 48 insertions(+), 12 deletions(-)

diff --git a/phpinspect-index.el b/phpinspect-index.el
index de032a297b..780fc1b7cc 100644
--- a/phpinspect-index.el
+++ b/phpinspect-index.el
@@ -544,18 +544,29 @@ NAMESPACE will be assumed the root namespace if not 
provided"
          namespaces type-resolver-factory location-resolver indexed))
     indexed))
 
-(defun phpinspect--index-functions-in-tokens (tokens type-resolver-factory 
&optional imports namespace add-used-types)
+
+
+(defun phpinspect--index-functions-in-tokens
+    (tokens type-resolver-factory &optional imports namespace add-used-types 
type-resolver)
   "Index functions in TOKENS."
-  (let ((type-resolver (funcall type-resolver-factory imports nil namespace))
-        comment-before functions)
+  (setq type-resolver (or type-resolver (funcall type-resolver-factory imports 
nil namespace)))
+  (let (comment-before functions function)
     (dolist (token tokens)
       (cond ((phpinspect-comment-p token)
              (setq comment-before token))
             ((phpinspect-function-p token)
-             (push (phpinspect--index-function-from-scope
-                    type-resolver `(:public ,token) comment-before 
add-used-types
-                    namespace)
-                   functions))))
+             (setq function (phpinspect--index-function-from-scope
+                             type-resolver `(:public ,token) comment-before 
add-used-types
+                             namespace))
+             (unless (phpinspect--function-anonyous-p function)
+             (push function functions)))
+            ((phpinspect-block-or-list-p token)
+             (dolist (fn (phpinspect--index-functions-in-tokens
+                          (cdr token) type-resolver-factory imports namespace
+                          add-used-types type-resolver))
+               (unless (phpinspect--function-anonyous-p fn)
+                 (push fn functions))))))
+
     functions))
 
 (defun phpinspect-function-declaration (function-token)
diff --git a/phpinspect-resolve.el b/phpinspect-resolve.el
index 3af2a33caa..40428e39fc 100644
--- a/phpinspect-resolve.el
+++ b/phpinspect-resolve.el
@@ -43,10 +43,6 @@
         :type phpinspect-token
         :documentation "The token that is assigned from"))
 
-(defsubst phpinspect-block-or-list-p (token)
-  (or (phpinspect-block-p token)
-      (phpinspect-list-p token)))
-
 (defsubst phpinspect-maybe-assignment-p (token)
   "Like `phpinspect-assignment-p', but includes \"as\" barewords as possible 
tokens."
   (or (phpinspect-assignment-p token)
diff --git a/phpinspect-token-predicates.el b/phpinspect-token-predicates.el
index 7c68103838..b27e5d8d48 100644
--- a/phpinspect-token-predicates.el
+++ b/phpinspect-token-predicates.el
@@ -128,6 +128,10 @@ Type can be any of the token types returned by
   (or (phpinspect-token-type-p token :list)
       (phpinspect-incomplete-list-p token)))
 
+(defsubst phpinspect-block-or-list-p (token)
+  (or (phpinspect-block-p token)
+      (phpinspect-list-p token)))
+
 (define-inline phpinspect-declaration-p (token)
   (inline-quote
    (phpinspect-token-type-p ,token :declaration)))
@@ -261,5 +265,4 @@ Type can be any of the token types returned by
   (inline-quote
    (not (phpinspect-comment-p ,token))))
 
-
 (provide 'phpinspect-token-predicates)
diff --git a/test/test-index.el b/test/test-index.el
index 96c217eab1..f2d2a32000 100644
--- a/test/test-index.el
+++ b/test/test-index.el
@@ -407,3 +407,29 @@ public function doStuff()
           (should method)
           (should (phpinspect--function-return-type method))
           (should (phpinspect--type= type (phpinspect--function-return-type 
method))))))))
+
+(ert-deftest phpinspect-index-nested-functions ()
+  (with-temp-buffer
+    (let* ((code "<php
+
+if (true) {
+    function conditional() {
+    }
+}
+
+if (something()) {
+    if (other()) {
+        function nestedConditional() {
+        }
+    }
+}")
+           (index (phpinspect--index-tokens (phpinspect-parse-string code)))
+           (functions (alist-get 'functions index)))
+
+      (should functions)
+      (should (= 2 (length functions)))
+
+      (let ((nestedConditional (car functions))
+            (conditional (cadr functions)))
+        (should (string= "conditional" (phpinspect--function-name 
conditional)))
+        (should (string= "nestedConditional" (phpinspect--function-name 
nestedConditional)))))))



reply via email to

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