Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imenu rework + Imenu consult #34

Open
piotrkwiecinski opened this issue Aug 8, 2023 · 1 comment
Open

Imenu rework + Imenu consult #34

piotrkwiecinski opened this issue Aug 8, 2023 · 1 comment

Comments

@piotrkwiecinski
Copy link
Contributor

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
Copy link
Member

zonuexe commented Aug 9, 2023

@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. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants