emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/phps-mode a80c12ef14 005/212: Passing imenu-generation


From: Christian Johansson
Subject: [elpa] externals/phps-mode a80c12ef14 005/212: Passing imenu-generation for file with multiple namespaces
Date: Wed, 26 Jan 2022 01:50:10 -0500 (EST)

branch: externals/phps-mode
commit a80c12ef14017fc2ee585aeb2f814c7de955e1d1
Author: Christian Johansson <christian@cvj.se>
Commit: Christian Johansson <christian@cvj.se>

    Passing imenu-generation for file with multiple namespaces
---
 test/phps-mode-test-lex-analyzer.el |   5 +
 test/phps-mode-test-parser.el       | 221 +++++++++++++++++++++++++++++-------
 2 files changed, 186 insertions(+), 40 deletions(-)

diff --git a/test/phps-mode-test-lex-analyzer.el 
b/test/phps-mode-test-lex-analyzer.el
index c062a2ecb4..172eec894d 100644
--- a/test/phps-mode-test-lex-analyzer.el
+++ b/test/phps-mode-test-lex-analyzer.el
@@ -1244,6 +1244,11 @@
    "Imenu with double quoted string with variable inside it and concatenated 
string"
    (should (equal (phps-mode-lex-analyzer--get-imenu) '(("MyNamespace" 
("aFunction" . 41) ("MyClass" ("__construct" . 160) ("myFunction1" . 261) 
("myFunction2" . 433) ("myFunction3" . 513) ("myFunction4" . 583)))))))
 
+  (phps-mode-test--with-buffer
+  "<?php\n\nnamespace MyNamespaceA\n{\n    function aFunctionA() {\n        
/**\n         * With some contents\n         */\n    }\n    class MyClass\n    
{\n\n        /**\n         *\n         */\n        public function 
__construct()\n        {\n            if ($test) {\n            }\n        
}\n\n        /**\n         *\n         */\n        public function 
myFunction1()\n        {\n            $this->addMessage(\"My random {$message} 
here\" . ($random > 1 ? \"A\" : \"\") . \" was he [...]
+  "Imenu with double quoted string with variable inside it and concatenated 
string in two namespaces"
+  (should (equal (phps-mode-lex-analyzer--get-imenu) '(("MyNamespaceA" 
("aFunctionA" . 46) ("MyClass" ("__construct" . 205) ("myFunction1" . 338) 
("myFunction2" . 542) ("myFunction3" . 646) ("myFunction4" . 740))) 
("aFunctionB" . 807) ("MyClass" ("__construct" . 925) ("myFunction1" . 1058) 
("myFunction2" . 1262) ("myFunction3" . 1366) ("myFunction4" . 1460))))))
+
   )
 
 (defun phps-mode-test-lex-analyzer--get-moved-imenu ()
diff --git a/test/phps-mode-test-parser.el b/test/phps-mode-test-parser.el
index 62e64bf11b..4b4bbd83c8 100644
--- a/test/phps-mode-test-parser.el
+++ b/test/phps-mode-test-parser.el
@@ -314,79 +314,107 @@
         (ast-current-namespace)
         (ast-current-namespace-children))
 
-    ;; function_declaration_statement -> (function returns_ref T_STRING 
backup_doc_comment "(" parameter_list ")" return_type backup_fn_flags "{" 
inner_statement_list "}" backup_fn_flags)
+    ;; top_statement_list -> (top_statement_list top_statement)
     (puthash
-     174
+     79
+     (lambda(args terminals)
+       ;; (message "top_statement_list: %S" args)
+       (let ((ast-object))
+         (if (car args)
+             (setq ast-object (append (car args) (cdr args)))
+           (setq ast-object (cdr args)))
+         ;; (message "ast-object: %S" ast-object)
+         ast-object))
+     phps-mode-parser--table-translations)
+
+    ;; top_statement -> (T_NAMESPACE namespace_declaration_name ";")
+    (puthash
+     106
      (lambda(args terminals)
        (let ((ast-object
               (list
                'type
-               'function
+               'namespace
                'name
-               (nth 2 args)
+               (nth 1 args)
                'index
-               (car (cdr (nth 2 terminals)))
+               (car (cdr (nth 1 terminals)))
                'start
-               (car (cdr (nth 9 terminals)))
+               (car (cdr (nth 2 terminals)))
                'end
-               (car (cdr (nth 11 terminals))))))
-         ;; (message "Function: %S" ast-object)
+               'max)))
+         ;; (message "Namespace %S" ast-object)
          ;; (message "args: %S" args)
          ;; (message "terminals: %S" terminals)
-         (if ast-current-namespace
-             (push
-              ast-object
-              ast-current-namespace-children)
-           (push
-            ast-object
-            ast))
+         (setq
+          ast-current-namespace
+          ast-object)
          ast-object))
      phps-mode-parser--table-translations)
 
