unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Help with tree-sitter imenu settings.
@ 2023-12-13 13:43 Aleksandr Vityazev
  2023-12-14 11:03 ` Aleksandr Vityazev
  0 siblings, 1 reply; 2+ messages in thread
From: Aleksandr Vityazev @ 2023-12-13 13:43 UTC (permalink / raw)
  To: help-gnu-emacs


Hi,

I'm writing a mode for devicetree with tree-sitter support, but I'm
having trouble with defining NAME-FN for treesit-simple-imenu-settings.

here is part of the syntax tree for example.dts:
(node name: (identifier) address: @ address: (unit_address) { ...

With (treesit-node-child-by-field-name node "name") I can get
name. With (treesit-node-child-by-field-name node "address")
I can get first address which is "@". But how to get a second one?

example.dts
#+begin_example dts
/dts-v1/;

/ {
    soc {
        flash_controller: flash-controller@4001e000 {
            reg = <0x4001e000 0x1000>;
            flash0: flash@0 {
                label = "SOC_FLASH";
                erase-block = <4096>;
            };
        };
    };
};
#+end_example

using treesit-explore-mode I get this syntax tree
#+begin_example syntax tree
(document
 (file_version /dts-v1/ ;)
 (node name: (identifier) {
  (node name: (identifier) {
   (labeled_item label: (identifier) :
    item:
     (node name: (identifier) address: @ address: (unit_address) {
      (property name: (identifier) =
       value: (integer_cells < (integer_literal) (integer_literal) >)
       ;)
      (labeled_item label: (identifier) :
       item:
        (node name: (identifier) address: @ address: (unit_address) {
         (property name: (identifier) =
          value: (string_literal " ")
          ;)
         (property name: (identifier) =
          value: (integer_cells < (integer_literal) >)
          ;)
         } ;))
      } ;))
   } ;)
  } ;))
#+end_example

treesit-simple-imenu-settings is:

#+begin_src elisp
(setq-local treesit-simple-imenu-settings
                `((nil "\\`node\\'"
                       nil devicetree-ts--mode--name-function)))
#+end_src

where devicetree-ts--mode--name-function is:

#+begin_src elisp
(defun devicetree-ts--mode--name-function (node)
  "Return name of NODE to use for in imenu."
  (let ((name (treesit-node-child-by-field-name node "name"))
        (address (treesit-node-child-by-field-name node "address")))
    (if address
        (concat (treesit-node-text name t)
                (treesit-node-text address t))
      (treesit-node-text name t))))
#+end_src

Imenu will look like this

#+begin_src
/
//soc
//soc/flash-controller@
//soc/flash-controller@ flash@
#+end_src

But I want it to look like this:

#+begin_example
/
//soc
//soc/flash-controller@4001e000
//soc/flash-controller@4001e000 flash@0
#+end_example

-- 
Best regards,
Aleksandr Vityazev



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Help with tree-sitter imenu settings.
  2023-12-13 13:43 Help with tree-sitter imenu settings Aleksandr Vityazev
@ 2023-12-14 11:03 ` Aleksandr Vityazev
  0 siblings, 0 replies; 2+ messages in thread
From: Aleksandr Vityazev @ 2023-12-14 11:03 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 743 bytes --]


I found a solution:

#+begin_src elisp
  (defun devicetree-ts-mode--node-address (node)
  "Return unit addresses for NODE concanated with @."
  (mapconcat (lambda (children)
               (if (string-equal (treesit-node-field-name children)
                                 "address")
                   (treesit-node-text children t)
                 ""))
             (treesit-node-children node)
             ""))

(defun devicetree-ts--mode--name-function (node)
  "Return name of NODE to use for in imenu."
  (let ((name (treesit-node-child-by-field-name node "name")))
    (concat (treesit-node-text name t)
            (devicetree-ts-mode--node-address node))))
#+end_src

-- 
Best regards,
Aleksandr Vityazev

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 227 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-12-14 11:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-13 13:43 Help with tree-sitter imenu settings Aleksandr Vityazev
2023-12-14 11:03 ` Aleksandr Vityazev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).