From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#60655: 30.0.50; tree-sitter: `treesit-transpose-sexps' is broken. Date: Mon, 30 Dec 2024 20:22:02 +0200 Organization: LINKOV.NET Message-ID: <87ed1p58vp.fsf@mail.linkov.net> References: <87r0w5jo0i.fsf@masteringemacs.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="17618"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) Cc: 60655@debbugs.gnu.org To: Mickey Petersen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Dec 30 19:26:24 2024 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 1tSKT9-0004NU-J5 for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 30 Dec 2024 19:26:23 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tSKSv-0002P7-Dl; Mon, 30 Dec 2024 13:26:09 -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 1tSKSq-0002NZ-4N for bug-gnu-emacs@gnu.org; Mon, 30 Dec 2024 13:26:06 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tSKSo-0004A2-AU for bug-gnu-emacs@gnu.org; Mon, 30 Dec 2024 13:26:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:References:In-Reply-To:From:To:Subject; bh=wA7ll7AEPsvT9fpB/L8CgYvMImnZO3Mjy42GCEilfSI=; b=NAQyGa9sx2m24jjPzlCxICT99AmGZDScrfRIHIRCnLDtTP0MtnOhgIpnBhl79C3vWn6k9hhjmpeQe9ZI6p3lpfOAy5MS4FW1fqKx+Qs5//Q/B+C87ZnEflK0wr9jfzXKmEYkV9lvaFzXH86hIj0eZa2v916dy8FYibgk/0U7n7oQj6gM2sDE9UcEG/n56yk0DLmjSDt6pWni5yRV7D31BVPndOJJ77nuYRpTefAJiWaVnZVdiCsBqjH/dMDlDoEdNMuEw8EXoxcMxlttw4nii64W0uRo14crWxl0d6UQKttYlWJnmbxVTGVta23MLFvWMS07+aCGw66g/TwI0OH2kw==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tSKSo-0008DJ-4B for bug-gnu-emacs@gnu.org; Mon, 30 Dec 2024 13:26:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 30 Dec 2024 18:26:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60655 X-GNU-PR-Package: emacs Original-Received: via spool by 60655-submit@debbugs.gnu.org id=B60655.173558313731537 (code B ref 60655); Mon, 30 Dec 2024 18:26:02 +0000 Original-Received: (at 60655) by debbugs.gnu.org; 30 Dec 2024 18:25:37 +0000 Original-Received: from localhost ([127.0.0.1]:59921 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tSKSP-0008Ca-3j for submit@debbugs.gnu.org; Mon, 30 Dec 2024 13:25:37 -0500 Original-Received: from relay1-d.mail.gandi.net ([217.70.183.193]:48671) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tSKSM-0008CD-4h for 60655@debbugs.gnu.org; Mon, 30 Dec 2024 13:25:35 -0500 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id E9C57240006; Mon, 30 Dec 2024 18:25:05 +0000 (UTC) In-Reply-To: <87r0w5jo0i.fsf@masteringemacs.org> (Mickey Petersen's message of "Sun, 08 Jan 2023 10:53:33 +0000") X-GND-Sasl: juri@linkov.net 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:298014 Archived-At: --=-=-= Content-Type: text/plain > The tree-sitter-enabled function, `treesit-transpose-sexps', > that is called by transpose-sexps, is broken. After many unsuccessful attempts I finally arrived at the usable implementation. For example, typing 'C-M-t' between two conditions: if (x > 72 && -!-y < 85) { // found do_something(); } swaps these two conditions: if (y < 85 && x > 72) { // found do_something(); } whereas inside the binary expression if (x > -!-72 && y < 85) { // found do_something(); } it swaps its components: if (72 > x && y < 85) { // found do_something(); } Or with point between two objects in an array: var a = [{ case: 'zzzz', default: 'yyyy' }, -!-{ case: 'zzzz2', default: 'yyyy' }]; it swaps these objects: var a = [{ case: 'zzzz2', default: 'yyyy' }, { case: 'zzzz', default: 'yyyy' }]; whereas with point between two pairs var a = [{ case: 'zzzz', -!-default: 'yyyy' }]; it swaps these pairs var a = [{ default: 'yyyy', case: 'zzzz' }]; while keeping the right punctuation. In a string it falls back to the default function. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=treesit-transpose-sexps.patch diff --git a/lisp/treesit.el b/lisp/treesit.el index 4fe4f7276f6..c17ffb1f9f4 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -2608,26 +2630,15 @@ treesit-transpose-sexps Return a pair of positions as described by `transpose-sexps-function' for use in `transpose-subr' and friends." - ;; First arrive at the right level at where the node at point is - ;; considered a sexp. If sexp isn't defined, or we can't find any - ;; node that's a sexp, use the node at point. - (let* ((node (or (treesit-thing-at-point 'sexp 'nested) - (treesit-node-at (point)))) - (parent (treesit-node-parent node)) - (child (treesit-node-child parent 0 t))) - (named-let loop ((prev child) - (next (treesit-node-next-sibling child t))) - (when (and prev next) - (if (< (point) (treesit-node-end next)) - (if (= arg -1) - (cons (treesit-node-start prev) - (treesit-node-end prev)) - (when-let* ((n (treesit-node-child - parent (+ arg (treesit-node-index prev t)) t))) - (cons (treesit-node-end n) - (treesit-node-start n)))) - (loop (treesit-node-next-sibling prev t) - (treesit-node-next-sibling next t))))))) + (let* ((pred #'treesit-node-named) + (sibling (if (> arg 0) + (treesit-thing-next (point) pred) + (treesit-thing-prev (point) pred)))) + (or (when sibling + (if (> arg 0) + (cons (treesit-node-end sibling) (treesit-node-start sibling)) + (cons (treesit-node-start sibling) (treesit-node-end sibling)))) + (transpose-sexps-default-function arg)))) ;;; Navigation, defun, things ;; --=-=-=--