GNU bug report logs - #60655
30.0.50; tree-sitter: `treesit-transpose-sexps' is broken.

Previous Next

Package: emacs;

Reported by: Mickey Petersen <mickey <at> masteringemacs.org>

Date: Sun, 8 Jan 2023 10:54:01 UTC

Severity: normal

Found in version 30.0.50

Fixed in version 31.0.50

Done: Juri Linkov <juri <at> linkov.net>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 60655 in the body.
You can then email your comments to 60655 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#60655; Package emacs. (Sun, 08 Jan 2023 10:54:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Mickey Petersen <mickey <at> masteringemacs.org>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sun, 08 Jan 2023 10:54:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Mickey Petersen <mickey <at> masteringemacs.org>
To: bug-gnu-emacs <at> gnu.org
Subject: 30.0.50; tree-sitter: `treesit-transpose-sexps' is broken.
Date: Sun, 08 Jan 2023 10:53:33 +0000
The tree-sitter-enabled function, `treesit-transpose-sexps', that is called by transpose-sexps, is broken.

It uses a naive method of sibling adjacency to determine transpositions. But it is unfortunately not correct.

Python:


  def -!-foo():
      pass
   
Turns into this with `C-M-t':

  def ()foo:
      pass

But it ought to be:

  foo def():
      pass


It's swapping two siblings that are indeed adjacent in the tree, but not on screen, which is confusing and a regression from its previous behaviour. 

You could make a cogent argument that both approaches are wrong from a syntactic perspective, but I think that misses the broader point that `C-M-t' now does something errant and unexpected.

Worse, it's not possible to revert to the old behaviour (see bug#60654)



In GNU Emacs 30.0.50 (build 6, x86_64-pc-linux-gnu, GTK+ Version
 3.24.20, cairo version 1.16.0) of 2023-01-02 built on mickey-work
Repository revision: c209802f7b3721a1b95113290934a23fee88f678
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12013000
System Description: Ubuntu 20.04.3 LTS




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60655; Package emacs. (Mon, 09 Jan 2023 06:39:02 GMT) Full text and rfc822 format available.

Message #8 received at 60655 <at> debbugs.gnu.org (full text, mbox):

From: Theodor Thornhill <theo <at> thornhill.no>
To: Mickey Petersen <mickey <at> masteringemacs.org>
Cc: 60655 <at> debbugs.gnu.org
Subject: Re: bug#60655: 30.0.50; tree-sitter: `treesit-transpose-sexps' is
 broken.
Date: Mon, 09 Jan 2023 07:38:16 +0100
Mickey Petersen <mickey <at> masteringemacs.org> > The tree-sitter-enabled function, `treesit-transpose-sexps', that is called by transpose-sexps, is broken.
>
> It uses a naive method of sibling adjacency to determine
> transpositions. But it is unfortunately not correct.
>
> Python:
>
>
>   def -!-foo():
>       pass
>    
> Turns into this with `C-M-t':
>
>   def ()foo:
>       pass
>
> But it ought to be:
>
>   foo def():
>       pass
>
>
> It's swapping two siblings that are indeed adjacent in the tree, but
> not on screen, which is confusing and a regression from its previous
> behaviour.
>

I can try to make transpose-sexps rely on only swapping "allowed"
node-types?  That would be able to keep the new, better function, yet
still disallow these syntax-breaking transposes.  What do you think?

> You could make a cogent argument that both approaches are wrong from a
> syntactic perspective, but I think that misses the broader point that
> `C-M-t' now does something errant and unexpected.

I don't really see how "foo def():" is any better at all.  We gain some
great improvements with this "naive" method - namely:

if 5 + 5 == 10 then 10 else 100 + 100.  If point is on the else the 100
+ 100 wil be swapped by 10, but the old behavior will be broken. 

>
> Worse, it's not possible to revert to the old behaviour (see
> bug#60654)
>
>

Right.

Thanks,
Theo




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60655; Package emacs. (Mon, 09 Jan 2023 08:56:02 GMT) Full text and rfc822 format available.

Message #11 received at 60655 <at> debbugs.gnu.org (full text, mbox):

From: Mickey Petersen <mickey <at> masteringemacs.org>
To: Theodor Thornhill <theo <at> thornhill.no>
Cc: 60655 <at> debbugs.gnu.org
Subject: Re: bug#60655: 30.0.50; tree-sitter: `treesit-transpose-sexps' is
 broken.
