From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill Newsgroups: gmane.emacs.devel Subject: Re: Plug treesit.el into other emacs constructs Date: Mon, 26 Dec 2022 20:11:45 +0100 Message-ID: <87o7rq7zf2.fsf@thornhill.no> References: <87wn6whete.fsf@thornhill.no> <04BB786A-3ED1-4918-8583-17AA01A1E453@gmail.com> <4E3940CA-67A6-45B7-8785-4E60FDECCDFB@gmail.com> <4315EFC6-7AA8-4A48-845C-9CA8B88034D9@thornhill.no> <87bko521n0.fsf@thornhill.no> <87359h1ybt.fsf@thornhill.no> <871qp01msi.fsf@thornhill.no> <87v8mczb6b.fsf@thornhill.no> <87sfhgz9s8.fsf@thornhill.no> <87pmckz8p0.fsf@thornhill.no> <5DF07C4E-2CCD-4561-AFFB-D5D81D67BFE0@thornhill.no> 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="13574"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Yuan Fu , emacs-devel@gnu.org, eliz@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Dec 26 20:12:42 2022 Return-path: Envelope-to: ged-emacs-devel@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 1p9str-0003J6-74 for ged-emacs-devel@m.gmane-mx.org; Mon, 26 Dec 2022 20:12:39 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p9stC-0006Al-TD; Mon, 26 Dec 2022 14:11:58 -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 1p9stB-0006Aa-1I for emacs-devel@gnu.org; Mon, 26 Dec 2022 14:11:57 -0500 Original-Received: from out2.migadu.com ([188.165.223.204]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p9st5-0003tD-MH; Mon, 26 Dec 2022 14:11:55 -0500 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thornhill.no; s=key1; t=1672081907; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=y6WEYzv6GHx6eQ35Th1XFOw762Q4IzwXpxBuZPOt47Q=; b=dUYMcrDddYSwCcnd5S1SR4WyehaEq+Nhc4Zsx0dNd5MYdXsOyuygtV5Dxkgu9sDUk9lrm6 rZMmeE8kW+/qNdwK5BoG/50yh4K0Jr5TPuQfy8B+gjhFM0Oa02RMWOtZd4rLTb+DqfS7Ge xaHOkcJ6wypQT9CmU7eOH7VlNKOvM1INJxzXge9nftLsYSGeUr54hHaOcnMxZW/iUj9Ktv qlFnxZgj8s2K0ovyZe1Ybvhf1OE+mur/pEKg8Wv7SLgXk13OAQ4LMPI475LqKW73oCDfPR NakqR4XOa140JHUEMZ6rS4Pyb1hJsQsA/OuCkbC431EMcQ7tErf6N5kkxUKQ5w== In-Reply-To: <5DF07C4E-2CCD-4561-AFFB-D5D81D67BFE0@thornhill.no> X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=188.165.223.204; envelope-from=theo@thornhill.no; helo=out2.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:301940 Archived-At: --=-=-= Content-Type: text/plain uld we use a `transpose-sexp-function` variable, which `treesit` can >>then set, so `simple.el` doesn't need to know about `treesit` at all? >> > > Yes absolutely! I'll make that change. It makes sense, because we need not really rely on forward-foo anyways:) > >>> (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 >> > > Yeah! I believe this either wasn't the latest patch, or i forgot to > send it. I'll see what lies around my system and wrap things up. What do you think about something like this? It feels a little iffy how to handle the separate return values, but it works. I'd be super happy for some feedback on how to best solve that, though :) Also, I made the treesit-transpose-sexps a little better imo, in that we only find named nodes to swap, but use every available node for the entry. We rarely, if ever want to swap the unnamed nodes. Theo --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-treesit-transpose-sexps-bug-60128.patch >From 0dc412eaf16123dcb65381970fb82c0741809753 Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Sun, 25 Dec 2022 20:11:59 +0100 Subject: [PATCH] Add treesit-transpose-sexps (bug#60128) We don't really need to rely on forward-sexp to define what to transpose. In tree-sitter we can consider siblings as "balanced expressions", and swap them without doing any movement to calculate where the siblings in question are. * lisp/simple.el (transpose-sexps-function): New defvar-local. (transpose-sexps): Use the new defvar-local if available. (transpose-subr): Check whether the mover function returns a cons of conses, then run transpose-subr-1 on the position-pairs. * lisp/treesit.el (treesit-transpose-sexps): New function. --- lisp/simple.el | 97 ++++++++++++++++++++++++++++--------------------- lisp/treesit.el | 24 +++++++++++- 2 files changed, 78 insertions(+), 43 deletions(-) diff --git a/lisp/simple.el b/lisp/simple.el index 4551b749d5..591b659c62 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -8438,6 +8438,14 @@ transpose-words (interactive "*p") (transpose-subr 'forward-word arg)) +(defvar-local transpose-sexps-function nil + "If non-nil, `transpose-sexps' delegates to this function. + +The return value of this function is expected to be a cons of two +conses, denoting the positions in the current buffer to be +transposed. If no such pair of positions is available, signal +USER-ERROR.") + (defun transpose-sexps (arg &optional interactive) "Like \\[transpose-chars] (`transpose-chars'), but applies to sexps. Unlike `transpose-words', point must be between the two sexps and not @@ -8454,36 +8462,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 transpose-sexps-function transpose-sexps-function + (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))) (defun transpose-lines (arg) @@ -8509,19 +8518,23 @@ transpose-lines ;; FIXME document SPECIAL. (defun transpose-subr (mover arg &optional special) "Subroutine to do the work of transposing objects. -Works for lines, sentences, paragraphs, etc. MOVER is a function that -moves forward by units of the given object (e.g. `forward-sentence', -`forward-paragraph'). If ARG is zero, exchanges the current object -with the one containing mark. If ARG is an integer, moves the -current object past ARG following (if ARG is positive) or -preceding (if ARG is negative) objects, leaving point after the -current object." - (let ((aux (if special mover - (lambda (x) - (cons (progn (funcall mover x) (point)) - (progn (funcall mover (- x)) (point)))))) - pos1 pos2) +Works for lines, sentences, paragraphs, etc. MOVER is either a +function that moves forward by units of the given +object (e.g. `forward-sentence', `forward-paragraph'), or a +function that calculates a cons of two position-pairs. If ARG is +zero, exchanges the current object with the one containing mark. +If ARG is an integer, moves the current object past ARG +following (if ARG is positive) or preceding (if ARG is negative) +objects, leaving point after the current object." + (let* ((aux (if special mover + (lambda (x) + (cons (progn (funcall mover x) (point)) + (progn (funcall mover (- x)) (point)))))) + (pos1 (save-excursion (funcall aux arg))) + pos2) (cond + ((and (consp (car pos1)) (consp (cdr pos1))) + (transpose-subr-1 (car pos1) (cdr pos1))) ((= arg 0) (save-excursion (setq pos1 (funcall aux 1)) diff --git a/lisp/treesit.el b/lisp/treesit.el index cefbed1a16..9f0965ac68 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -1582,6 +1582,27 @@ treesit-search-forward-goto (goto-char current-pos))) node)) +(defun treesit-transpose-sexps (&optional arg) + "Tree-sitter `transpose-sexps' function. +Arg is the same as in `transpose-sexps'. + +Return a pair of positions describing the regions to transpose +for use in `transpose-subr' and friends." + (let* ((parent (treesit-node-parent (treesit-node-at (point)))) + (child (treesit-node-child parent 0 t))) + (named-let loop ((prev child) + (next (treesit-node-child + parent (+ arg (treesit-node-index child t)) + t))) + (if (< (point) (or (treesit-node-end next) + (user-error "Don't have two things to transpose"))) + (cons (cons (treesit-node-start prev) + (treesit-node-end prev)) + (cons (treesit-node-start next) + (treesit-node-end next))) + (loop (treesit-node-next-sibling prev t) + (treesit-node-next-sibling next t)))))) + ;;; Navigation, defun, things ;; ;; Emacs lets you define "things" by a regexp that matches the type of @@ -2111,7 +2132,8 @@ treesit-major-mode-setup ;; Defun name. (when treesit-defun-name-function (setq-local add-log-current-defun-function - #'treesit-add-log-current-defun))) + #'treesit-add-log-current-defun)) + (setq-local transpose-sexps-function #'treesit-transpose-sexps)) ;;; Debugging -- 2.34.1 --=-=-=--