-    ;; attributed_class_statement -> (method_modifiers function returns_ref 
identifier backup_doc_comment "(" parameter_list ")" return_type 
backup_fn_flags method_body backup_fn_flags)
+    ;; top_statement -> (T_NAMESPACE namespace_declaration_name "{" 
top_statement_list "}")
     (puthash
-     280
+     107
      (lambda(args terminals)
        (let ((ast-object
               (list
                'type
-               'method
+               'namespace
                'name
-               (nth 3 args)
+               (nth 1 args)
                'index
-               (car (cdr (nth 3 terminals)))
+               (car (cdr (nth 1 terminals)))
                'start
-               (car (cdr (car (nth 10 terminals))))
+               (car (cdr (nth 2 terminals)))
                'end
-               (cdr (cdr (car (cdr (cdr (nth 10 terminals)))))))))
-         ;; (message "Method: %S" ast-object)
+               (car (cdr (nth 4 terminals)))
+               'children
+               (nth 3 args))))
+         ;; (message "Namespace %S" ast-object)
          ;; (message "args: %S" args)
          ;; (message "terminals: %S" terminals)
+         ;; (message "ast-object: %S" ast-object)
+         (push
+          ast-object
+          ast)
          ast-object))
      phps-mode-parser--table-translations)
 
-    ;; top_statement -> (T_NAMESPACE namespace_declaration_name ";")
+    ;; top_statement -> (T_NAMESPACE "{" top_statement_list "}")
     (puthash
-     106
+     108
+     (lambda(args terminals)
+       (message "T_NAMESPACE: %S" args)
+       (when (nth 2 args)
+         (setq
+          ast
+          (append ast (nth 2 args))))
+       (nth 2 args))
+     phps-mode-parser--table-translations)
+
+    ;; function_declaration_statement -> (function returns_ref T_STRING 
backup_doc_comment "(" parameter_list ")" return_type backup_fn_flags "{" 
inner_statement_list "}" backup_fn_flags)
+    (puthash
+     174
      (lambda(args terminals)
        (let ((ast-object
               (list
                'type
-               'namespace
+               'function
                'name
-               (nth 1 args)
+               (nth 2 args)
                'index
-               (car (cdr (nth 1 terminals)))
-               'start
                (car (cdr (nth 2 terminals)))
+               'start
+               (car (cdr (nth 9 terminals)))
                'end
-               'max)))
-         ;; (message "Namespace %S" ast-object)
+               (car (cdr (nth 11 terminals))))))
+         ;; (message "Function: %S" ast-object)
          ;; (message "args: %S" args)
          ;; (message "terminals: %S" terminals)
-         (setq
-          ast-current-namespace
-          ast-object)
+         (when ast-current-namespace
+           (push
+            ast-object
+            ast-current-namespace-children))
          ast-object))
      phps-mode-parser--table-translations)
 
@@ -411,13 +439,10 @@
          ;; (message "Class %S" ast-object)
          ;; (message "args: %S" args)
          ;; (message "terminals: %S" terminals)
-         (if ast-current-namespace
-             (push
-              ast-object
-              ast-current-namespace-children)
+         (when ast-current-namespace
            (push
             ast-object
-            ast))
+            ast-current-namespace-children))
          ast-object))
      phps-mode-parser--table-translations)
 
@@ -434,6 +459,28 @@
          ast-object))
      phps-mode-parser--table-translations)
 
