branch: externals/phps-mode commit 0de10fafd1ae10974343fd05d890b23544ff28af Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Passes first test for imenu --- phps-mode-functions.el | 60 +++++++++++++++++++++++++++++++++++++++++++-- phps-mode-test-functions.el | 7 +++--- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/phps-mode-functions.el b/phps-mode-functions.el index eba4714..798aee0 100644 --- a/phps-mode-functions.el +++ b/phps-mode-functions.el @@ -623,9 +623,65 @@ "Create index for imenu." (let ((index '())) - ;; TODO Iterate namespaces, classes and functions and add to index + (when (boundp 'phps-mode-lexer-tokens) + (let ((tokens phps-mode-lexer-tokens) + (in-namespace-declaration nil) + (in-class-declaration nil) + (in-function-declaration nil)) + (dolist (token tokens) + (let ((token-symbol (car token)) + (token-start (car (cdr token))) + (token-end (cdr (cdr token)))) + (cond - index)) + (in-namespace-declaration + (cond + + ((or (string= token-symbol "{") + (string= token-symbol ";")) + (setq in-namespace-declaration nil)) + + ((equal token-symbol 'T_STRING) + (let ((index-name (format "namespace %s" (buffer-substring-no-properties token-start token-end))) + (index-pos token-start)) + (push `(,index-name . ,index-pos) index))))) + + (in-class-declaration + (cond + + ((string= token-symbol "{") + (setq in-class-declaration nil)) + + ((equal token-symbol 'T_STRING) + (let ((index-name (format "class %s" (buffer-substring-no-properties token-start token-end))) + (index-pos token-start)) + (push `(,index-name . ,index-pos) index))))) + + (in-function-declaration + (cond + + ((or (string= token-symbol "{") + (string= token-symbol ";")) + (setq in-function-declaration nil)) + + ((equal token-symbol 'T_STRING) + (let ((index-name (format "function %s" (buffer-substring-no-properties token-start token-end))) + (index-pos token-start)) + (push `(,index-name . ,index-pos) index))))) + + (t + (cond + + ((equal token-symbol 'T_NAMESPACE) + (setq in-namespace-declaration t)) + + ((equal token-symbol 'T_CLASS) + (setq in-class-declaration t)) + + ((equal token-symbol 'T_FUNCTION) + (setq in-function-declaration t))))))))) + + (nreverse index))) (defun phps-mode-functions-init () "PHP specific init-cleanup routines." diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el index 1de7fdf..c889176 100644 --- a/phps-mode-test-functions.el +++ b/phps-mode-test-functions.el @@ -584,12 +584,13 @@ ) (defun phps-mode-test-functions-imenu () - "Test for imenu" + "Test for imenu." + (phps-mode-test-with-buffer "<?php\nfunction myFunctionA() {}\nfunction myFunctionB() {}\n" "Imenu function-oriented file" - (should (equal (phps-mode-functions-imenu-create-index-function) '(("myFunctionA" . 16) ("myFunction B" . 42)))) - ) + (should (equal (phps-mode-functions-imenu-create-index-function) '(("function myFunctionA" . 16) ("function myFunctionB" . 42))))) + ) ;; TODO Add tests for all examples here: https://www.php-fig.org/psr/psr-2/