emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Plug treesit.el into other emacs constructs


From: Stefan Monnier
Subject: Re: Plug treesit.el into other emacs constructs
Date: Sat, 24 Dec 2022 09:01:36 -0500
User-agent: Gnus/5.13 (Gnus v5.13)

> -(eval-when-compile (require 'cl-lib))
> +(eval-when-compile (require 'cl-lib)
> +                   (require 'treesit))

I don't see any part of `treesit` used during compilation.
Is it a left over, maybe?

> @@ -8453,36 +8455,37 @@ transpose-sexps
>            (transpose-sexps arg nil)
>          (scan-error (user-error "Not between two complete sexps")))
>      (transpose-subr
> -     (lambda (arg)
> -       ;; Here we should try to simulate the behavior of
> -       ;; (cons (progn (forward-sexp x) (point))
> -       ;;       (progn (forward-sexp (- x)) (point)))
> -       ;; Except that we don't want to rely on the second forward-sexp
> -       ;; putting us back to where we want to be, since forward-sexp-function
> -       ;; might do funny things like infix-precedence.
> -       (if (if (> arg 0)
> -            (looking-at "\\sw\\|\\s_")
> -          (and (not (bobp))
> -               (save-excursion
> -                    (forward-char -1)
> -                    (looking-at "\\sw\\|\\s_"))))
> -        ;; Jumping over a symbol.  We might be inside it, mind you.
> -        (progn (funcall (if (> arg 0)
> -                            'skip-syntax-backward 'skip-syntax-forward)
> -                        "w_")
> -               (cons (save-excursion (forward-sexp arg) (point)) (point)))
> -         ;; Otherwise, we're between sexps.  Take a step back before jumping
> -         ;; to make sure we'll obey the same precedence no matter which
> -         ;; direction we're going.
> -         (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward)
> -                  " .")
> -         (cons (save-excursion (forward-sexp arg) (point))
> -            (progn (while (or (forward-comment (if (> arg 0) 1 -1))
> -                              (not (zerop (funcall (if (> arg 0)
> -                                                       'skip-syntax-forward
> -                                                     'skip-syntax-backward)
> -                                                   ".")))))
> -                   (point)))))
> +     (if (treesit-parser-list) #'treesit-transpose-sexps
> +       (lambda (arg)
> +         ;; Here we should try to simulate the behavior of
> +         ;; (cons (progn (forward-sexp x) (point))
> +         ;;       (progn (forward-sexp (- x)) (point)))
> +         ;; Except that we don't want to rely on the second forward-sexp
> +         ;; putting us back to where we want to be, since 
> forward-sexp-function
> +         ;; might do funny things like infix-precedence.
> +         (if (if (> arg 0)
> +              (looking-at "\\sw\\|\\s_")
> +            (and (not (bobp))
> +                 (save-excursion
> +                      (forward-char -1)
> +                      (looking-at "\\sw\\|\\s_"))))
> +             ;; Jumping over a symbol.  We might be inside it, mind you.
> +          (progn (funcall (if (> arg 0)
> +                              'skip-syntax-backward 'skip-syntax-forward)
> +                          "w_")
> +                 (cons (save-excursion (forward-sexp arg) (point)) (point)))
> +           ;; Otherwise, we're between sexps.  Take a step back before 
> jumping
> +           ;; to make sure we'll obey the same precedence no matter which
> +           ;; direction we're going.
> +           (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward)
> +                    " .")
> +           (cons (save-excursion (forward-sexp arg) (point))
> +              (progn (while (or (forward-comment (if (> arg 0) 1 -1))
> +                                (not (zerop (funcall (if (> arg 0)
> +                                                         'skip-syntax-forward
> +                                                       'skip-syntax-backward)
> +                                                     ".")))))
> +                     (point))))))
>       arg 'special)))

Could we use a `transpose-sexp-function` variable, which `treesit` can
then set, so `simple.el` doesn't need to know about `treesit` at all?

>  (defun transpose-lines (arg)
> @@ -8521,6 +8524,9 @@ transpose-subr
>                      (progn (funcall mover (- x)) (point))))))
>       pos1 pos2)
>      (cond
> +     ((treesit-parser-list)
> +      (cl-multiple-value-bind (p1 p2) (funcall aux arg)
> +        (transpose-subr-1 p1 p2)))
>       ((= arg 0)
>        (save-excursion
>       (setq pos1 (funcall aux 1))

Please use `pcase-let` instead of `cl-multiple-value-bind` (especially
since you use it to decompose something built with `list` rather than
with `cl-values`).

Also to avid re-testing `treesit-parser-list`, I'd recommend you extend
the semantics of `mover` so it can either return a position (the old
protocol) or directly return a pair of positions.  You could even add
a 3rd kind of return value to explicitly trigger the error message
instead of relying on the (cons 0 1) hack.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]