+    ;; attributed_class_statement -> (method_modifiers function returns_ref 
identifier backup_doc_comment "(" parameter_list ")" return_type 
backup_fn_flags method_body backup_fn_flags)
+    (puthash
+     280
+     (lambda(args terminals)
+       (let ((ast-object
+              (list
+               'type
+               'method
+               'name
+               (nth 3 args)
+               'index
+               (car (cdr (nth 3 terminals)))
+               'start
+               (car (cdr (car (nth 10 terminals))))
+               'end
+               (cdr (cdr (car (cdr (cdr (nth 10 terminals)))))))))
+         ;; (message "Method: %S" ast-object)
+         ;; (message "args: %S" args)
+         ;; (message "terminals: %S" terminals)
+         ast-object))
+     phps-mode-parser--table-translations)
+
     (phps-mode-test-parser--buffer-contents
      "<?php\n\nnamespace MyNamespace;\n\nfunction aFunction() {\n    /**\n     
* With some contents\n     */\n}\n\nclass MyClass\n{\n\n    /**\n     *\n     
*/\n    public function __construct()\n    {\n        if ($test) {\n        }\n 
   }\n\n    /**\n     *\n     */\n    public function myFunction1()\n    {\n    
    $this->addMessage(\"My random {$message} here\" . ($random > 1 ? \"A\" : 
\"\") . \" was here.\");\n    }\n    \n    /**\n     *\n     */\n    public 
function myFunction2() [...]
      "Imenu with double quoted string with variable inside it and concatenated 
string"
@@ -511,7 +558,101 @@
             (equal
              imenu-index
              '(("MyNamespace" ("aFunction" . 41) ("MyClass" ("__construct" . 
160) ("myFunction1" . 261) ("myFunction2" . 433) ("myFunction3" . 513) 
("myFunction4" . 583))))))
-           )))))
+           ))))
+
+    (setq
+     ast-current-namespace
+     nil)
+    (setq
+     ast-current-namespace-children
+     nil)
+    (setq
+     ast
+     nil)
+
+    (phps-mode-test-parser--buffer-contents
+     "<?php\n\nnamespace MyNamespaceA\n{\n    function aFunctionA() {\n        
/**\n         * With some contents\n         */\n    }\n    class MyClass\n    
{\n\n        /**\n         *\n         */\n        public function 
__construct()\n        {\n            if ($test) {\n            }\n        
}\n\n        /**\n         *\n         */\n        public function 
myFunction1()\n        {\n            $this->addMessage(\"My random {$message} 
here\" . ($random > 1 ? \"A\" : \"\") . \" was [...]
+     "Imenu with double quoted string with variable inside it and concatenated 
string in two namespaces"
+     (lambda()
+       (let ((parse (phps-mode-parser-parse)))
+         (message "Left-to-right with left-most derivation:\n%S\n" parse)
+         (dolist (production-number (reverse parse))
+           (let ((production
+                  (phps-mode-parser--get-grammar-production-by-number
+                   production-number)))
+             (message
+              "%d: %S -> %S"
+              production-number
+              (car (car production))
+              (car (cdr production))))))
+       (let ((translation (phps-mode-parser-translate))
+             (imenu-index))
+         ;; (message "translation: %S" translation)
+
+         (when ast-current-namespace
+           (plist-put
+            ast-current-namespace
+            'children
+            (reverse ast-current-namespace-children))
+           (push
+            ast-current-namespace
+            ast))
+
+         (message "\nAST:\n%S\n" ast)
+
+         (let ((imenu-index))
+           (dolist (item ast)
+             (let ((children (plist-get item 'children))
+                   (item-type (plist-get item 'type))
+                   (parent))
+               (if (and
+                    (or
+                     (equal item-type 'namespace)
+                     (equal item-type 'class))
+                    children)
+                   (progn
+                     (dolist (child children)
+                       (let ((grandchildren (plist-get child 'children))
+                             (child-type (plist-get child 'type))
+                             (subparent))
+                         (if (and
+                              (equal child-type 'class)
+                              grandchildren)
+                             (progn
+                               (dolist (grandchild grandchildren)
+                                 (push
+                                  `(,(plist-get grandchild 'name) . 
,(plist-get grandchild 'index))
+                                  subparent))
+                               (push
+                                (append
+                                 (list (plist-get child 'name))
+                                 (reverse subparent))
+                                parent))
+                           (push
+                            `(,(plist-get child 'name) . ,(plist-get child 
'index))
+                            parent)))
+                       )
+                     (push
+                      (append
+                       (list (plist-get item 'name))
+                       (reverse parent))
+                      imenu-index))
+                 (push
+                  `(,(plist-get item 'name) . ,(plist-get item 'index))
+                  imenu-index))))
+           (setq
+            imenu-index
+            (reverse imenu-index))
+
+           (message "imenu-index:\n%S\n" imenu-index)
+
+           (should
+            (equal
+             imenu-index
+             '(("MyNamespaceA" ("aFunctionA" . 46) ("MyClass" ("__construct" . 
205) ("myFunction1" . 338) ("myFunction2" . 542) ("myFunction3" . 646) 
("myFunction4" . 740))) ("aFunctionB" . 807) ("MyClass" ("__construct" . 925) 
("myFunction1" . 1058) ("myFunction2" . 1262) ("myFunction3" . 1366) 
("myFunction4" . 1460)))))
+           ))))
+
+    )
 
   (message "\n-- Ran tests for parser translation. --"))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]