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#61368: [PATCH] Extend go-ts-mode with support for pre-filling return statements Date: Thu, 9 Feb 2023 13:47:01 +0200 Message-ID: References: <83edr0nmq6.fsf@gnu.org> 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="5700"; mail-complaints-to="usenet@ciao.gmane.io" Cc: dev@rjt.dev, theo@thornhill.no, 61368@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Feb 09 12:48:36 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 1pQ5Pl-0001Dv-Mk for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 09 Feb 2023 12:48:33 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pQ5PT-0004m0-Rg; Thu, 09 Feb 2023 06:48:15 -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 1pQ5PH-0004ld-CN for bug-gnu-emacs@gnu.org; Thu, 09 Feb 2023 06:48:04 -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 1pQ5PF-0005uT-Oc for bug-gnu-emacs@gnu.org; Thu, 09 Feb 2023 06:48:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pQ5PF-0005H7-L9 for bug-gnu-emacs@gnu.org; Thu, 09 Feb 2023 06:48:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Evgeni Kolev Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 09 Feb 2023 11:48:01 +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 Original-Received: via spool by 61368-submit@debbugs.gnu.org id=B61368.167594325620109 (code B ref 61368); Thu, 09 Feb 2023 11:48:01 +0000 Original-Received: (at 61368) by debbugs.gnu.org; 9 Feb 2023 11:47:36 +0000 Original-Received: from localhost ([127.0.0.1]:57728 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pQ5Op-0005EE-Js for submit@debbugs.gnu.org; Thu, 09 Feb 2023 06:47:36 -0500 Original-Received: from mail-qt1-f174.google.com ([209.85.160.174]:39639) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pQ5On-0005DX-Ar for 61368@debbugs.gnu.org; Thu, 09 Feb 2023 06:47:33 -0500 Original-Received: by mail-qt1-f174.google.com with SMTP id g18so1503590qtb.6 for <61368@debbugs.gnu.org>; Thu, 09 Feb 2023 03:47:33 -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=xabf3RZYrrJ64Lb5TAyVS9s/yGdReZQfPJmmo2NHjSU=; b=i7X7DOfV+1x3t0pLmd7+du8tPv0Y+56iTr35zuo+2ZALQemzuo6JWotM9xrEhnITpC qBDImXbtNm4ZcYQof8GXa2s1NM/k/Wz9QzsAx1iOK8PhKmA3bvY/U/cDaHkytXgzs38F AD7SFF43wya9vkVzSAfEc/qKrpkRzW194SXVEe+I/qAVZ6z/98l8PKAJJRHI6V6wjc1G DPqd96vH6XkbwbItKpARI5SvF1nhZK7JJgINvsYExRU+WD4TZ4NV9F97/YEyh6LMqim+ 3Gelg99I1iy3/K6qFrW0Tjg0WruVjtDPtVUJgJtZNZ7CG43y706qGvJFN9BhRMk2h7rs w3dg== 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=xabf3RZYrrJ64Lb5TAyVS9s/yGdReZQfPJmmo2NHjSU=; b=lOrgZ7px9yzpg0kxTeMCXf5Kq7/iMFgiUtR/NMv/q6Y5HcWvudv+RSybONjECVXDv/ 2dVw/ZaXR6jxk4y0EV20NIDWqvpCU1w6b4wfmzMHDGSCYzG4Phy9IVUPUBL1Hh8EdmCR KE/grqbjoIueepOV1p0m5ybAp96DlbD7MHAqVQmt+3Kmac8RAuva+tPars/PAUI1QqZt HOWVs8juevgYcHXMFxscgq72lanKHR/RCuY0k338jDrr9DcTxORjaqctOfWtmwwQmTum y/EwHVdHO/sURLr7utCSXgLneP1lBmu/YAQek98ASnE46Q620Xpr3mwgKLs5GfTDZY7S RCOw== X-Gm-Message-State: AO0yUKVvyEBB5AkIS6NCL1UfzIfNnHfApdJ61Y4jGbUaD24175Z7zYU1 RFiD27BzWDAEyUPiFZnZn6sQCZpYzjHoYFRaCOE= X-Google-Smtp-Source: AK7set+BQeSDvVgVlPieSrYKk+JqbwoDrcGq4LOivWypT44Sosa5SkVDbQ4jvV9GssbjL+kAwDhMYJKYY3RW5CbNH5g= X-Received: by 2002:a05:622a:1007:b0:3ac:c2b8:13 with SMTP id d7-20020a05622a100700b003acc2b80013mr1868008qte.332.1675943247646; Thu, 09 Feb 2023 03:47:27 -0800 (PST) In-Reply-To: <83edr0nmq6.fsf@gnu.org> 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:255214 Archived-At: On Wed, Feb 8, 2023 at 9:20 PM Theodor Thornhill wrote: > > Evgeni Kolev writes: > > > > > 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? I've never used either of these packages. Is there a consensus which one is preferred for major modes to use? > > 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? > Great suggestion! I did a quick research and this seems to already be implemented in gopls here: https://github.com/golang/tools/blob/master/gopls/internal/lsp/source/completion/statements.go#L179 To use it you have to enable "usePlaceholders", reference: https://github.com/golang/tools/blob/master/gopls/doc/settings.md Do you think it makes sense go-ts-mode to also have its own implementation? I think it does - similar to how both gopls (eglot) and tree sitter provide Imenu candidates. I personally use tree sitter's Imenu (setq eglot-stay-out-of '(imenu)) because I find it advantageous to avoid the RPC call to an external process. Also having a tree sitter implementation is more flexible. For example, I'm planning to have multiple yasnippet snippets for different scenarios (wrap the error, don't wrap, return a new error, etc.). The gopls implementation is ridgid - it only works if there's an "err" variable in scope, and the trigger is one of these three "if e", "if er" and "if err". On Wed, Feb 8, 2023 at 6:30 PM Eli Zaretskii wrote: > > 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. > > Instead of a command bound to a special key sequences, would it > perhaps make more sense to add some kind of "electric" behavior to > "normal" keys? Like perhaps typing RET after inserting "return" would > add the "context-aware return statement"? Then the user could turn on > the electric behavior with some minor mode, and get this behavior, > while avoiding the need to dedicate a key sequence. > > WDYT? Makes sense. I am familiar with electric-pair-mode. I see that it adds a hook in post-self-insert-hook. Is your suggestion to do something like this: - add hook in post-self-insert-hook - in the hook check if RET is typed after an "return" - if yes, replace the "return" with the "context-aware return statement", possibly using (yas-expand-snippet) I'm wondering what customization options would make sense for users - changing the electric trigger from "return" RET to "ret" RET for example. Is there another major mode which can be used as an example for such electric behavior?