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).
master
Hugo Thunnissen 1 month ago
parent 7eed1d57e9
commit 25ae878cfc

@ -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)

@ -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)

@ -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)

@ -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)))))))

Loading…
Cancel
Save