From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#61368: [PATCH] Extend go-ts-mode with support for pre-filling return statements Date: Wed, 08 Feb 2023 20:20:27 +0100 Message-ID: <87o7q46k1w.fsf@thornhill.no> References: Reply-To: Theodor Thornhill Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25662"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Randy Taylor To: evgenysw@gmail.com, 61368@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Feb 08 20:21:25 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 1pPq0S-0006QA-GS for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 08 Feb 2023 20:21:24 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pPq0B-0003Zk-JD; Wed, 08 Feb 2023 14:21:07 -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 1pPq07-0003ZV-Ix for bug-gnu-emacs@gnu.org; Wed, 08 Feb 2023 14:21:03 -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 1pPq06-00052z-PY for bug-gnu-emacs@gnu.org; Wed, 08 Feb 2023 14:21:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pPq06-0001Aw-7l for bug-gnu-emacs@gnu.org; Wed, 08 Feb 2023 14:21:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 08 Feb 2023 19:21:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 61368 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: Evgeni Kolev , bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16758840384469 (code B ref -1); Wed, 08 Feb 2023 19:21:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 8 Feb 2023 19:20:38 +0000 Original-Received: from localhost ([127.0.0.1]:56701 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pPpzi-0001A1-Bv for submit@debbugs.gnu.org; Wed, 08 Feb 2023 14:20:38 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:48136) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pPpze-00019q-Ak for submit@debbugs.gnu.org; Wed, 08 Feb 2023 14:20:36 -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 1pPpze-0003Ve-4M for bug-gnu-emacs@gnu.org; Wed, 08 Feb 2023 14:20:34 -0500 Original-Received: from out-165.mta1.migadu.com ([2001:41d0:203:375::a5]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pPpza-0004yW-Hm for bug-gnu-emacs@gnu.org; Wed, 08 Feb 2023 14:20:33 -0500 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thornhill.no; s=key1; t=1675884028; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=5Xnl/zb6YScx0+0e9VRnmchqHJz5WXBIyKXkUrMgR2c=; b=PgQsIUoy+zSeX5IXGr4T5Tq3Uokj8w2x85qsN/3KmKukkFhxcP3Nu6MH8u59EAEPFoqqUp RsOVckDwYxw0d2UuffvQrZnMGlFhicJDj3wlHNYY9gipyRz7MvQgCaDEvKUSAuvVxhRghz iHfaY04I9Eq4w9GO3fCifXnobpypM6nBxui77US4Mxd0ItQ++CcbC+LAbghu65UYhrojMS gh0Ci8dHmdcm67Ak8RnUS4Ug015H0UGLKBF19g6urWS3JfkpAyfWcbCI9d59SUXQfdssDb KXb5KLIeD6BhK7Ml/ZDMNFDzLeGAL9iqlAmYYejEoz6cm9RYNit/TH4M4tzBYg== In-Reply-To: X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=2001:41d0:203:375::a5; envelope-from=theo@thornhill.no; helo=out-165.mta1.migadu.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, 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:255161 Archived-At: Evgeni Kolev writes: > [CC Randy, author of go-ts-mode, Theo, who asked to keep the patches coming :)] > :-) > This is a patch which adds support to go-ts-mode to insert > context-aware return statements for the current function. The return > statements are pre-filled with the Go zero values of the output > parameters. > > For example, all these return statements are inserted by M-x > go-ts-mode-insert-return: > > ``` > func f() (x, y, z int) { > return 0, 0, 0 > } > > func exotic() (bool, int, myStruct, *int, chan bool, error) { > return false, 0, myStruct{}, nil, nil, err > } > > func closure(numbers []int) { > sort.Slice(numbers, func(i, j int) bool { > return false > }) > } > ``` Cool! > > The command go-ts-mode-insert-return is experimentally bound to a key > C-c C-r ("r" as return statement). It's a user error to run C-c C-r > outside of a function body. > > Customization: when the output params contain "error" the pre-filled > text is "err". A customization variable is added to allow the user to > pick a different value, for example they might pick `fmt.Errorf("...: > %w", err)` which will result in error wrapping: > ``` > func f() (int, error) { > return 0, fmt.Errorf("...: %w", err) > } > ``` > > Personally, I'll use yasnippet instead of C-c C-r. Something like this: > ``` > # -*- mode: snippet -*- > # name: ret > # key: ret > # type: command > # -- > > (let ((go-ts-mode-error-zero-value "${1:fmt.Errorf(\"${2:format}: > %w\", err)}")) > (yas-expand-snippet (go-ts-mode-return))) > ``` > > I'm open to suggestions about how to best expose this functionality to > the user. I think a snippet makes the most sense, but there's no > standard way for major modes to expose snippets as far as I'm aware. > It's possible to tweak C-c C-r to call (yas-expand-snippet) if > available, otherwise call (insert). In general, I don't feel strong > about the C-c C-r key binding, but I didn't have a better idea. > How about using tempo or skeleton as fallbacks when yasnippet isn't installed? > Known bug: this example does not work, fails with error "Unknown Go > type "[int]int"": > ``` > func notOk() (int, map[int]int) { > } > ``` > The root cause is an issue with the tree-sitter-go parser. The bug has > been reported here > https://github.com/tree-sitter/tree-sitter-go/issues/107 > > Known limitation: unfamiliar types are assumed to be structs. This > assumption does not work for aliased types. For example: > ``` > type MyInt = int > > func alias() MyInt { > return MyInt{} > } > ``` > Above the correct return statement is "return MyInt(0)". My assumption > is that type aliases of primitive types are rare in Go codebases. In > these rare cases, the user is expected to replace "MyInt{}" with > "MyInt(0)", or not use C-c C-r at all. > > Inspired by: this emacs conf talk "08:21 Intelligent templates" > https://emacsconf.org/2022/talks/treesitter/ > > Feedback is more than welcome! In particular: > - how to best expose the functionality - key binding, snippet (how > exactly?), something else? > Personally I think this should be a contrib to gopls as a code action so that others can benefit from it. Is upstreaming it to gopls too hard? Theo