unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#60894: 30.0.50; [PATCH] Add treesit-forward-sexp
@ 2023-01-17 20:44 Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-01-17 20:53 ` Dmitry Gutov
  2023-01-17 21:13 ` Mickey Petersen
  0 siblings, 2 replies; 25+ messages in thread
From: Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-01-17 20:44 UTC (permalink / raw)
  To: 60894; +Cc: Juri Linkov, Mickey Petersen, Stefan Monnier

[-- Attachment #1: Type: text/plain, Size: 1683 bytes --]


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



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-treesit-forward-sexp.patch --]
[-- Type: text/x-patch, Size: 2961 bytes --]

From b02d09216cad2833f96decf46587d51a5ce98ea9 Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <theo@thornhill.no>
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


^ permalink raw reply related	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2023-01-19 19:03 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-17 20:44 bug#60894: 30.0.50; [PATCH] Add treesit-forward-sexp Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-17 20:53 ` Dmitry Gutov
2023-01-17 21:07   ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-18  1:50     ` Dmitry Gutov
2023-01-18  5:35       ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-18 12:21         ` Eli Zaretskii
2023-01-18 13:39         ` Dmitry Gutov
2023-01-18 18:28           ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-18 19:01           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-18 21:59             ` Dmitry Gutov
2023-01-19  2:43               ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-19  3:30                 ` Dmitry Gutov
2023-01-19  3:58                   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-19 18:44                     ` Dmitry Gutov
2023-01-19 19:03                       ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-18 17:16         ` Juri Linkov
2023-01-18 18:27           ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-17 21:13 ` Mickey Petersen
2023-01-18 13:31   ` Dmitry Gutov
2023-01-18 17:09   ` Juri Linkov
2023-01-18 18:27     ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-18 18:55       ` Juri Linkov
2023-01-18 22:06       ` Dmitry Gutov
2023-01-19  6:24         ` Eli Zaretskii
2023-01-19  7:58         ` Juri Linkov

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).