From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#60894: 30.0.50; [PATCH] Add treesit-forward-sexp Date: Tue, 17 Jan 2023 22:07:45 +0100 Message-ID: <87358827ku.fsf@thornhill.no> References: <875yd428ng.fsf@thornhill.no> <4e2dacb8-31b3-58e2-4987-2d22501c3324@yandex.ru> Reply-To: Theodor Thornhill 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="552"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Mickey Petersen , Stefan Monnier , Juri Linkov To: Dmitry Gutov , 60894@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Jan 17 22:08:14 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1pHtBl-000AP8-J2 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 17 Jan 2023 22:08:14 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pHtBc-0007bx-EL; Tue, 17 Jan 2023 16:08:04 -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 1pHtBb-0007bp-9v for bug-gnu-emacs@gnu.org; Tue, 17 Jan 2023 16:08:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pHtBb-0006ar-21 for bug-gnu-emacs@gnu.org; Tue, 17 Jan 2023 16:08:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pHtBa-0000NN-HP for bug-gnu-emacs@gnu.org; Tue, 17 Jan 2023 16:08:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 17 Jan 2023 21:08:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60894 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 60894-submit@debbugs.gnu.org id=B60894.16739896771434 (code B ref 60894); Tue, 17 Jan 2023 21:08:02 +0000 Original-Received: (at 60894) by debbugs.gnu.org; 17 Jan 2023 21:07:57 +0000 Original-Received: from localhost ([127.0.0.1]:38407 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pHtBU-0000N3-EK for submit@debbugs.gnu.org; Tue, 17 Jan 2023 16:07:56 -0500 Original-Received: from out-182.mta0.migadu.com ([91.218.175.182]:18224) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pHtBP-0000Mq-Tw for 60894@debbugs.gnu.org; Tue, 17 Jan 2023 16:07:54 -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=1673989667; 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=lmPj2/vOfcpWWr10050tpQPMsDjC6NeK+pxrF8quILM=; b=oxMVeWaR6i0hLBAQqfIl0cMdlBb4DcLnH924WP9Ip3e5V4wbGd+YZbLRCtWSniQy5SvZ0W KU6TreR2RDkmQYpqU3ClMv0fTMB2HaWR7TfsnK1eRrsxwSQoQSV+PsV/HMek44oOk4DLJq lgMhHXJH1CkgKNdUNWpo4jLxZau3xIqpojhKm66LVY6OqnGwpKu6TRg7/IwPynj1rhPboe KjYqt/Wre0SUVQZQyl6SZhxsISu9GXfCeR+XaFOL6Kzi/L/ooeBweAhgup+FuZt7rYlqIj PXegWooOjrvdRd6zd7l5gRrfvHqlA+X098stf9ArPBu44HdBW9+spU9aLSAMkg== In-Reply-To: <4e2dacb8-31b3-58e2-4987-2d22501c3324@yandex.ru> X-Migadu-Flow: FLOW_OUT X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:253584 Archived-At: --=-=-= Content-Type: text/plain Dmitry Gutov writes: > On 17/01/2023 22:44, Theodor Thornhill via Bug reports for GNU Emacs, > the Swiss army knife of text editors wrote: >> 1. What should a sexp be? >> >> Is it basically "everything", or is there a distincition between >> "word", "sexp" and "sentence"? For lisp forward-sexp looks like a >> "jump over words, or a balanced pair of parens". In other languages >> that can look a little weird - consider: >> >> ``` >> foo().|bar().baz(); -> foo().bar|().baz(); -> foo().bar()|.baz(); >> ``` >> >> In a sense it could be considered "better", or at least distinct from >> forward-word to: >> >> ``` >> foo().|bar().baz(); -> foo().bar()|.baz(); -> foo().bar().baz()|; > > One of the key things for Ruby, I think, is to jump over expressions. > > E.g. when the point is before 'def' in > > def foo > ... > end > > forward-sexp jumps to after 'end'. And backward-sexp jumps back. > > Same for > > if 2 == 3 > ... > end > > , parenthesized expressions and (less important) method calls and > statements as well. Test this very untested addition to the patch (I know no ruby). It seems to do what you want. I'd consider this sentence movement, though. For M-e Theo --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-treesit-forward-sexp.patch >From 9934c4225633ff0524269902b8c574666cdfe0cb Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Tue, 17 Jan 2023 21:18:29 +0100 Subject: [PATCH] Add treesit-forward-sexp * lisp/progmodes/java-ts-mode.el (java-ts-mode): Use treesit-sexp-type-regexp. * lisp/treesit.el (treesit-sexp-type-regexp): New defvar. (treesit-forward-sexp): New command. (treesit-major-mode-setup): Conditionally set forward-sexp-function. * lisp/progmodes/ruby-ts-mode.el: Add some types to ruby-ts-mode. --- lisp/progmodes/java-ts-mode.el | 15 +++++++++++++++ lisp/progmodes/ruby-ts-mode.el | 4 ++++ lisp/treesit.el | 17 +++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el index 83c437d307..03093e0980 100644 --- a/lisp/progmodes/java-ts-mode.el +++ b/lisp/progmodes/java-ts-mode.el @@ -328,6 +328,21 @@ java-ts-mode "package_declaration" "import_declaration"))) + (setq-local treesit-sexp-type-regexp + (regexp-opt '("annotation" + "parenthesized_expression" + "argument_list" + "identifier" + "modifiers" + "block" + "body" + "literal" + "access" + "reference" + "_type" + "true" + "false"))) + ;; Font-lock. (setq-local treesit-font-lock-settings java-ts-mode--font-lock-settings) (setq-local treesit-font-lock-feature-list diff --git a/lisp/progmodes/ruby-ts-mode.el b/lisp/progmodes/ruby-ts-mode.el index 939c054b04..78186c78d5 100644 --- a/lisp/progmodes/ruby-ts-mode.el +++ b/lisp/progmodes/ruby-ts-mode.el @@ -984,6 +984,10 @@ ruby-ts-mode ;; Navigation. (setq-local treesit-defun-type-regexp ruby-ts--method-regex) + (setq-local treesit-sexp-type-regexp + (regexp-opt '("method" + "if"))) + ;; AFAIK, Ruby can not nest methods (setq-local treesit-defun-prefer-top-level nil) diff --git a/lisp/treesit.el b/lisp/treesit.el index 69bfff21df..735df7a8f4 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -1622,6 +1622,21 @@ treesit-search-forward-goto (goto-char current-pos))) node)) +(defvar-local treesit-sexp-type-regexp nil + "A regexp that matches the node type of sexp nodes. + +A sexp node is a node that is bigger than punctuation, and +delimits medium sized statements in the source code. It is, +however, smaller in scope than sentences. This is used by +`treesit-forward-sexp' and friends.") + +(defun treesit-forward-sexp (&optional arg) + (interactive "^p") + (or arg (setq arg 1)) + (funcall + (if (> arg 0) #'treesit-end-of-thing #'treesit-beginning-of-thing) + treesit-sexp-type-regexp (abs arg))) + (defun treesit-transpose-sexps (&optional arg) "Tree-sitter `transpose-sexps' function. Arg is the same as in `transpose-sexps'. @@ -2287,6 +2302,8 @@ treesit-major-mode-setup (setq-local add-log-current-defun-function #'treesit-add-log-current-defun)) + (when treesit-sexp-type-regexp + (setq-local forward-sexp-function #'treesit-forward-sexp)) (setq-local transpose-sexps-function #'treesit-transpose-sexps) (when treesit-sentence-type-regexp (setq-local forward-sentence-function #'treesit-forward-sentence)) -- 2.34.1 --=-=-=--