Date: Mon, 09 Jan 2023 08:48:04 +0000
Theodor Thornhill <theo <at> thornhill.no> writes:

> Mickey Petersen <mickey <at> masteringemacs.org> > The tree-sitter-enabled
> function, `treesit-transpose-sexps', that is called by
> transpose-sexps, is broken.
>>
>> It uses a naive method of sibling adjacency to determine
>> transpositions. But it is unfortunately not correct.
>>
>> Python:
>>
>>
>>   def -!-foo():
>>       pass
>>
>> Turns into this with `C-M-t':
>>
>>   def ()foo:
>>       pass
>>
>> But it ought to be:
>>
>>   foo def():
>>       pass
>>
>>
>> It's swapping two siblings that are indeed adjacent in the tree, but
>> not on screen, which is confusing and a regression from its previous
>> behaviour.
>>
>
> I can try to make transpose-sexps rely on only swapping "allowed"
> node-types?  That would be able to keep the new, better function, yet
> still disallow these syntax-breaking transposes.  What do you think?
>

This is a hard problem. I'm building the self-same in Combobulate, so
when I saw this implementation I saw a well-trodden path by myself.
There's a lot of subtlety to it, and it is not immediately possible to
accurately gauge the right things to swap with simple (or not so
simple) sibling transpositions.

Using a defined list is better, but with the caveat that it requires manual
intervention per mode. This is a really tricky thing to build well.



>> You could make a cogent argument that both approaches are wrong from a
>> syntactic perspective, but I think that misses the broader point that
>> `C-M-t' now does something errant and unexpected.
>
> I don't really see how "foo def():" is any better at all.  We gain some
> great improvements with this "naive" method - namely:
>
> if 5 + 5 == 10 then 10 else 100 + 100.  If point is on the else the 100
> + 100 wil be swapped by 10, but the old behavior will be broken.
>

The old behaviour was consistent. It had a simple *modus operandi*:
swap two things around point. As someone who has used `C-M-t' for
decades, I know what it'll do in pretty much all situations, because I
know what `C-M-k` and `C-M-f/b` do at all times.

Neither approach is great if you holistically approach this task as
"making it correct at all times", and it is easy to confect scenarios
that result in something that is semantically wrong, but syntactically
correct; something that is plain wrong, both semantically and
syntactically; and something that is occasionally correct.

'Like' siblings are an easy way out of this mess with the caveat, as
you'll see, but now you need to carefully pluck the right nodes from
the tree!

Consider the node type `pair' in a dict in Python. They are easily transposable for
that very reason, notwithstanding the anonymous "," node betwixt them.

That is why Combobulate has a list of stuff that it can safely
transpose, and for everything else it defaults to the "classic"
transpose.

>>
>> Worse, it's not possible to revert to the old behaviour (see
>> bug#60654)
>>
>>

Thanks for fixing that!

Kind regards,

Mickey.

>
> Right.
>
> Thanks,
> Theo





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60655; Package emacs. (Mon, 09 Jan 2023 12:30:02 GMT) Full text and rfc822 format available.

Message #14 received at 60655 <at> debbugs.gnu.org (full text, mbox):

From: Theodor Thornhill <theo <at> thornhill.no>
To: Mickey Petersen <mickey <at> masteringemacs.org>
Cc: 60655 <at> debbugs.gnu.org
Subject: Re: bug#60655: 30.0.50; tree-sitter: `treesit-transpose-sexps' is
 broken.
Date: Mon, 09 Jan 2023 13:29:12 +0100
Mickey Petersen <mickey <at> masteringemacs.org> writes:

> Theodor Thornhill <theo <at> thornhill.no> writes:
>
>> Mickey Petersen <mickey <at> masteringemacs.org> > The tree-sitter-enabled
>> function, `treesit-transpose-sexps', that is called by
>> transpose-sexps, is broken.
>>>
>>> It uses a naive method of sibling adjacency to determine
>>> transpositions. But it is unfortunately not correct.
>>>
>>> Python:
>>>
>>>
>>>   def -!-foo():
>>>       pass
>>>
>>> Turns into this with `C-M-t':
>>>
>>>   def ()foo:
>>>       pass
>>>
>>> But it ought to be:
>>>
>>>   foo def():
>>>       pass
>>>
>>>
>>> It's swapping two siblings that are indeed adjacent in the tree, but
>>> not on screen, which is confusing and a regression from its previous
>>> behaviour.
>>>
>>
>> I can try to make transpose-sexps rely on only swapping "allowed"
>> node-types?  That would be able to keep the new, better function, yet
>> still disallow these syntax-breaking transposes.  What do you think?
>>
>
> This is a hard problem. I'm building the self-same in Combobulate, so
> when I saw this implementation I saw a well-trodden path by myself.
> There's a lot of subtlety to it, and it is not immediately possible to
> accurately gauge the right things to swap with simple (or not so
> simple) sibling transpositions.
>
> Using a defined list is better, but with the caveat that it requires manual
> intervention per mode. This is a really tricky thing to build well.

