branch: externals/phps-mode commit b8e94497740ac0245cc9668657e35ca6aa08632e Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Refactored Imenu to use a persistent variable per buffer --- phps-mode-functions.el | 40 +++++++++++++++++++++++----------------- phps-mode-lexer.el | 4 ++++ phps-mode-test-functions.el | 14 +++++++------- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/phps-mode-functions.el b/phps-mode-functions.el index 04c36ae..74de4bc 100644 --- a/phps-mode-functions.el +++ b/phps-mode-functions.el @@ -34,6 +34,9 @@ (defvar phps-mode-functions-lines-indent nil "The indentation of each line in buffer, nil if none.") +(defvar phps-mode-functions-imenu nil + "The Imenu alist for current buffer, nil if none.") + (defvar phps-mode-functions-verbose nil "Verbose messaging, default nil.") @@ -568,8 +571,7 @@ (defun phps-mode-functions-indent-line () "Indent line." ;; Set lines indent if not set - (unless (and (boundp 'phps-mode-functions-lines-indent) - phps-mode-functions-lines-indent) + (unless phps-mode-functions-lines-indent (setq phps-mode-functions-lines-indent (phps-mode-functions-get-lines-indent))) (when phps-mode-functions-lines-indent @@ -618,7 +620,7 @@ ;; (message "phps-mode-functions-after-change %s %s %s" start stop length) )) -(defun phps-mode-functions-imenu-create-index-function () +(defun phps-mode-functions--imenu-create-index-function () "Create index for imenu." (let ((index '())) @@ -632,7 +634,6 @@ (in-class-name nil) (in-function-declaration nil) (in-function-name nil) - (open-function-level nil) (nesting-level 0)) (dolist (token tokens) (let ((token-symbol (car token)) @@ -695,7 +696,6 @@ (cond ((string= token-symbol "{") - (setq open-function-level nesting-level) (setq in-function-name nil) (setq in-function-declaration nil)) @@ -713,23 +713,28 @@ (setq index-name (concat in-namespace-name "\\" index-name))) (push `(,index-name . ,index-pos) index))))) - (t - (cond + (t (cond - ((equal token-symbol 'T_NAMESPACE) - (setq in-namespace-name nil) - (setq in-namespace-declaration t)) + ((equal token-symbol 'T_NAMESPACE) + (setq in-namespace-name nil) + (setq in-namespace-declaration t)) - ((equal token-symbol 'T_CLASS) - (setq in-class-name nil) - (setq in-class-declaration t)) + ((equal token-symbol 'T_CLASS) + (setq in-class-name nil) + (setq in-class-declaration t)) - ((equal token-symbol 'T_FUNCTION) - (setq in-function-name nil) - (setq in-function-declaration t))))))))) + ((equal token-symbol 'T_FUNCTION) + (setq in-function-name nil) + (setq in-function-declaration t))))))))) (nreverse index))) +(defun phps-mode-functions-get-imenu () + "Get Imenu for current buffer." + (unless phps-mode-functions-imenu + (setq phps-mode-functions-imenu (phps-mode-functions--imenu-create-index-function))) + phps-mode-functions-imenu) + (defun phps-mode-functions-init () "PHP specific init-cleanup routines." @@ -737,7 +742,7 @@ (set (make-local-variable 'indent-line-function) #'phps-mode-functions-indent-line) ;; Support Imenu - (set (make-local-variable 'imenu-create-index-function) #'phps-mode-functions-imenu-create-index-function) + (set (make-local-variable 'imenu-create-index-function) #'phps-mode-functions-get-imenu) (when (and (boundp 'phps-mode-use-psr-2) phps-mode-use-psr-2) @@ -752,6 +757,7 @@ (set (make-local-variable 'phps-mode-functions-buffer-changes-start) nil) (set (make-local-variable 'phps-mode-functions-lines-indent) nil) + (set (make-local-variable 'phps-mode-functions-imenu) nil) (add-hook 'after-change-functions #'phps-mode-functions-after-change)) diff --git a/phps-mode-lexer.el b/phps-mode-lexer.el index 9733cc5..971dc77 100644 --- a/phps-mode-lexer.el +++ b/phps-mode-lexer.el @@ -1283,6 +1283,10 @@ ANY_CHAR' phps-mode-functions-lines-indent) (setq phps-mode-functions-lines-indent nil)) + ;; Reset imenu index + (when (and (boundp 'phps-mode-functions-imenu) + phps-mode-functions-imenu) + (setq phps-mode-functions-imenu nil)) (setq phps-mode-lexer-states nil) (phps-mode-lexer-BEGIN phps-mode-lexer-ST_INITIAL))) diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el index b69ae50..039a606 100644 --- a/phps-mode-test-functions.el +++ b/phps-mode-test-functions.el @@ -589,37 +589,37 @@ (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) ("myFunctionB()" . 42))))) + (should (equal (phps-mode-functions-get-imenu) '(("myFunctionA()" . 16) ("myFunctionB()" . 42))))) (phps-mode-test-with-buffer "<?php\nclass myClass {\n public function myFunctionA() {}\n protected function myFunctionB() {}\n}\n" "Imenu object-oriented file" - (should (equal (phps-mode-functions-imenu-create-index-function) '(("myClass" . 13) ("myClass->myFunctionA()" . 43) ("myClass->myFunctionB()" . 83))))) + (should (equal (phps-mode-functions-get-imenu) '(("myClass" . 13) ("myClass->myFunctionA()" . 43) ("myClass->myFunctionB()" . 83))))) (phps-mode-test-with-buffer "<?php\nnamespace myNamespace {\n class myClass {\n public function myFunctionA() {}\n protected function myFunctionB() {}\n }\n}\n" "Imenu object-oriented file with namespace, class and function" - (should (equal (phps-mode-functions-imenu-create-index-function) '(("\\myNamespace" . 17) ("\\myNamespace\\myClass" . 41) ("\\myNamespace\\myClass->myFunctionA()" . 75) ("\\myNamespace\\myClass->myFunctionB()" . 119))))) + (should (equal (phps-mode-functions-get-imenu) '(("\\myNamespace" . 17) ("\\myNamespace\\myClass" . 41) ("\\myNamespace\\myClass->myFunctionA()" . 75) ("\\myNamespace\\myClass->myFunctionB()" . 119))))) (phps-mode-test-with-buffer "<?php\nnamespace myNamespace;\nclass myClass {\n public function myFunctionA() {}\n protected function myFunctionB() {}\n}\n" "Imenu object-oriented file with bracket-less namespace, class and function" - (should (equal (phps-mode-functions-imenu-create-index-function) '(("\\myNamespace" . 17) ("\\myNamespace\\myClass" . 36) ("\\myNamespace\\myClass->myFunctionA()" . 66) ("\\myNamespace\\myClass->myFunctionB()" . 106))))) + (should (equal (phps-mode-functions-get-imenu) '(("\\myNamespace" . 17) ("\\myNamespace\\myClass" . 36) ("\\myNamespace\\myClass->myFunctionA()" . 66) ("\\myNamespace\\myClass->myFunctionB()" . 106))))) (phps-mode-test-with-buffer "<?php\nnamespace myNamespace {\n class myClass extends myAbstract {\n public function myFunctionA() {}\n protected function myFunctionB() {}\n }\n}\n" "Imenu object-oriented file with namespace, class that extends and functions" - (should (equal (phps-mode-functions-imenu-create-index-function) '(("\\myNamespace" . 17) ("\\myNamespace\\myClass" . 41) ("\\myNamespace\\myClass->myFunctionA()" . 94) ("\\myNamespace\\myClass->myFunctionB()" . 138))))) + (should (equal (phps-mode-functions-get-imenu) '(("\\myNamespace" . 17) ("\\myNamespace\\myClass" . 41) ("\\myNamespace\\myClass->myFunctionA()" . 94) ("\\myNamespace\\myClass->myFunctionB()" . 138))))) (phps-mode-test-with-buffer "<?php\nnamespace myNamespace;\nclass myClass extends myAbstract implements myInterface {\n public function myFunctionA() {}\n protected function myFunctionB() {}\n}\n" "Imenu object-oriented file with bracket-less namespace, class that extends and implements and functions" - (should (equal (phps-mode-functions-imenu-create-index-function) '(("\\myNamespace" . 17) ("\\myNamespace\\myClass" . 36) ("\\myNamespace\\myClass->myFunctionA()" . 108) ("\\myNamespace\\myClass->myFunctionB()" . 148))))) + (should (equal (phps-mode-functions-get-imenu) '(("\\myNamespace" . 17) ("\\myNamespace\\myClass" . 36) ("\\myNamespace\\myClass->myFunctionA()" . 108) ("\\myNamespace\\myClass->myFunctionB()" . 148))))) (phps-mode-test-with-buffer "<?php\nnamespace myNamespace;\nclass myClass extends myAbstract implements myInterface {\n public function myFunctionA($myArg = null) {}\n protected function myFunctionB($myArg = 'abc') {}\n}\n" "Imenu object-oriented file with bracket-less namespace, class that extends and implements and functions with optional arguments" - (should (equal (phps-mode-functions-imenu-create-index-function) '(("\\myNamespace" . 17) ("\\myNamespace\\myClass" . 36) ("\\myNamespace\\myClass->myFunctionA()" . 108) ("\\myNamespace\\myClass->myFunctionB()" . 161))))) + (should (equal (phps-mode-functions-get-imenu) '(("\\myNamespace" . 17) ("\\myNamespace\\myClass" . 36) ("\\myNamespace\\myClass->myFunctionA()" . 108) ("\\myNamespace\\myClass->myFunctionB()" . 161))))) )