From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Evgeni Kolev Newsgroups: gmane.emacs.bugs Subject: bug#60805: [PATCH] Extend go-ts-mode with command to add docstring to function Date: Fri, 20 Jan 2023 11:41:33 +0200 Message-ID: References: <83ilh98sln.fsf@gnu.org> <87358dz6hy.fsf@thornhill.no> <83tu0t718s.fsf@gnu.org> <87sfgdtga2.fsf@thornhill.no> <875yd4njdc.fsf@thornhill.no> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="21227"; mail-complaints-to="usenet@ciao.gmane.io" Cc: dev@rjt.dev, Eli Zaretskii , 60805@debbugs.gnu.org To: Theodor Thornhill Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Jan 20 10:43:24 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pInvf-0005KK-GF for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 20 Jan 2023 10:43:23 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pInvO-0007D3-Sf; Fri, 20 Jan 2023 04:43:06 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pInvK-0007Ce-U8 for bug-gnu-emacs@gnu.org; Fri, 20 Jan 2023 04:43:05 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pInvK-0002U8-MH for bug-gnu-emacs@gnu.org; Fri, 20 Jan 2023 04:43:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pInvK-0008Os-2T for bug-gnu-emacs@gnu.org; Fri, 20 Jan 2023 04:43:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Evgeni Kolev Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 20 Jan 2023 09:43:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60805 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 60805-submit@debbugs.gnu.org id=B60805.167420773032219 (code B ref 60805); Fri, 20 Jan 2023 09:43:02 +0000 Original-Received: (at 60805) by debbugs.gnu.org; 20 Jan 2023 09:42:10 +0000 Original-Received: from localhost ([127.0.0.1]:45615 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pInuU-0008Nb-CY for submit@debbugs.gnu.org; Fri, 20 Jan 2023 04:42:10 -0500 Original-Received: from mail-qt1-f181.google.com ([209.85.160.181]:47056) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pInuP-0008N3-Lu for 60805@debbugs.gnu.org; Fri, 20 Jan 2023 04:42:08 -0500 Original-Received: by mail-qt1-f181.google.com with SMTP id x7so3678328qtv.13 for <60805@debbugs.gnu.org>; Fri, 20 Jan 2023 01:42:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=WvCwXraTYkarPw5SO84wQdjC7OPs1scX2vwBukAwv8Y=; b=hxa/I+tzfhq6xm0WitUJqlWNXvyQD15KcLTucy5/yCLvnARxWofKzuTdTooaId9JlO BcXu5vSSI+rldkHyF6TULS1y7v7s/4gpMN8Ia1fC9u2J7AhvBQKxHV4+dAwVlq0lforv 7lfqg4lYvRIz/jaFy7Rmm7LcvuWMD8CK4SC0wJ0jQx+ajzwyVubZjVlIrQSNG5Au+vSq HsyKL4dVh37hg8JJ8DncVNuz8mfOo4wx+Iba2Y5wJUyiGnLQ5x72t0vMjhwb2uTsn/Qq H9T8PuHKJSo2+A6L4Pt62tur2w4OvEueFJEiicSI7XQC5d+YEZKQ6lrMox6ERKfFLHqt 3sEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WvCwXraTYkarPw5SO84wQdjC7OPs1scX2vwBukAwv8Y=; b=J/DuP53VKS6ERvi60P1NJl289M/IDOaM3bKHjRlNz/K09zs4WUpvOGW4/e61RwuR+f ZLc/mWJEGoikmOtoRx0ea1aXNkin+6sZ70oNIjaOQXFmH0nLrzn1J1pOvE7fFHNCSFLO cdnqxEUswxyD3TdD9qMdL3IohfpTA/ND0H5aoCZmPrNhyu6cWKVuDNjNdmo9jAoFuScy GPf6W4XBQLEyMGpMQzhGAtXmZ5N1F1Epg0NjsEZ4NO3Vwf0LupFnQyCB/5uCas2v0ZHf P6E5O2a87N9ia25cT+u/RPCdlmDLexdpmYKA1acF+13tvsWEjTxcuGzAw+nK+X+TLw7K 8jOw== X-Gm-Message-State: AFqh2koCSVdaTYQts1xm9UqLZGZ4HCJaKGZoT5Xl76UMuKCCbZwdT23E FGDUq56YKXlP7e3QF46uC0o/PDDqpjJ90bMxT+g= X-Google-Smtp-Source: AMrXdXuFQ6W0CFwd0tLu+bBjUjO9XbyFumvVDJXd10BzLIhhq94cnA3mSj52rJGh3Bt9bIB5ujw3SAS7Ag2cBZ2uBMQ= X-Received: by 2002:ac8:71d0:0:b0:3b0:1d75:e3b6 with SMTP id i16-20020ac871d0000000b003b01d75e3b6mr709367qtp.170.1674207719919; Fri, 20 Jan 2023 01:41:59 -0800 (PST) In-Reply-To: <875yd4njdc.fsf@thornhill.no> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:253760 Archived-At: > 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))) 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 Date: Sat, 14 Jan 2023 08:28:06 +0200 Subject: [PATCH] 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. * etc/NEWS: Mention the change. --- etc/NEWS | 8 ++++++++ lisp/progmodes/go-ts-mode.el | 30 +++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/etc/NEWS b/etc/NEWS index cde6783349f..68ad90ab189 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -165,6 +165,14 @@ the new argument 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 diff --git a/lisp/progmodes/go-ts-mode.el b/lisp/progmodes/go-ts-mode.el index 64e761d2f72..cc8171e9be6 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,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