emacs-php / php-ts-mode

A Tree-sitter based major mode for editing PHP codes
GNU General Public License v3.0
23 stars 8 forks source link

Imenu rework + Imenu consult #34

Open piotrkwiecinski opened 1 year ago

piotrkwiecinski commented 1 year ago

I'm working on aligning imenu with what's implemented in php-mode:


(defconst php-imenu-generic-expression-simple
  (eval-when-compile
    `(("Methods"
       ,(php-create-regexp-for-method nil) 2)
      ("Properties"
       ,(rx line-start
            (* (syntax whitespace))
            (+ (or "public" "protected" "private" "static" "var")
               (+ (syntax whitespace)))
            (* (? (? (or "|" "?"))
                  (or "\\" (syntax word) (syntax symbol))
                  (+ (syntax whitespace))))
            (group
             "$" (+ (or (syntax word) (syntax symbol))))
            word-boundary)
       1)
      ("Constants"
       ,(rx line-start
            (* (syntax whitespace))
            (group
             (* (or "public" "protected" "private")
                (+ (syntax whitespace)))
             "const"
             (+ (syntax whitespace))
             (+ (or (syntax word) (syntax symbol)))))
       1)
      ("Functions"
       ,(rx line-start
            (* (syntax whitespace))
            "function"
            (+ (syntax whitespace))
            (group
             (+ (or (syntax word) (syntax symbol)))))
       1)
      ("Classes"
       ,(php-create-regexp-for-classlike "\\(?:class\\|interface\\|trait\\|enum\\)") 1)
      ("Namespace"
       ,(php-create-regexp-for-classlike "namespace") 1)))
  "Imenu generic expression for PHP Mode.  See `imenu-generic-expression'.")

In addition I'm trying to have similar imenu consult implementation to emacs-lisp-mode:

(defcustom consult-imenu-config
  '((emacs-lisp-mode :toplevel "Functions"
                     :types ((?f "Functions" font-lock-function-name-face)
                             (?m "Macros"    font-lock-function-name-face)
                             (?p "Packages"  font-lock-constant-face)
                             (?t "Types"     font-lock-type-face)
                             (?v "Variables" font-lock-variable-name-face))))

emacsconsultimenu

Here is what I have so far locally: emacsphptsconsultimenu

@zonuexe what do you think is it good direction?

(require 'consult-imenu)
(add-to-list 'consult-imenu-config '(php-ts-mode :toplevel "Namespace"
                                                 :types ((?n "Namespace" font-lock-function-name-face)
                                                         (?p "Properties" font-lock-type-face)
                                                         (?o "Constants" font-lock-type-face)
                                                         (?c "Classes"    font-lock-type-face)
                                                         (?f "Functions" font-lock-function-name-face)
                                                         (?i "Imports" font-lock-type-face)
                                                         (?m "Methods"  font-lock-function-name-face))))

I still have to finish a function to extract names for imports, properties, constants as they don't have :name property but I think I'm a right track.

  (defun php-ts-mode--imenu-node-name (node)
    "Imenu node name."
    ;;; TODO: traverse children based on node type
    (message "%s : %s" node (treesit-node-text (treesit-node-child node 0)))
    (treesit-node-text (treesit-node-child node 0)))

  ;; Imenu.
  (setq-local treesit-simple-imenu-settings
              `(("Namespace" "\\`namespace_definition\\'" nil nil)
                ("Classes" ,(rx bos (or "class_declaration"
                                        "interface_declaration"
                                        "enum_declaration"
                                        "trait_declaration")
                                eos)
                 nil nil)
                ("Methods" "\\`method_declaration\\'" nil nil)
                ("Functions" "\\`function_definition\\'" nil nil)
                ("Constants" "\\`const_declaration\\'" nil php-ts-mode--imenu-node-name)
                ("Imports" "\\`namespace_use_declaration\\'"nil php-ts-mode--imenu-node-name)
                ("Properties" "\\`property_declaration\\'" nil php-ts-mode--imenu-node-name)))
zonuexe commented 1 year ago

@piotrkwiecinski Thank you, I think it looks good in that direction. That code snippet was written by me so there is no copyright issue in copying the code from PHP Mode. 👍