From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Evgeni Kolev Newsgroups: gmane.emacs.bugs Subject: bug#60407: [PATCH] Update go-ts-mode to use Imenu facility Date: Thu, 29 Dec 2022 18:05:49 +0200 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16785"; mail-complaints-to="usenet@ciao.gmane.io" To: 60407@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Dec 29 17:07:32 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pAvRM-0004D3-Fj for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 29 Dec 2022 17:07:32 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pAvQu-0004ng-1C; Thu, 29 Dec 2022 11:07:04 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pAvQs-0004nK-Ju for bug-gnu-emacs@gnu.org; Thu, 29 Dec 2022 11:07:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pAvQs-0006fV-Bt for bug-gnu-emacs@gnu.org; Thu, 29 Dec 2022 11:07:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pAvQs-0001XH-78 for bug-gnu-emacs@gnu.org; Thu, 29 Dec 2022 11:07:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Evgeni Kolev Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 29 Dec 2022 16:07:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 60407 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16723299825838 (code B ref -1); Thu, 29 Dec 2022 16:07:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 29 Dec 2022 16:06:22 +0000 Original-Received: from localhost ([127.0.0.1]:60920 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pAvQD-0001W5-JN for submit@debbugs.gnu.org; Thu, 29 Dec 2022 11:06:22 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:35128) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pAvQB-0001Vx-HA for submit@debbugs.gnu.org; Thu, 29 Dec 2022 11:06:20 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pAvQB-0004a3-CK for bug-gnu-emacs@gnu.org; Thu, 29 Dec 2022 11:06:19 -0500 Original-Received: from mail-yb1-xb31.google.com ([2607:f8b0:4864:20::b31]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pAvQ9-0006XG-GZ for bug-gnu-emacs@gnu.org; Thu, 29 Dec 2022 11:06:19 -0500 Original-Received: by mail-yb1-xb31.google.com with SMTP id n78so20989074yba.12 for ; Thu, 29 Dec 2022 08:06:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=ltXNar22Su3E6C0NQNxFkfZClQ8vnGCgk8XmarqghsM=; b=NvOUfufkfNFFocz2Cr/dWlskhrKJUib31d5rAdTC/5Rt2hgTZIyFKmZwgQSWWjk539 XzumOMjovVRcuHGJ0FWH2XrDa7DkMGtEypmJ62f/+kIvcTax0pJTLwsJRs5SqwWhoRoz dgyto9ygu8Xk1L5l+d3hXywC7GdZPjqOanSgvWWQJzpGPQaJvC9+bTwqw9HjAxE7m81u PFkSXz8MHewy8mNxoSDdhHi0rW0+LjtQ8jlwOmfeRlJzBYfcny6q1rcpb7x14bNP/x8/ ksO8wehzwN7luJHr69kMl74ovuegT1bB6XG3IPFi6iXZVTsOBHOyHCupLHPhpbDePcCX mXXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ltXNar22Su3E6C0NQNxFkfZClQ8vnGCgk8XmarqghsM=; b=MeesEeZcwy5hKkXiFb9DmAZPSEAAcOExNg+FY6u/JECcxEtNe9ZOScYN2GdDyXs+XA JI3W2yORJgeaitma8UvGvpUsA8Dik376zNmUkTyk7VGPLhvBGRIvVZ/6W7wPCGhp4Xvl C91ypg6TqCc+r1hWuKdt9BXsfJBpL19jf/7WZBTtlYZXaMKlQncO07zy5Vz1uGMinSsx izKE2hPBRCj/0PAEKSd3/WVEtCW6w7GqZuUaQeLojV5m64ip0/bnFErM92RlWJ96x4MD +ZWhl+iLHV0pdszSW/w3/hp+vH1nwEPo9jkB4BY36EWRjaAugFMuEyuP69zaXzv9zkxR FWCg== X-Gm-Message-State: AFqh2kplapRmCtV13JgBkU7hNKgdCjIP156I0xBjViLtGxlEtocR7ozC r99u4gpRvAM4r7dpeELEiS7nELWHXoEd1+WNGvE4/4S1l5M= X-Google-Smtp-Source: AMrXdXsl3yg0aHsVrwPvLkoV/mck0Falo54wkjACrlq2k1ggLEdKsv+GpEZa+6Gr3VpbBP4Opzr2QD9Sg3hC5UPIE+Y= X-Received: by 2002:a25:ce8c:0:b0:78f:12e9:7e83 with SMTP id x134-20020a25ce8c000000b0078f12e97e83mr318697ybe.587.1672329975788; Thu, 29 Dec 2022 08:06:15 -0800 (PST) Received-SPF: pass client-ip=2607:f8b0:4864:20::b31; envelope-from=evgenysw@gmail.com; helo=mail-yb1-xb31.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:252042 Archived-At: 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. commit 71ff7b21fe92167313bd1761b68b6e6fd879b09f Author: Evgeni Kolev 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