branch: externals/phps-mode commit 88ad17844c57efdb9bb75092810865b3ad0c282a Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Bookkeeping support for arrow function variables --- phps-mode-lex-analyzer.el | 67 +++++++++++++++++++++++++++++++++---- phps-mode.el | 4 +-- test/phps-mode-test-lex-analyzer.el | 14 ++++++++ 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/phps-mode-lex-analyzer.el b/phps-mode-lex-analyzer.el index 5f23658..35ff67e 100644 --- a/phps-mode-lex-analyzer.el +++ b/phps-mode-lex-analyzer.el @@ -1081,6 +1081,10 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (in-anonymous-function-declaration) (in-anonymous-function-number 0) (in-anonymous-function-nesting-level) + (in-global-declaration nil) + (in-arrow-fn nil) + (in-arrow-fn-declaration nil) + (in-arrow-fn-number 0) (bookkeeping (make-hash-table :test 'equal))) (push `(END_PARSE ,(length string) . ,(length string)) tokens) @@ -1154,6 +1158,7 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (equal next-token "["))))) (let ((bookkeeping-namespace namespace) + (bookkeeping-alternative-namespace nil) (bookkeeping-index (list token-start token-end)) (bookkeeping-variable-name (substring string (1- token-start) (1- token-end))) (bookkeeping-in-assignment nil) @@ -1218,12 +1223,24 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." ;; Anonymous function level (when in-anonymous-function-nesting-level - (setq bookkeeping-namespace (format "%s anonymous function %s" bookkeeping-namespace in-anonymous-function-number)))) + (setq bookkeeping-namespace (format "%s anonymous function %s" bookkeeping-namespace in-anonymous-function-number))) + + ;; In arrow function body + (when in-arrow-fn + (if in-arrow-fn-declaration + (setq bookkeeping-namespace (format "%s arrow function %s" bookkeeping-namespace in-arrow-fn-number)) + (setq bookkeeping-alternative-namespace bookkeeping-namespace) + (setq bookkeeping-namespace (format "%s arrow function %s" bookkeeping-namespace in-arrow-fn-number))))) (unless bookkeeping-named (when (equal previous-token 'T_STATIC) - (setq bookkeeping-namespace (concat bookkeeping-namespace " static"))) - (setq bookkeeping-namespace (concat bookkeeping-namespace " id " bookkeeping-variable-name))) + (setq bookkeeping-namespace (concat bookkeeping-namespace " static")) + (when bookkeeping-alternative-namespace + (setq bookkeeping-alternative-namespace (concat bookkeeping-alternative-namespace " static")))) + + (setq bookkeeping-namespace (concat bookkeeping-namespace " id " bookkeeping-variable-name)) + (when bookkeeping-alternative-namespace + (setq bookkeeping-alternative-namespace (concat bookkeeping-alternative-namespace " id " bookkeeping-variable-name)))) (phps-mode-debug-message (message "Bookkeeping-namespace: '%s'" bookkeeping-namespace)) @@ -1293,6 +1310,17 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (equal token 'T_VARIABLE)) (setq bookkeeping-in-assignment t)) + ;; In arrow function variable declaration + (when (and in-arrow-fn-declaration + (equal token 'T_VARIABLE)) + (setq bookkeeping-in-assignment t)) + + ;; In global variable declaration + (when (and in-global-declaration + (equal token 'T_VARIABLE) + imenu-in-function-name) + (setq bookkeeping-in-assignment t)) + ;; In [$abc, $def] = .. or array($abc, $def) = ... (when (and array-variable-declaration @@ -1332,9 +1360,15 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (phps-mode-debug-message (message "Bookkeeping-hit: %s" bookkeeping-index)) (puthash bookkeeping-index 1 bookkeeping)) - (phps-mode-debug-message - (message "Bookkeeping-miss: %s" bookkeeping-index)) - (puthash bookkeeping-index 0 bookkeeping))))) + (if (and bookkeeping-alternative-namespace + (gethash bookkeeping-alternative-namespace bookkeeping)) + (progn + (phps-mode-debug-message + (message "Bookkeeping-alternative-hit: %s" bookkeeping-index)) + (puthash bookkeeping-index 1 bookkeeping)) + (phps-mode-debug-message + (message "Bookkeeping-miss: %s" bookkeeping-index)) + (puthash bookkeeping-index 0 bookkeeping)))))) ;; Keep track of array variable declaration (when first-token-on-line @@ -1343,6 +1377,12 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (setq array-variable-declaration t) (setq array-variable-declaration nil))) + ;; Keep track of global declaration for bookkeeping + (when first-token-on-line + (if (equal token 'T_GLOBAL) + (setq in-global-declaration t) + (setq in-global-declaration nil))) + ;; Keep track of open catch blocks for bookkeeping (when (equal token 'T_CATCH) (setq in-catch-declaration t)) @@ -1365,6 +1405,21 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (equal curly-bracket-level (car in-anonymous-function-nesting-level))) (pop in-anonymous-function-nesting-level)) + ;; Keep track of arrow function declaration + (when (equal token 'T_FN) + (unless in-arrow-fn + (setq in-arrow-fn-number (1+ in-arrow-fn-number))) + (setq in-arrow-fn t) + (setq in-arrow-fn-declaration t)) + (when (and + in-arrow-fn-declaration + (equal token ")")) + (setq in-arrow-fn-declaration nil)) + (when (and + in-arrow-fn + (equal token ";")) + (setq in-arrow-fn nil) + (setq in-arrow-fn-declaration nil)) ;; IMENU LOGIC diff --git a/phps-mode.el b/phps-mode.el index d3ec1ff..665a3d3 100644 --- a/phps-mode.el +++ b/phps-mode.el @@ -5,8 +5,8 @@ ;; Author: Christian Johansson <christ...@cvj.se> ;; Maintainer: Christian Johansson <christ...@cvj.se> ;; Created: 3 Mar 2018 -;; Modified: 15 Sep 2020 -;; Version: 0.3.56 +;; Modified: 16 Sep 2020 +;; Version: 0.3.58 ;; Keywords: tools, convenience ;; URL: https://github.com/cjohansson/emacs-phps-mode diff --git a/test/phps-mode-test-lex-analyzer.el b/test/phps-mode-test-lex-analyzer.el index 032e36b..3d26c91 100644 --- a/test/phps-mode-test-lex-analyzer.el +++ b/test/phps-mode-test-lex-analyzer.el @@ -1446,6 +1446,20 @@ (phps-mode-test--hash-to-list (phps-mode-lex-analyzer--get-bookkeeping) t) (list (list " id $random" 1) (list (list 9 16) 1) (list " id $bandom" 1) (list (list 18 25) 1) (list (list 45 52) 1) (list (list 78 85) 1) (list " id $random2" 1) (list (list 114 122) 1) (list " id $bandom2" 1) (list (list 124 132) 1) (list (list 153 161) 1) (list (list 187 195) 0))))) + (phps-mode-test--with-buffer + "<?php\n\n$var = 123;\n\nfunction test($abc) {\n global $var;\n if ($var) {\n echo 'Hit';\n }\n}" + "Global variable declaration in function" + (should (equal + (phps-mode-test--hash-to-list (phps-mode-lex-analyzer--get-bookkeeping) t) + (list (list " id $var" 1) (list (list 8 12) 1) (list " function test id $abc" 1) (list (list 35 39) 1) (list " function test id $var" 1) (list (list 54 58) 1) (list (list 68 72) 1))))) + + (phps-mode-test--with-buffer + "<?php\n$y = 1;\n$fn1 = fn($x) => $x + $y;\n$z = 1;\n$fn = fn($x2) => fn($y2) => $x2 * $y2 + $z;\nfn(array $x3) => $x3;\n$x4 = 4;\nstatic fn(): int => $x4;\nfn($x5 = 42) => $x5;\nfn(&$x6) => $x6;\nfn&($x7) => $x7;\nfn($x8, ...$rest) => $rest;" + "Bookkeeping in arrow functions" + (should (equal + (phps-mode-test--hash-to-list (phps-mode-lex-analyzer--get-bookkeeping) t) + (list (list " id $y" 1) (list (list 7 9) 1) (list " id $fn1" 1) (list (list 15 19) 1) (list " arrow function 1 id $x" 1) (list (list 25 27) 1) (list (list 32 34) 1) (list (list 37 39) 1) (list " id $z" 1) (list (list 41 43) 1) (list " id $fn" 1) (list (list 49 52) 1) (list " arrow function 2 id $x2" 1) (list (list 58 61) 1) (list " arrow function 2 id $y2" 1) (list (list 69 72) 1) (list (list 77 80) 1) (list (list 83 86) 1) (list (list 89 91) 1) (list " arrow function 3 id $x [...] + ) (defun phps-mode-test-lex-analyzer ()