unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: Mickey Petersen <mickey@masteringemacs.org>
Cc: 60655@debbugs.gnu.org
Subject: bug#60655: 30.0.50; tree-sitter: `treesit-transpose-sexps' is broken.
Date: Mon, 30 Dec 2024 20:22:02 +0200	[thread overview]
Message-ID: <87ed1p58vp.fsf@mail.linkov.net> (raw)
In-Reply-To: <87r0w5jo0i.fsf@masteringemacs.org> (Mickey Petersen's message of "Sun, 08 Jan 2023 10:53:33 +0000")

[-- Attachment #1: Type: text/plain, Size: 1087 bytes --]

> 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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: treesit-transpose-sexps.patch --]
[-- Type: text/x-diff, Size: 1796 bytes --]

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
 ;;

  parent reply	other threads:[~2024-12-30 18:22 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-08 10:53 bug#60655: 30.0.50; tree-sitter: `treesit-transpose-sexps' is broken Mickey Petersen
2023-01-09  6:38 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-09  8:48   ` Mickey Petersen
2023-01-09 12:29     ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-09 12:30       ` Mickey Petersen
2023-01-09 13:37         ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-12-30 18:22 ` Juri Linkov [this message]
2025-01-04 18:30   ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ed1p58vp.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=60655@debbugs.gnu.org \
    --cc=mickey@masteringemacs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).