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: Sat, 14 Jan 2023 08:45:31 +0200 Message-ID: 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="37237"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Randy Taylor To: 60805@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Jan 14 07:47:26 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 1pGaK6-0009Xd-BJ for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 14 Jan 2023 07:47:26 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pGaJm-0000O7-8L; Sat, 14 Jan 2023 01:47: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 1pGaJl-0000Nc-0J for bug-gnu-emacs@gnu.org; Sat, 14 Jan 2023 01:47: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 1pGaJj-00008q-5w for bug-gnu-emacs@gnu.org; Sat, 14 Jan 2023 01:47:04 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pGaJi-0006uK-Mp for bug-gnu-emacs@gnu.org; Sat, 14 Jan 2023 01:47: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: Sat, 14 Jan 2023 06:47:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 60805 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.167367876526478 (code B ref -1); Sat, 14 Jan 2023 06:47:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 14 Jan 2023 06:46:05 +0000 Original-Received: from localhost ([127.0.0.1]:53060 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pGaIn-0006sz-BK for submit@debbugs.gnu.org; Sat, 14 Jan 2023 01:46:05 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:40422) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pGaIm-0006ss-CH for submit@debbugs.gnu.org; Sat, 14 Jan 2023 01:46:04 -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 1pGaIm-0000Dl-5K for bug-gnu-emacs@gnu.org; Sat, 14 Jan 2023 01:46:04 -0500 Original-Received: from mail-yw1-x112b.google.com ([2607:f8b0:4864:20::112b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pGaIh-0008S1-1L for bug-gnu-emacs@gnu.org; Sat, 14 Jan 2023 01:46:02 -0500 Original-Received: by mail-yw1-x112b.google.com with SMTP id 00721157ae682-4d59d518505so144559127b3.1 for ; Fri, 13 Jan 2023 22:45:58 -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:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=GPHLKfCo14jmHkVU6j+z8vHRqzEA5U91uA3eQ54FbpE=; b=K4VCZBqYTvc7n52UZkykdI2AfJBnzAh4a07QTMR78K1EvX3+6LR61FWqrNtPq/xMcG mSM6s+WXZFCFBIbYP6fuJxrTkP04YQA95hs0KKZCXwf9DpaCwbrkogauCr01nQkWZTzC ejZC6icfIAU5uKsK+CI5RMGHWU/MfJejsfeAJlRHTnUqXSa2svU1ie3oVftv0E7Z56L4 95USmuMOpctAFrxgnnz6eT0/hGzAsBpqFIywg3aJkh4HVg0Ssc5SuUT78Bu7TxrPCQfc +Q5trzzx/xs9/gpixVUWyg+YF9hvy7IiuOgevIo83kbIaekTsEFxnTeHe+ZyErNzj859 u9Lg== 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:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=GPHLKfCo14jmHkVU6j+z8vHRqzEA5U91uA3eQ54FbpE=; b=3cnjzjxO5GssmItMDEm1G7x7B81pgg4bN42J4cuLID457A6bJ+eSis26vigQDXOPp6 ilIO2OQjJAA0Hd75niWHn0NiyuDVGDDwm1e8UZ+VuvadnKh4GwCLjDCgdiKfD45P+ixL gIRjtdlmMJPooF/96RnvWQVEsltaDSgPHyXRYZXo/ukKhODRyiJac/hIlH/hLZoTv1zg p9mf85L0sQbQEwZwg2NdB4aauT6XvKPCmjkM0OaB8kceW6VdyiF99Tb99meYHLT1f4hP vUZ+4uutDAiMZtDq8AksgVkZWcoWrHIDGlg+LEBaDA5v+RiTw5G5myu4CPnL3TzNPLs/ l4ZA== X-Gm-Message-State: AFqh2ko3GShmJV2UazRk+41OWC9q84H0gUe5UxoAZCS8wkDSKR7T6C1G G1h3u/XN5yLR6O8JfbpjKEEMEE4jG6SGhy6eTp5kTeVRnrFtTKBq3es= X-Google-Smtp-Source: AMrXdXvdHbvH5v8MqE8Gwg0OlkQJ3D7t/W3bgEYqn17H/iLaoRxp8GRrEFjAsEZ/39ebCuryLCIt6cZS5wZ+1/TZ5lQ= X-Received: by 2002:a05:690c:d0c:b0:46d:89d7:498f with SMTP id cn12-20020a05690c0d0c00b0046d89d7498fmr2565574ywb.461.1673678757700; Fri, 13 Jan 2023 22:45:57 -0800 (PST) Received-SPF: pass client-ip=2607:f8b0:4864:20::112b; envelope-from=evgenysw@gmail.com; helo=mail-yw1-x112b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:253321 Archived-At: [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 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