From: Theodor Thornhill <theo@thornhill.no>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: Yuan Fu <casouri@gmail.com>, emacs-devel@gnu.org, eliz@gnu.org
Subject: Re: Plug treesit.el into other emacs constructs
Date: Thu, 15 Dec 2022 20:37:01 +0100 [thread overview]
Message-ID: <871qp01msi.fsf@thornhill.no> (raw)
In-Reply-To: <jwvv8mdu1ac.fsf-monnier+emacs@gnu.org>
Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> Great. It seems there has been almost no development, nor documentation
>> done in this area for a long time. Should I try to improve on this part
>> of the code while I'm at it?
>
> That would be welcome, yes.
>
Ok, will do.
>> Yeah, that's what I'm thinking too. I'm just thinking we should be
>> clear on what a word/sexp/sentence/paragraph/defun etc is in non-lisp
>> and non-human languages.
>
> In the context of SMIE I came up with a usable meaning for sexp (which
> I've been trying to explain in this thread), but for sentence and
> paragraph it seems harder and is likely to depend on the specifics of
> the language (e.g. for some languages "sentence" might be mapped to
> "instruction" or maybe "instruction that doesn't itself contain nested
> instructions", but some languages don't have a notion of instruction).
>
Yeah, I understand.
>>>> Now I'm having issues where movement over sexps ends up not in the
>>>> same place.
>>> Same place as?
>> IIRC there's no guarantee that the movement sequence used for
>> transpose-sexp moves over the same blocks of code, so in non-lisp
>> languages there's no real semantic to go from.
>
> I guess in general it can be difficult to be consistent, indeed.
> In SMIE I preserve the following (or at least I try to):
>
> <FOO> infix <BAR>
> ^ ^ ^ ^
> AB AE BB BE
>
> if `transpose-sexp` swaps FOO and BAR, then `forward-sexp` from AE goes
> to BE and `backward-sexp` from BB goes to AB. But when you start to
> consider mixfix syntax it can become much less clear what needs to
> be done. I don't think we should worry too much if `transpose-sexp` and
> `forward/backward-sexp` don't align 100% is all cases: we should strive
> to keep them consistent, but it's OK to break down in corner cases.
>
Actually, I think there is a nice solution waiting for us. If we
consider the 'balanced expressions' to be swapped in transpose-sexps as
siblings, we could just:
(defun treesit-transpose-sexps (&optional arg)
"Tree-sitter `beginning-of-defun' function.
ARG is the same as in `beginning-of-defun'."
(interactive "*p")
(if-let* ((node (treesit-node-at (point)))
(prev (treesit-node-prev-sibling node))
(next (treesit-node-next-sibling node)))
(list (cons (treesit-node-start prev)
(treesit-node-end prev))
(cons (treesit-node-start next)
(treesit-node-end next)))
;; Hack to trigger the error message in transpose-subr-1 when we
;; don't have siblings to swap.
(list (cons 0 1) (cons 0 1))))
If this code is plugged into transpose-sexps we get this nice behavior:
Python:
```
def foo():
return x |if x == 5 else False
def foo():
return x == 5 if x| else False
```
and
```
def foo():
return x if x == 5 el|se False
def foo():
return x if False else x == 5|
```
and
```
def foo():
return| x if x == 5 else False ;; Don't have two things to transpose
```
Java:
```
public class foo {
void foo(String x,| int y) {}
}
public class foo {
void foo(int y, String x|) {}
```
and
```
public class foo {
void foo() {
if (x =|= y) {}
}
}
public class foo {
void foo() {
if (y == x|) {}
}
}
```
and
```
public class foo {
void foo(String x, int y) {
foo(a + 4|, c * b + y, b, d).bar();
}
}
public class foo {
void foo(String x, int y) {
foo(c * b + y, a + 4|, b, d).bar();
}
}
```
and
```
foo(a + 4, c * b |+ y, b, d);
foo(a + 4, y + c * b, b, d);
```
Now forward/backward-sexp can actually work a little differently, as you
suggest, or we can let it use the same "move over siblings"-semantic.
In that case we don't even need the treesit-sexp-type-regexp variables to
control this, I think.
What do you think?
Theo
next prev parent reply other threads:[~2022-12-15 19:37 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-12 14:33 Plug treesit.el into other emacs constructs Theodor Thornhill
2022-12-12 14:45 ` Eli Zaretskii
2022-12-13 18:17 ` Theodor Thornhill
2022-12-12 15:46 ` Stefan Monnier
2022-12-13 18:27 ` Theodor Thornhill
2022-12-13 19:37 ` Stefan Monnier
2022-12-13 19:53 ` Yuan Fu
2022-12-13 20:06 ` Perry Smith
2022-12-13 23:19 ` Stefan Monnier
2022-12-14 8:14 ` Yuan Fu
2022-12-14 8:42 ` Theodor Thornhill
2022-12-14 14:01 ` Stefan Monnier
2022-12-14 16:24 ` Theodor Thornhill
2022-12-14 17:46 ` Stefan Monnier
2022-12-14 18:07 ` Theodor Thornhill
2022-12-14 19:25 ` Stefan Monnier
2022-12-14 19:35 ` Stefan Monnier
2022-12-14 20:04 ` Theodor Thornhill
2022-12-14 20:50 ` Stefan Monnier
2022-12-14 21:15 ` Theodor Thornhill
2022-12-14 21:34 ` Stefan Monnier
2022-12-15 19:37 ` Theodor Thornhill [this message]
2022-12-15 19:56 ` Stefan Monnier
2022-12-15 20:03 ` Theodor Thornhill
2022-12-15 20:33 ` Theodor Thornhill
2022-12-15 20:57 ` Theodor Thornhill
2022-12-24 7:00 ` Eli Zaretskii
2022-12-24 8:44 ` Yuan Fu
2022-12-24 14:01 ` Stefan Monnier
2022-12-24 14:15 ` Theodor Thornhill
2022-12-26 19:11 ` Theodor Thornhill
2022-12-26 22:46 ` Stefan Monnier
2022-12-26 22:51 ` Stefan Monnier
2022-12-27 22:15 ` Theodor Thornhill via Emacs development discussions.
2022-12-28 0:12 ` Stefan Monnier
2022-12-28 9:26 ` Theodor Thornhill via Emacs development discussions.
2022-12-28 18:01 ` Stefan Monnier
2022-12-28 18:27 ` Theodor Thornhill
2022-12-26 22:56 ` Theodor Thornhill
2022-12-27 15:46 ` Lynn Winebarger
2022-12-14 23:31 ` Yuan Fu
2022-12-15 0:05 ` Yuan Fu
2022-12-15 7:09 ` Eli Zaretskii
2022-12-15 7:14 ` Theodor Thornhill
2022-12-15 4:37 ` Stefan Monnier
2022-12-15 5:59 ` Theodor Thornhill
2022-12-15 21:23 ` Yuan Fu
2022-12-15 21:28 ` Theodor Thornhill
2022-12-13 20:02 ` Theodor Thornhill
2022-12-13 23:10 ` Stefan Monnier
2022-12-14 23:32 ` Stephen Leake
2022-12-16 10:02 ` Kévin Le Gouguec
2022-12-16 11:54 ` [SPAM UNSURE] " Stephen Leake
2022-12-17 15:30 ` Kévin Le Gouguec
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=871qp01msi.fsf@thornhill.no \
--to=theo@thornhill.no \
--cc=casouri@gmail.com \
--cc=eliz@gnu.org \
--cc=emacs-devel@gnu.org \
--cc=monnier@iro.umontreal.ca \
/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).