unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#62371: [PATCH] Fix go-ts-mode incorrect docstring inserted for methods
@ 2023-03-22  8:05 Evgeni Kolev
  2023-03-24 18:21 ` Yuan Fu
  2023-03-25 12:17 ` Eli Zaretskii
  0 siblings, 2 replies; 11+ messages in thread
From: Evgeni Kolev @ 2023-03-22  8:05 UTC (permalink / raw)
  To: 62371

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

The docstring inserted with go-ts-mode's C-c C-d was incorrectly
prefixed with the receiver "(myStruct).":

    // (myStruct).act
    func (m *myStruct) act () {...}

The above docstring is not correct because the receiver "myStruct"
should not be in the docstring.

The issue is caused by imenu and go-ts-mode--defun-name using the same
code to determine the defun name. Instead, they should produce
different results - imenu should show the "myStruct" prefix, but the
docstring should not.

This commit fixes the incorrect behavior by introducing an optional
SKIP-PREFIX parameter to (go-ts-mode--defun-name). Pressing C-c C-d
now inserts just the method name:

    // act
    func (m *myStruct) act () {...}

* lisp/progmodes/go-ts-mode.el (go-ts-mode--defun-name): New optional
parameter SKIP-PREFIX. (go-ts-mode-docstring):
Call (go-ts-mode--defun-name t) instead of (treesit-defun-name).

[-- Attachment #2: 0001-Fix-go-ts-mode-incorrect-docstring-inserted-for-meth.patch --]
[-- Type: application/octet-stream, Size: 3069 bytes --]

From 43ec08412868099735b229e4aeff5eb276c4f082 Mon Sep 17 00:00:00 2001
From: Evgeni Kolev <evgenysw@gmail.com>
Date: Wed, 8 Feb 2023 17:16:02 +0200
Subject: [PATCH] Fix go-ts-mode incorrect docstring inserted for methods

The docstring inserted with go-ts-mode's C-c C-d was incorrectly
prefixed with the receiver "(myStruct).":

    // (myStruct).act
    func (m *myStruct) act () {...}

The above docstring is not correct because the receiver "myStruct"
should not be in the docstring.

The issue is caused by imenu and go-ts-mode--defun-name using the same
code to determine the defun name. Instead, they should produce
different results - imenu should show the "myStruct" prefix, but the
docstring should not.

This commit fixes the incorrect behavior by introducing an optional
SKIP-PREFIX parameter to (go-ts-mode--defun-name). Pressing C-c C-d
now inserts just the method name:

    // act
    func (m *myStruct) act () {...}

* lisp/progmodes/go-ts-mode.el (go-ts-mode--defun-name): New optional
parameter SKIP-PREFIX. (go-ts-mode-docstring):
Call (go-ts-mode--defun-name t) instead of (treesit-defun-name).
---
 lisp/progmodes/go-ts-mode.el | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/lisp/progmodes/go-ts-mode.el b/lisp/progmodes/go-ts-mode.el
index e6e8abd6445..fda6a36e42d 100644
--- a/lisp/progmodes/go-ts-mode.el
+++ b/lisp/progmodes/go-ts-mode.el
@@ -255,9 +255,10 @@ go-ts-mode
 (if (treesit-ready-p 'go)
     (add-to-list 'auto-mode-alist '("\\.go\\'" . go-ts-mode)))
 
-(defun go-ts-mode--defun-name (node)
+(defun go-ts-mode--defun-name (node &optional skip-prefix)
   "Return the defun name of NODE.
-Return nil if there is no name or if NODE is not a defun node."
+Return nil if there is no name or if NODE is not a defun node.
+Methods are prefixed with the receiver name, unless SKIP-PREFIX is t."
   (pcase (treesit-node-type node)
     ("function_declaration"
      (treesit-node-text
@@ -266,11 +267,10 @@ go-ts-mode--defun-name
       t))
     ("method_declaration"
      (let* ((receiver-node (treesit-node-child-by-field-name node "receiver"))
-            (type-node (treesit-search-subtree receiver-node "type_identifier"))
-            (name-node (treesit-node-child-by-field-name node "name")))
-       (concat
-        "(" (treesit-node-text type-node) ")."
-        (treesit-node-text name-node))))
+            (receiver (treesit-node-text (treesit-search-subtree receiver-node "type_identifier")))
+            (method (treesit-node-text (treesit-node-child-by-field-name node "name"))))
+       (if skip-prefix method
+         (concat "(" receiver ")." method))))
     ("type_declaration"
      (treesit-node-text
       (treesit-node-child-by-field-name
@@ -314,7 +314,7 @@ go-ts-mode-docstring
         ;; go to top comment line
         (while (go-ts-mode--comment-on-previous-line-p)
           (forward-line -1))
-      (insert "// " (treesit-defun-name defun-node))
+      (insert "// " (go-ts-mode--defun-name defun-node t))
       (newline)
       (backward-char))))
 
-- 
2.39.1


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

end of thread, other threads:[~2023-03-27 10:04 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-22  8:05 bug#62371: [PATCH] Fix go-ts-mode incorrect docstring inserted for methods Evgeni Kolev
2023-03-24 18:21 ` Yuan Fu
2023-03-25  0:58   ` Randy Taylor
2023-03-25 12:17 ` Eli Zaretskii
2023-03-25 19:37   ` Randy Taylor
2023-03-26  4:34     ` Eli Zaretskii
2023-03-26 11:44       ` Randy Taylor
2023-03-26 12:05         ` Eli Zaretskii
2023-03-27 10:04           ` Evgeni Kolev
2023-03-26  1:56   ` Dmitry Gutov
2023-03-26  5:22     ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).