branch: externals/phps-mode commit e614a20ada15e5c1ddec8710a0ceb6e4cef0443b Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Added bookkeeping for referenced foreach variables --- phps-mode-parser-sdt.el | 49 +++++++++++++++++++++++++++++++++++++++++----- test/phps-mode-test-ast.el | 2 +- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/phps-mode-parser-sdt.el b/phps-mode-parser-sdt.el index a626eb02a5..a2bd37905b 100644 --- a/phps-mode-parser-sdt.el +++ b/phps-mode-parser-sdt.el @@ -2375,8 +2375,46 @@ ;; 205 ((foreach_variable) (ampersand variable)) (puthash 205 - (lambda(args _terminals) - ;; TODO Declare variable here + (lambda(args terminals) + ;; Save variable declaration in bookkeeping buffer + (let* ((variable (nth 1 args)) + (variable-type (plist-get variable 'ast-type))) + (cond + ((equal variable-type 'variable-callable-variable) + (let* ((callable-variable (plist-get variable 'callable-variable)) + (callable-variable-type (plist-get callable-variable 'ast-type))) + (cond + ((equal callable-variable-type 'callable-variable-simple-variable) + (let ((callable-variable-simple-variable + (plist-get callable-variable 'simple-variable))) + (let ((callable-variable-simple-variable-type + (plist-get + callable-variable-simple-variable + 'ast-type))) + (cond + ((equal + callable-variable-simple-variable-type + 'simple-variable-variable) + (let* ((variable-name + (plist-get + callable-variable-simple-variable + 'variable)) + (symbol-name + variable-name) + (symbol-start + (car (cdr (nth 1 terminals)))) + (symbol-end + (cdr (cdr (nth 1 terminals)))) + (symbol-scope + phps-mode-parser-sdt--bookkeeping-namespace)) + ;; (message "declared foreach variable from terminals: %S" (nth 1 terminals)) + (push + (list + symbol-name + symbol-scope + symbol-start + symbol-end) + phps-mode-parser-sdt--bookkeeping-symbol-assignment-stack)))))))))))) `( ast-type @@ -2444,8 +2482,7 @@ ast-type foreach-statement statement - ,args - )) + ,args)) phps-mode-parser--table-translations) ;; 211 ((foreach_statement) (":" inner_statement_list T_ENDFOREACH ";")) @@ -5937,7 +5974,9 @@ (cdr (cdr terminals))) (namespace phps-mode-parser-sdt--bookkeeping-namespace)) (when phps-mode-parser-sdt--bookkeeping-namespace-stack - (setq namespace (pop phps-mode-parser-sdt--bookkeeping-namespace-stack))) + (setq + namespace + (pop phps-mode-parser-sdt--bookkeeping-namespace-stack))) (push (list symbol-name diff --git a/test/phps-mode-test-ast.el b/test/phps-mode-test-ast.el index 7883ec8207..b6485e2ecc 100644 --- a/test/phps-mode-test-ast.el +++ b/test/phps-mode-test-ast.el @@ -266,7 +266,7 @@ (phps-mode-test-ast--should-bookkeep "<?php\n$items = array(1, 2, 3);\nforeach ($items as &$item) {\n if ($item) {\n echo 'Hit';\n }\n}\nforeach ($items as $key => &$item2) {\n if ($item) {\n echo 'Hit';\n }\n}" "Bookkeeping of foreach reference variable declaration" - '((" id $items" 1) ((7 13) 1) ((41 47) 1) (" id $item" 1) ((52 57) 1) ((69 74) 1) ((115 121) 1) (" id $key" 1) ((125 129) 1) (" id $item2" 1) ((134 140) 1) ((152 157) 1))) + '((" id $items" ((7 13))) ((7 13) 1) (" id $item" ((52 57))) ((69 74) 1) ((52 57) 1) ((41 47) 1) (" id $item2" ((134 140))) (" id $key" ((125 129))) ((152 157) 1) ((134 140) 1) ((125 129) 1) ((115 121) 1))) (phps-mode-test-ast--should-bookkeep "<?php\n\n[$random, $bandom] = myValues();\nif ($random) {\n echo 'Hit';\n}\nif ($bandom) {\n echo 'Hit';\n}\n"