unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Evgeni Kolev <evgenysw@gmail.com>
Cc: dev@rjt.dev, Eli Zaretskii <eliz@gnu.org>, 60805@debbugs.gnu.org
Subject: bug#60805: [PATCH] Extend go-ts-mode with command to add docstring to function
Date: Fri, 20 Jan 2023 11:14:50 +0100	[thread overview]
Message-ID: <87r0vpv7fp.fsf@thornhill.no> (raw)
In-Reply-To: <CAMCrgaVg5NzsR-Cbji+xveFMnPrAq6G2EM+ttUaoCj+t6Yuixg@mail.gmail.com>

Evgeni Kolev <evgenysw@gmail.com> writes:

>> What about something like this to locate the first comment above a
>> defun?
>>
>> Theo
>>
>> (defun move-to-top-comment ()
>>   (interactive)
>>   (save-restriction
>>     (narrow-to-defun t)
>>     (let ((comments (cdr (treesit-induce-sparse-tree
>>                           (treesit-buffer-root-node)
>>                           (lambda (n)
>>                             (equal (treesit-node-type n)
>>                                    "comment"))))))
>>       (goto-char
>>        (apply #'treesit-node-start (car comments))))))
>
> Thank you for the suggestion Theo, I learned a lot about
> treesit-induce-sparse-tree! When I tested your suggestion however, I
> found that the tree sitter functions don't take into account the
> narrowing, instead they operate on the whole buffer. So the 'comments'
> variable holds all the buffer's comments, not just the ones above the
> current defun. The suggestion works because (car comments) returns the
> top comment in the buffer and goto-char tries to go to it, but is
> restricted by the narrowing. In other words, this works as well:
>
> (defun move-to-top-comment ()
>   (interactive)
>   (save-restriction
>     (narrow-to-defun t)
>     (goto-char 0)))
>

In that case, maybe it is even simpler to do something like:

(defun defun-comment-p ()
  (interactive)
  (save-restriction
    (narrow-to-defun t)
    (goto-char (point-min))
    (equal
      (treesit-node-type (treesit-node-at (point)))
      "comment")))

But sure, I won't argue :)


However, now this code would act on code such as:

```
//go:generate protoc .........

// some docstring
func main() {

}
```

And return point on the line with go:generate.  Is that desired?


> I'm providing an updated patch below. The algorithm for finding the
> top comment is a while-loop which does (forward-line -1) until the
> previous treesit sibling is not a comment.
>
> I've executed 'checkdoc' this time, and added an /etc/NEWS entry.
>
> The patch is below. Any feedback is appreciated!
>
> From 5cd8a1cb4e491b9db6ed586d88ac79ab52e395f0 Mon Sep 17 00:00:00 2001
> From: Evgeni Kolev <evgenysw@gmail.com>
> Date: Sat, 14 Jan 2023 08:28:06 +0200
> Subject: [PATCH] Extend go-ts-mode with command to add docstring to function
>

> +(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)
> +

Should we bind something to the C-c prefix?  I thought this was
"user-reserved", so a user should bind them themselves.

>  ;;;###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,27 @@ 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 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))
> +             (defun-name (treesit-defun-name defun-node)))
> +    (let ((prev-node (treesit-node-prev-sibling defun-node t)))
> +      (if (string-equal "comment" (treesit-node-type prev-node))
> +          ;; go to the top-most comment line
> +          (progn
> +            (goto-char (treesit-node-start prev-node))
> +            (while-let ((curr-node (treesit-node-at (point)))
> +                        (prev-node (treesit-node-prev-sibling curr-node t))
> +                        ((string-equal "comment" (treesit-node-type
> prev-node))))
> +              (forward-line -1)))
> +        (goto-char (treesit-node-start defun-node))
> +        (newline)
> +        (forward-line -1)
> +        (insert "// " defun-name)))))
> +
>  ;; go.mod support.
>
>  (defvar go-mod-ts-mode--syntax-table
> -- 
> 2.30.2

Thanks,
Theo





  reply	other threads:[~2023-01-20 10:14 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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=87r0vpv7fp.fsf@thornhill.no \
    --to=bug-gnu-emacs@gnu.org \
    --cc=60805@debbugs.gnu.org \
    --cc=dev@rjt.dev \
    --cc=eliz@gnu.org \
    --cc=evgenysw@gmail.com \
    --cc=theo@thornhill.no \
    /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).