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. The command is bound to "C-c C-d". * lisp/progmodes/go-ts-mode.el (go-ts-mode): Extend docstring. (go-ts-mode-docstring): New function. (go-ts-mode--comment-on-previous-line-p): New function. (go-ts-mode-map): New map variable. * etc/NEWS: Mention the change. (bug#60805)
This commit is contained in:
parent
cb07f1bcd0
commit
f0971f94fe
2 changed files with 42 additions and 1 deletions
8
etc/NEWS
8
etc/NEWS
|
@ -181,6 +181,14 @@ the new argument NEW-BUFFER non-nil, it will use a new buffer instead.
|
|||
Interactively, invoke 'eww-open-file' with a prefix argument to
|
||||
activate this behavior.
|
||||
|
||||
** go-ts-mode
|
||||
|
||||
+++
|
||||
*** New command 'go-ts-mode-docstring'.
|
||||
This command adds a docstring comment to the current defun. If a
|
||||
comment already exists, point is only moved to the comment. It is
|
||||
bound to 'C-c C-d' in 'go-ts-mode'.
|
||||
|
||||
|
||||
* New Modes and Packages in Emacs 30.1
|
||||
|
||||
|
|
|
@ -177,9 +177,16 @@
|
|||
;;;###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,32 @@ Return nil if there is no name or if NODE is not a defun node."
|
|||
(not (go-ts-mode--struct-node-p node))
|
||||
(not (go-ts-mode--alias-node-p node))))
|
||||
|
||||
(defun go-ts-mode-docstring ()
|
||||
"Add a docstring comment for the current defun.
|
||||
The added docstring is prefilled with the defun's name. If the
|
||||
comment already exists, jump to it."
|
||||
(interactive)
|
||||
(when-let ((defun-node (treesit-defun-at-point)))
|
||||
(goto-char (treesit-node-start defun-node))
|
||||
(if (go-ts-mode--comment-on-previous-line-p)
|
||||
;; go to top comment line
|
||||
(while (go-ts-mode--comment-on-previous-line-p)
|
||||
(forward-line -1))
|
||||
(insert "// " (treesit-defun-name defun-node))
|
||||
(newline)
|
||||
(backward-char))))
|
||||
|
||||
(defun go-ts-mode--comment-on-previous-line-p ()
|
||||
"Return t if the previous line is a comment."
|
||||
(when-let ((point (- (pos-bol) 1))
|
||||
((> point 0))
|
||||
(node (treesit-node-at point)))
|
||||
(and
|
||||
;; check point is actually inside the found node
|
||||
;; treesit-node-at can return nodes after point
|
||||
(<= (treesit-node-start node) point (treesit-node-end node))
|
||||
(string-equal "comment" (treesit-node-type node)))))
|
||||
|
||||
;; go.mod support.
|
||||
|
||||
(defvar go-mod-ts-mode--syntax-table
|
||||
|
|
Loading…
Add table
Reference in a new issue