From: Evgeni Kolev <evgenysw@gmail.com>
To: 60805@debbugs.gnu.org
Cc: Randy Taylor <dev@rjt.dev>
Subject: bug#60805: [PATCH] Extend go-ts-mode with command to add docstring to function
Date: Sat, 14 Jan 2023 08:45:31 +0200 [thread overview]
Message-ID: <CAMCrgaUDsPdV8PPb2w+CRN8uqoL6gn-NtWNEPwVcCGTiR=HT7Q@mail.gmail.com> (raw)
[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
next reply other threads:[~2023-01-14 6:45 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-14 6:45 Evgeni Kolev [this message]
2023-01-14 7:48 ` bug#60805: [PATCH] Extend go-ts-mode with command to add docstring to function 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
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAMCrgaUDsPdV8PPb2w+CRN8uqoL6gn-NtWNEPwVcCGTiR=HT7Q@mail.gmail.com' \
--to=evgenysw@gmail.com \
--cc=60805@debbugs.gnu.org \
--cc=dev@rjt.dev \
/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 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).