Yeah, but I guess that is a sensible change.  It isn't easy, no, so I'm
open for suggestions and improvements.  IMO an improvement would be to
increase the likelihood that a transpose-sexps will still be valid code.
I don't really think it is useful to do things like "def foo() -> foo
def()" because that is nonsensical code, and is covered by
transpose-words anyway.  To me a _more_ sensible approach here would be
to transpose the defun at point with the next one, as they are usually
interchangeable.  I am looking into such an improvement, and have been
for a while.

>
>
>
>>> You could make a cogent argument that both approaches are wrong from a
>>> syntactic perspective, but I think that misses the broader point that
>>> `C-M-t' now does something errant and unexpected.
>>
>> I don't really see how "foo def():" is any better at all.  We gain some
>> great improvements with this "naive" method - namely:
>>
>> if 5 + 5 == 10 then 10 else 100 + 100.  If point is on the else the 100
>> + 100 wil be swapped by 10, but the old behavior will be broken.
>>
>
> The old behaviour was consistent. It had a simple *modus operandi*:
> swap two things around point. As someone who has used `C-M-t' for
> decades, I know what it'll do in pretty much all situations, because I
> know what `C-M-k` and `C-M-f/b` do at all times.

It may be consistent, but imo it is too close to transpose-words, and
too likely to create useless code in non-lisp languages.

>
> Neither approach is great if you holistically approach this task as
> "making it correct at all times", and it is easy to confect scenarios
> that result in something that is semantically wrong, but syntactically
> correct; something that is plain wrong, both semantically and
> syntactically; and something that is occasionally correct.
>

I see what you mean, but to me semantically _and_ syntactically correct
is the benefit we should pursue when we actually have the parse tree.
The current implementation will semantically correct in many interesting
cases, such as the one I outlined, and is a huge improvement to the
current "transpose-words"-like behavior.  

> 'Like' siblings are an easy way out of this mess with the caveat, as
> you'll see, but now you need to carefully pluck the right nodes from
> the tree!
>
> Consider the node type `pair' in a dict in Python. They are easily transposable for
> that very reason, notwithstanding the anonymous "," node betwixt them.
>
> That is why Combobulate has a list of stuff that it can safely
> transpose, and for everything else it defaults to the "classic"
> transpose.
>

Yeah, such an approach seems reasonable, and there is already precedence
in defining such "things" in Emacs.  As for the default fallback, I'll
see what I can do in the "treesit-transpose-sexps" function.  The
machinery in transpose-subr and friends is a little finicky, so to
adhere to that mechanism isn't the easiest thing.

>>>
>>> Worse, it's not possible to revert to the old behaviour (see
>>> bug#60654)
>>>
>>>
>
> Thanks for fixing that!
>

No problem - hopefully it is installed pretty soon.

Theo




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60655; Package emacs. (Mon, 09 Jan 2023 12:47:02 GMT) Full text and rfc822 format available.

Message #17 received at 60655 <at> debbugs.gnu.org (full text, mbox):

From: Mickey Petersen <mickey <at> masteringemacs.org>
To: Theodor Thornhill <theo <at> thornhill.no>
Cc: 60655 <at> debbugs.gnu.org
Subject: Re: bug#60655: 30.0.50; tree-sitter: `treesit-transpose-sexps' is
 broken.
Date: Mon, 09 Jan 2023 12:30:11 +0000
Theodor Thornhill <theo <at> thornhill.no> writes:

