From: Evgeni Kolev <evgenysw@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Randy Taylor <dev@rjt.dev>,
60407@debbugs.gnu.org, Yuan Fu <casouri@gmail.com>
Subject: bug#60407: [PATCH] Update go-ts-mode to use Imenu facility
Date: Sun, 1 Jan 2023 15:05:05 +0200 [thread overview]
Message-ID: <CAMCrgaUbQmXGrUYTF8C_hgu-bUTFmaDQXgjrGcM=i-5JkKpq2g@mail.gmail.com> (raw)
In-Reply-To: <83y1qm39o3.fsf@gnu.org>
To illustrate the change, here's a comparison before VS after (VS eglot).
I'm using a sample .go file (at the bottom of this mail).
Before:
4 candidates:
Function: measure
Type: geometry
Type: rect
Type: circle
After:
8 candidates:
Function: measure
Type: geometry
Type: rect
Type: circle
Method: area
Method: perim
Method: area
Method: perim
For comparison, here's eglot's Imenu (using Go's gopls language server):
13 candidates:
Interface: geometry
Struct: rect
Struct: circle
Field.rect: width
Field.rect: height
Field.circle: radius
Method.geometry: area
Method.geometry: perim
Method: (rect).area
Method: (rect).perim
Method: (circle).area
Method: (circle).perim
Function: measure
Sample .go file:
> type geometry interface {
> area() float64
> perim() float64
> }
>
> type rect struct {
> width, height float64
> }
>
> type circle struct {
> radius float64
> }
>
> func (r rect) area() float64 {
> return r.width * r.height
> }
>
> func (r rect) perim() float64 {
> return 2*r.width + 2*r.height
> }
>
> func (c circle) area() float64 {
> return math.Pi * c.radius * c.radius
> }
>
> func (c circle) perim() float64 {
> return 2 * math.Pi * c.radius
> }
>
> func measure(g geometry) {
> fmt.Println(g)
> fmt.Println(g.area())
> fmt.Println(g.perim())
> }
On Sun, Jan 1, 2023 at 11:07 AM Eli Zaretskii <eliz@gnu.org> wrote:
>
> > From: Evgeni Kolev <evgenysw@gmail.com>
> > Date: Thu, 29 Dec 2022 18:05:49 +0200
> >
> > This patch updates go-ts-mode to use Imenu facility added in
> > https://git.savannah.gnu.org/cgit/emacs.git/commit/?h=emacs-29&id=b39dc7ab27a696a8607ab859aeff3c71509231f5
> >
> > The Imenu items are extended to support "Method", in addition to
> > "Function" and "Type".
> >
> > The current Imenu approach uses "type_spec" to identify "Type" which
> > acts as a catch-all for many Go constructs, for example struct and
> > interface definitions. This catch-all approach is not optimal because
> > structs and interfaces are put in the same "Type" bucket. In a
> > follow-up patch I'll try to change the approach and have separate
> > "Interface" and "Struct" types.
> >
> > The patch is below.
>
> Randy, Yuan, are you looking into this?
>
> > commit 71ff7b21fe92167313bd1761b68b6e6fd879b09f
> > Author: Evgeni Kolev <evgenysw@gmail.com>
> > Date: Thu Dec 29 17:49:40 2022 +0200
> >
> > Update go-ts-mode to use Imenu facility
> >
> > go-ts-mode is updated to use the Imenu facility added in commit
> > b39dc7ab27a696a8607ab859aeff3c71509231f5.
> >
> > The Imenu items are extended to support "Method", in addition to
> > "Function" and "Type".
> >
> > * lisp/progmodes/go-ts-mode.el (go-ts-mode--imenu-1) (go-ts-mode--imenu):
> > Remove functions.
> > (go-ts-mode--defun-name): New function.
> >
> > diff --git a/lisp/progmodes/go-ts-mode.el b/lisp/progmodes/go-ts-mode.el
> > index 124d9b044a2..c6c1c61d9f4 100644
> > --- a/lisp/progmodes/go-ts-mode.el
> > +++ b/lisp/progmodes/go-ts-mode.el
> > @@ -173,44 +173,6 @@ go-ts-mode--font-lock-settings
> > '((ERROR) @font-lock-warning-face))
> > "Tree-sitter font-lock settings for `go-ts-mode'.")
> >
> > -(defun go-ts-mode--imenu ()
> > - "Return Imenu alist for the current buffer."
> > - (let* ((node (treesit-buffer-root-node))
> > - (func-tree (treesit-induce-sparse-tree
> > - node "function_declaration" nil 1000))
> > - (type-tree (treesit-induce-sparse-tree
> > - node "type_spec" nil 1000))
> > - (func-index (go-ts-mode--imenu-1 func-tree))
> > - (type-index (go-ts-mode--imenu-1 type-tree)))
> > - (append
> > - (when func-index `(("Function" . ,func-index)))
> > - (when type-index `(("Type" . ,type-index))))))
> > -
> > -(defun go-ts-mode--imenu-1 (node)
> > - "Helper for `go-ts-mode--imenu'.
> > -Find string representation for NODE and set marker, then recurse
> > -the subtrees."
> > - (let* ((ts-node (car node))
> > - (children (cdr node))
> > - (subtrees (mapcan #'go-ts-mode--imenu-1
> > - children))
> > - (name (when ts-node
> > - (treesit-node-text
> > - (pcase (treesit-node-type ts-node)
> > - ("function_declaration"
> > - (treesit-node-child-by-field-name ts-node "name"))
> > - ("type_spec"
> > - (treesit-node-child-by-field-name ts-node "name"))))))
> > - (marker (when ts-node
> > - (set-marker (make-marker)
> > - (treesit-node-start ts-node)))))
> > - (cond
> > - ((or (null ts-node) (null name)) subtrees)
> > - (subtrees
> > - `((,name ,(cons name marker) ,@subtrees)))
> > - (t
> > - `((,name . ,marker))))))
> > -
> > ;;;###autoload
> > (add-to-list 'auto-mode-alist '("\\.go\\'" . go-ts-mode))
> >
> > @@ -228,9 +190,18 @@ go-ts-mode
> > (setq-local comment-end "")
> > (setq-local comment-start-skip (rx "//" (* (syntax whitespace))))
> >
> > + ;; Navigation.
> > + (setq-local treesit-defun-type-regexp
> > + (regexp-opt '("method_declaration"
> > + "function_declaration"
> > + "type_spec")))
> > + (setq-local treesit-defun-name-function #'go-ts-mode--defun-name)
> > +
> > ;; Imenu.
> > - (setq-local imenu-create-index-function #'go-ts-mode--imenu)
> > - (setq-local which-func-functions nil)
> > + (setq-local treesit-simple-imenu-settings
> > + `(("Function" "\\`function_declaration\\'" nil nil)
> > + ("Type" "\\`type_spec\\'" nil nil)
> > + ("Method" "\\`method_declaration\\'" nil nil)))
> >
> > ;; Indent.
> > (setq-local indent-tabs-mode t
> > @@ -247,6 +218,18 @@ go-ts-mode
> >
> > (treesit-major-mode-setup)))
> >
> > +(defun go-ts-mode--defun-name (node)
> > + "Return the defun name of NODE.
> > +Return nil if there is no name or if NODE is not a defun node."
> > + (pcase (treesit-node-type node)
> > + ((or "function_declaration"
> > + "method_declaration"
> > + "type_spec")
> > + (treesit-node-text
> > + (treesit-node-child-by-field-name
> > + node "name")
> > + t))))
> > +
> > ;; go.mod support.
> >
> > (defvar go-mod-ts-mode--syntax-table
> >
> >
> >
> >
next prev parent reply other threads:[~2023-01-01 13:05 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-29 16:05 bug#60407: [PATCH] Update go-ts-mode to use Imenu facility Evgeni Kolev
2023-01-01 9:07 ` Eli Zaretskii
2023-01-01 13:05 ` Evgeni Kolev [this message]
2023-01-01 17:08 ` Evgeni Kolev
2023-01-01 22:31 ` Randy Taylor
2023-01-03 9:01 ` Evgeni Kolev
2023-01-03 14:30 ` Randy Taylor
2023-01-05 7:24 ` Evgeni Kolev
2023-01-05 14:21 ` Randy Taylor
2023-01-01 13:08 ` Randy Taylor
2023-01-08 0:20 ` Yuan Fu
2023-01-08 8:10 ` Evgeni Kolev
2023-01-08 10:59 ` Eli Zaretskii
2023-01-09 0:35 ` Yuan Fu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAMCrgaUbQmXGrUYTF8C_hgu-bUTFmaDQXgjrGcM=i-5JkKpq2g@mail.gmail.com' \
--to=evgenysw@gmail.com \
--cc=60407@debbugs.gnu.org \
--cc=casouri@gmail.com \
--cc=dev@rjt.dev \
--cc=eliz@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.