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 21:44:35 +0100 Message-ID: <875yd428ng.fsf@thornhill.no> 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="2168"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Juri Linkov , Mickey Petersen , Stefan Monnier To: 60894@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Jan 17 21:45:26 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 1pHsph-0000Hg-LL for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 17 Jan 2023 21:45:26 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pHspS-0000bw-1V; Tue, 17 Jan 2023 15:45:10 -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 1pHspP-0000bE-5k for bug-gnu-emacs@gnu.org; Tue, 17 Jan 2023 15:45:08 -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 1pHspN-0002hW-HH for bug-gnu-emacs@gnu.org; Tue, 17 Jan 2023 15:45:06 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pHspK-0008DM-2x; Tue, 17 Jan 2023 15:45:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: juri@linkov.net, mickey@masteringemacs.org, monnier@iro.umontreal.ca, bug-gnu-emacs@gnu.org Resent-Date: Tue, 17 Jan 2023 20:45:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 60894 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: Juri Linkov , Mickey Petersen , Stefan Monnier Original-Received: via spool by submit@debbugs.gnu.org id=B.167398829131531 (code B ref -1); Tue, 17 Jan 2023 20:45:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 17 Jan 2023 20:44:51 +0000 Original-Received: from localhost ([127.0.0.1]:38387 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pHsp8-0008CS-Bz for submit@debbugs.gnu.org; Tue, 17 Jan 2023 15:44:50 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:46632) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pHsp2-0008CG-CE for submit@debbugs.gnu.org; Tue, 17 Jan 2023 15:44:48 -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 1pHsp1-0000Y3-PI for bug-gnu-emacs@gnu.org; Tue, 17 Jan 2023 15:44:44 -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 1pHsoz-0002Uz-8d for bug-gnu-emacs@gnu.org; Tue, 17 Jan 2023 15:44:43 -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=1673988277; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=44zAOtgZoG1fSIWihX5EOFvdWivpX1SgyxP/EBah2l0=; b=bcVVFx+HKEYmj4BDNZ6J+Tg/fbtT5qlZc+8n2duvc6EsZ6g9y7RErXoOGqXfENHX52d3Tn EDRHi45Ar6zQn5oZ0vmU8+VXrRyYvqxvbOzVcdDd02BsQH+704CCJuK/2l9HMB+D4SUgNb GuY+U6r2CbEnpShh1Fhl9OHxZmY8Wv267nti9brYB+XJgnhoZR0qWTXSdZJ1Nye1SfrfCa DUQmJpr8mBuTlTo5F9AMiP+9D4sZrTKxPVvSolFzEMd2wfvxdKGiExfZjCa1vUe5+ya9Aj oJLY+8momxP5W9UWgR4KwqmYHDqH9QqVn8C3K+KmCq9xuewmH+BzRw3//zpd7Q== 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: 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:253581 Archived-At: --=-=-= Content-Type: text/plain Hi Emacs (also Juri and Mickey as you've expressed some interest for this) This is an example patch for sexp movement with tree sitter. I want to put it out here to hopefully produce some discussion, sooner rather than later. Three initial questions: 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()|; ``` 2. Should this new function be leveraged in transpose-sexps? IMO if the forward-sexp gets too close to forward-word, or forward-sentence we lose some nice properties with the current 'treesit-transpose-sexps', namely (among others): ``` f(String foo,| Integer bar) -> void foo(Integer bar, String foo|) ``` I know you Mickey have expressed some dissatisfaction with the current implementation - now is a good time to make some worthwhile improvements. 3. What are the "rules"? In c-mode, elisp-mode without paredit forward-sexp won't jump out of the current scope, however with paredit enabled it will. If we simply want some code similar to this to live and slowly evolve I guess we can install something like this patch after some tweaks and iterate when we have more experience. Anyway, I hope these questions and thoughs will spark some discussion, Theo --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-treesit-forward-sexp.patch >From b02d09216cad2833f96decf46587d51a5ce98ea9 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/java-ts-mode.el | 15 +++++++++++++++ lisp/treesit.el | 17 +++++++++++++++++ 2 files changed, 32 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/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 --=-=-=--