> Mickey Petersen <mickey <at> masteringemacs.org> writes:
>
>> Theodor Thornhill <theo <at> thornhill.no> writes:
>>
>>> Mickey Petersen <mickey <at> masteringemacs.org> > The tree-sitter-enabled
>>> function, `treesit-transpose-sexps', that is called by
>>> transpose-sexps, is broken.
>>>>
>>>> It uses a naive method of sibling adjacency to determine
>>>> transpositions. But it is unfortunately not correct.
>>>>
>>>> Python:
>>>>
>>>>
>>>>   def -!-foo():
>>>>       pass
>>>>
>>>> Turns into this with `C-M-t':
>>>>
>>>>   def ()foo:
>>>>       pass
>>>>
>>>> But it ought to be:
>>>>
>>>>   foo def():
>>>>       pass
>>>>
>>>>
>>>> It's swapping two siblings that are indeed adjacent in the tree, but
>>>> not on screen, which is confusing and a regression from its previous
>>>> behaviour.
>>>>
>>>
>>> I can try to make transpose-sexps rely on only swapping "allowed"
>>> node-types?  That would be able to keep the new, better function, yet
>>> still disallow these syntax-breaking transposes.  What do you think?
>>>
>>
>> This is a hard problem. I'm building the self-same in Combobulate, so
>> when I saw this implementation I saw a well-trodden path by myself.
>> There's a lot of subtlety to it, and it is not immediately possible to
>> accurately gauge the right things to swap with simple (or not so
>> simple) sibling transpositions.
>>
>> Using a defined list is better, but with the caveat that it requires manual
>> intervention per mode. This is a really tricky thing to build well.
>
> Yeah, but I guess that is a sensible change.  It isn't easy, no, so I'm
> open for suggestions and improvements.  IMO an improvement would be to
> increase the likelihood that a transpose-sexps will still be valid code.
> I don't really think it is useful to do things like "def foo() -> foo
> def()" because that is nonsensical code, and is covered by
> transpose-words anyway.  To me a _more_ sensible approach here would be
> to transpose the defun at point with the next one, as they are usually
> interchangeable.  I am looking into such an improvement, and have been
> for a while.
>

That, in my opinion, is the wrong way to look at it.

`C-M-t' already works well: it transposes stuff around point. Nothing
more, nothing less.

If I write rubbish code as a human, no amount of machine intelligence
will (yet) undo that. Nor should a 'clever' mechanism that is only
clever by half. Trying to transpose things on, near or around
point is a useful addition if, and only if, it can do so in a manner
that is sensible, and predictable, to its operator.

You will very quickly run into umpteen problems generalizing this.
That's why I have shown restraint and limited Combobulate to things
that I feel are simple (but made it quite easy for someone to
customize, if they disagree!)

As a user, I may well want to put my code into an erroneous state,
temporarily, because I am doing something that cannot be represented
atomically as a single command. Therefore, `self-insert-command' (for
example) does not predict what I am about to type and intercedes when
it disagrees with me: it merely abides.

When I do this with `C-M-t' it is because it is an intentional act on
my behalf. The example I gave above is illustrative; it's designed to
highlight the problem.

>>
>>
>>
>>>> You could make a cogent argument that both approaches are wrong from a
>>>> syntactic perspective, but I think that misses the broader point that
>>>> `C-M-t' now does something errant and unexpected.
>>>
>>> I don't really see how "foo def():" is any better at all.  We gain some
>>> great improvements with this "naive" method - namely:
>>>
>>> if 5 + 5 == 10 then 10 else 100 + 100.  If point is on the else the 100
>>> + 100 wil be swapped by 10, but the old behavior will be broken.
>>>
>>
>> The old behaviour was consistent. It had a simple *modus operandi*:
>> swap two things around point. As someone who has used `C-M-t' for
>> decades, I know what it'll do in pretty much all situations, because I
>> know what `C-M-k` and `C-M-f/b` do at all times.
>
> It may be consistent, but imo it is too close to transpose-words, and
> too likely to create useless code in non-lisp languages.
>

No, transpose word and transpose sexp are very different; do different
things; and apply in vastly different circumstances:

Let -!- be point:

    d = {'Hello, World!': -!- 1}

    # C-M-t
    d = {1: 'Hello, World!'}

    # M-t
    d = {'Hello, 1!': World}

`transpose-sexps' works just fine the way it is: enriching it with a
greater understanding of certain contexts is a fruitful endeavour, if
it is done sympathetically.

>>
>> Neither approach is great if you holistically approach this task as
>> "making it correct at all times", and it is easy to confect scenarios
>> that result in something that is semantically wrong, but syntactically
>> correct; something that is plain wrong, both semantically and
>> syntactically; and something that is occasionally correct.
>>
>
> I see what you mean, but to me semantically _and_ syntactically correct
> is the benefit we should pursue when we actually have the parse tree.
> The current implementation will semantically correct in many interesting
> cases, such as the one I outlined, and is a huge improvement to the
> current "transpose-words"-like behavior.
>

