unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#60805: [PATCH] Extend go-ts-mode with command to add docstring to function
@ 2023-01-14  6:45 Evgeni Kolev
  2023-01-14  7:48 ` Eli Zaretskii
  0 siblings, 1 reply; 19+ messages in thread
From: Evgeni Kolev @ 2023-01-14  6:45 UTC (permalink / raw)
  To: 60805; +Cc: Randy Taylor

[CC Randy Taylor, author of go-ts-mode]

This patch extends go-ts-mode with a command to add docstring to
current function. If a comment already exists, the point is instead
moved to the top-most comment line.

Tested with the following Go code:

First test, with this input, point inside the function body.
```
func test1(a, b int) int {
    return a + b
}
```

Pressing C-c C-d results in:
```
// test1
func test1(a, b int) int {
    return a + b
}
```

Second test, with this input, point inside the function body.
```
// multiple line
// pre-existing comment
func test2(a, b int) int {
    return a + b
}
```
Pressing C-c C-d does not change the buffer, point is moved to
beginning of  this line "// multiple line".

Third test, the new behavior works identically for all "defun" types
recognized by go-ts-mode:
```
type MyInt = int
type Option func(s string)
type List[T any] struct {
    head, tail *element[T]
}
type Number interface {
    int64 | float64
}
```

The patch is below.

commit e47f7f256a71aaae2ed645c0ea67afc90e956071
Author: Evgeni Kolev <evgenysw@gmail.com>
Date:   Sat Jan 14 08:28:06 2023 +0200

    Extend go-ts-mode with command to add docstring to function

    go-ts-mode is extended with command go-ts-mode-docstring which adds
    docstring comment to the defun at point. If a comment already exists,
    the point is instead moved to the top-most comment line.

    * lisp/progmodes/go-ts-mode.el (go-ts-mode): Extend docstring.
    (go-ts-mode-docstring): New function.
    (go-ts-mode-map): New map variable.

diff --git a/lisp/progmodes/go-ts-mode.el b/lisp/progmodes/go-ts-mode.el
index 64e761d2f72..f8b0289ca5b 100644
--- a/lisp/progmodes/go-ts-mode.el
+++ b/lisp/progmodes/go-ts-mode.el
@@ -177,9 +177,16 @@ go-ts-mode--font-lock-settings
 ;;;###autoload
 (add-to-list 'auto-mode-alist '("\\.go\\'" . go-ts-mode))

+(defvar-keymap go-ts-mode-map
+  :doc "Keymap used in Go mode, powered by tree-sitter"
+  :parent prog-mode-map
+  "C-c C-d" #'go-ts-mode-docstring)
+
 ;;;###autoload
 (define-derived-mode go-ts-mode prog-mode "Go"
-  "Major mode for editing Go, powered by tree-sitter."
+  "Major mode for editing Go, powered by tree-sitter.
+
+\\{go-ts-mode-map}"
   :group 'go
   :syntax-table go-ts-mode--syntax-table

@@ -274,6 +281,25 @@ go-ts-mode--other-type-node-p
    (not (go-ts-mode--struct-node-p node))
    (not (go-ts-mode--alias-node-p node))))

+(defun go-ts-mode-docstring ()
+    "Add docstring for the current function if it does not
+exists. Otherwise, jump to it."
+    (interactive)
+    (when-let* ((defun-node (treesit-defun-at-point))
+                (defun-name (treesit-defun-name defun-node))
+                (defun-start (treesit-node-start defun-node)))
+      (let ((prev-node (treesit-node-at (- defun-start 1))))
+        (if (string-equal "comment" (treesit-node-type prev-node))
+            (progn
+              (goto-char (treesit-node-start prev-node))
+              ;; go to the top-most comment line
+              (while (string-equal "comment" (treesit-node-type
(treesit-node-at (- (point) 1))))
+                (forward-line -1)))
+          (goto-char defun-start)
+          (newline)
+          (forward-line -1)
+          (insert "// " defun-name)))))
+
 ;; go.mod support.

 (defvar go-mod-ts-mode--syntax-table





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

end of thread, other threads:[~2023-01-21 21:26 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-14  6:45 bug#60805: [PATCH] Extend go-ts-mode with command to add docstring to function Evgeni Kolev
2023-01-14  7:48 ` Eli Zaretskii
2023-01-14 11:43   ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-14 12:24     ` Eli Zaretskii
2023-01-14 13:08       ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-18  6:26         ` Evgeni Kolev
2023-01-18 11:59           ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-20  9:41             ` Evgeni Kolev
2023-01-20 10:14               ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-20 11:39                 ` Evgeni Kolev
2023-01-20 14:56                   ` Evgeni Kolev
2023-01-20 20:39                     ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-21  3:30                       ` Randy Taylor
2023-01-21  6:48                         ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-21  7:34                           ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-21 14:39                             ` Evgeni Kolev
2023-01-21 20:37                               ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-21 21:26                               ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-18  7:46       ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors

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