There is no such thing as "syntactically correct" if you allow a user
unfettered access to type in a buffer. Merely typing in the wrong
place will break that promise. And who are we to judge what someone
writes and where?

The resting state of all code is "almost always broken" as you're
typing out your code.

>> 'Like' siblings are an easy way out of this mess with the caveat, as
>> you'll see, but now you need to carefully pluck the right nodes from
>> the tree!
>>
>> Consider the node type `pair' in a dict in Python. They are easily transposable for
>> that very reason, notwithstanding the anonymous "," node betwixt them.
>>
>> That is why Combobulate has a list of stuff that it can safely
>> transpose, and for everything else it defaults to the "classic"
>> transpose.
>>
>
> Yeah, such an approach seems reasonable, and there is already precedence
> in defining such "things" in Emacs.  As for the default fallback, I'll
> see what I can do in the "treesit-transpose-sexps" function.  The
> machinery in transpose-subr and friends is a little finicky, so to
> adhere to that mechanism isn't the easiest thing.
>

Sure. But please be careful when you make changes to `transpose-subr'
(or `transpose-subr-1') so that they don't break its existing contract
with its current users. It is a *very* powerful set of commands that
can swap arbitrary tracts of text.

>>>>
>>>> Worse, it's not possible to revert to the old behaviour (see
>>>> bug#60654)
>>>>
>>>>
>>
>> Thanks for fixing that!
>>
>
> No problem - hopefully it is installed pretty soon.
>
> Theo





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60655; Package emacs. (Mon, 09 Jan 2023 13:38:01 GMT) Full text and rfc822 format available.

Message #20 received at 60655 <at> debbugs.gnu.org (full text, mbox):

From: Theodor Thornhill <theo <at> thornhill.no>
To: Mickey Petersen <mickey <at> masteringemacs.org>
Cc: 60655 <at> debbugs.gnu.org
Subject: Re: bug#60655: 30.0.50; tree-sitter: `treesit-transpose-sexps' is
 broken.
Date: Mon, 09 Jan 2023 14:37:11 +0100
Mickey Petersen <mickey <at> masteringemacs.org> writes:

> Theodor Thornhill <theo <at> thornhill.no> writes:
>
>> Mickey Petersen <mickey <at> masteringemacs.org> writes:
>>
>>> Theodor Thornhill <theo <at> thornhill.no> writes:
>>>
>>>> Mickey Petersen <mickey <at> masteringemacs.org> > The tree-sitter-enabled
>>>> function, `treesit-transpose-sexps', that is called by
>>>> transpose-sexps, is broken.
>>>>>
>>>>> It uses a naive method of sibling adjacency to determine
>>>>> transpositions. But it is unfortunately not correct.
>>>>>
>>>>> Python:
>>>>>
>>>>>
>>>>>   def -!-foo():
>>>>>       pass
>>>>>

[...]

>
> That, in my opinion, is the wrong way to look at it.
>
> `C-M-t' already works well: it transposes stuff around point. Nothing
> more, nothing less.
>
> If I write rubbish code as a human, no amount of machine intelligence
> will (yet) undo that. Nor should a 'clever' mechanism that is only
> clever by half. Trying to transpose things on, near or around
> point is a useful addition if, and only if, it can do so in a manner
> that is sensible, and predictable, to its operator.
>
> You will very quickly run into umpteen problems generalizing this.
> That's why I have shown restraint and limited Combobulate to things
> that I feel are simple (but made it quite easy for someone to
> customize, if they disagree!)

Yes, I agree. But the commands M-t and C-M-t are different for a reason,
so they should be used in different circumstances, right?

>
> As a user, I may well want to put my code into an erroneous state,
> temporarily, because I am doing something that cannot be represented
> atomically as a single command. Therefore, `self-insert-command' (for
> example) does not predict what I am about to type and intercedes when
> it disagrees with me: it merely abides.
>
> When I do this with `C-M-t' it is because it is an intentional act on
> my behalf. The example I gave above is illustrative; it's designed to
> highlight the problem.
>

Sure, I believe I understand what you mean.

>>>
>>>
>>>
>>>>> You could make a cogent argument that both approaches are wrong from a
>>>>> syntactic perspective, but I think that misses the broader point that
>>>>> `C-M-t' now does something errant and unexpected.
>>>>
>>>> I don't really see how "foo def():" is any better at all.  We gain some
>>>> great improvements with this "naive" method - namely:
>>>>
>>>> if 5 + 5 == 10 then 10 else 100 + 100.  If point is on the else the 100
>>>> + 100 wil be swapped by 10, but the old behavior will be broken.
>>>>
>>>
>>> The old behaviour was consistent. It had a simple *modus operandi*:
>>> swap two things around point. As someone who has used `C-M-t' for
>>> decades, I know what it'll do in pretty much all situations, because I
>>> know what `C-M-k` and `C-M-f/b` do at all times.

Yeah, but "thing" is very vague, so the consistency is an accident more
than anything else.

>>
>> It may be consistent, but imo it is too close to transpose-words, and
>> too likely to create useless code in non-lisp languages.
>>
>
> No, transpose word and transpose sexp are very different; do different
> things; and apply in vastly different circumstances:
>
> Let -!- be point:
>
>     d = {'Hello, World!': -!- 1}
>
>     # C-M-t
>     d = {1: 'Hello, World!'}
>
>     # M-t
>     d = {'Hello, 1!': World}
>
> `transpose-sexps' works just fine the way it is: enriching it with a
> greater understanding of certain contexts is a fruitful endeavour, if
> it is done sympathetically.
>

You say they are used in vastly different circumstances, and I argued
that the "def foo" case is a case where it should't be used.  I still
believe there are oceans of improvements to be made, but I don't
understand the desire not to improve what is there, whatever an
improvement is considered to be.

>>>
>>> Neither approach is great if you holistically approach this task as
>>> "making it correct at all times", and it is easy to confect scenarios
>>> that result in something that is semantically wrong, but syntactically
>>> correct; something that is plain wrong, both semantically and
>>> syntactically; and something that is occasionally correct.
>>>
>>
>> I see what you mean, but to me semantically _and_ syntactically correct
>> is the benefit we should pursue when we actually have the parse tree.
>> The current implementation will semantically correct in many interesting
>> cases, such as the one I outlined, and is a huge improvement to the
>> current "transpose-words"-like behavior.
>>
>

I see two directions myself.  Either we try to make a point that
transpose-words and transpose-sexps operate on different contexts, or we
try our best to blend the two, to avoid backwards incompatible changes.
I'm ok with both, myself.  We could even try to make a transpose-dwim
command that will guess the surroundings and choose modus-operandi
accordingly?


>
> Sure. But please be careful when you make changes to `transpose-subr'
> (or `transpose-subr-1') so that they don't break its existing contract
> with its current users. It is a *very* powerful set of commands that
> can swap arbitrary tracts of text.
>

Of course - I'm trying hard to be conservative :)

Theo




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60655; Package emacs. (Mon, 30 Dec 2024 18:26:02 GMT) Full text and rfc822 format available.

Message #23 received at 60655 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Mickey Petersen <mickey <at> masteringemacs.org>
Cc: 60655 <at> debbugs.gnu.org
Subject: Re: bug#60655: 30.0.50; tree-sitter: `treesit-transpose-sexps' is
 broken.
Date: Mon, 30 Dec 2024 20:22:02 +0200
[Message part 1 (text/plain, inline)]
> 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.

[treesit-transpose-sexps.patch (text/x-diff, inline)]
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
 ;;

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60655; Package emacs. (Sat, 04 Jan 2025 18:33:02 GMT) Full text and rfc822 format available.

Message #26 received at 60655 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Mickey Petersen <mickey <at> masteringemacs.org>
Cc: 60655 <at> debbugs.gnu.org
Subject: Re: bug#60655: 30.0.50; tree-sitter: `treesit-transpose-sexps' is
 broken.
Date: Sat, 04 Jan 2025 20:30:28 +0200
close 60655 31.0.50
thanks

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

Ok, this is pushed now to master.




bug marked as fixed in version 31.0.50, send any further explanations to 60655 <at> debbugs.gnu.org and Mickey Petersen <mickey <at> masteringemacs.org> Request was from Juri Linkov <juri <at> linkov.net> to control <at> debbugs.gnu.org. (Sat, 04 Jan 2025 18:33:03 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 02 Feb 2025 12:24:14 GMT) Full text and rfc822 format available.

This bug report was last modified 14 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.