all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Fu Yuan <casouri@gmail.com>
Cc: Theodor Thornhill <theo@thornhill.no>,
	 Lars Ingebrigtsen <larsi@gnus.org>,
	 emacs-devel <emacs-devel@gnu.org>
Subject: Re: Average-user-facing interface for tree-sitter
Date: Mon, 24 Oct 2022 08:57:02 -0400	[thread overview]
Message-ID: <jwvlep52yky.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <8BAAB6CC-C8BA-4255-9E60-8963A828BE31@gmail.com> (Fu Yuan's message of "Sat, 22 Oct 2022 18:59:35 -0700")

> Here’s my thought (that didn’t go anywhere): since major modes sets
> a plethora of local hooks and variables, only the major mode itself knows
> how to reverse them. The cleanest way is probably to clear all the local
> variables and hooks and re-run the major mode setup, which suggests we
> should let major mode branch on whether to enable tree-sitter during
> initialization. I wonder if minor modes can somehow work with this model?

Re-running is fairly problematic.  Not only because it risks repeating
side effects but also because it starts by killing all buffer-local
vars, so we'd need extra hacks to try and preserve the treesit-mode's
own information (making it permanent-local is one way, but that can
cause further breakage when the user really wants to change to another
mode, so it tends to be hackish).

> It would be also nice to leave room for inclusion of other “backends”
> besides elisp and tree-sitter in the future.

I'm not comfortable with this notion of "backend", because each one of
those "backends" (elisp, treesit, eglot, ...) tends to support
a different set of features, so in practice, I'd expect that in the
common case many major modes will use a mix of those backends.

A simple solution, tho not as elegant as I'd like, is to keep the code
we have (where the major mode sets all vars upfront) but add to the
major mode something like:

   (add-hook 'treesit-mode-hook #'js--treesit-mode-hook nil t)
   (js--treesit-mode-hook)

where `js--treesit-mode-hook` is in charge of removing those settings
that don't apply when `treesit-mode` is enabled` (and to re-instate
them when `treesit-mode` is disabled, which is why I call it right away
in the example above, so we don't duplicate the code between the major
mode's body and the `js--treesit-mode-hook`).

Sample completely untested patch below.

We could try and help write this code by providing a helper function
that relies on some buffer-local var containing a list of vars to be set
(along with their values), a list of hooks to add (and remove), ...
so we don't need to duplicate the list into a "set" and an "unset"
branch like I had to do in the patch.

Note that it's very similar to a "backend" function.  But it's only
meant to choose between "treesit activated" and "treesit not activated".


        Stefan


diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index 52160fbb5ee..94295da5167 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3617,14 +3617,8 @@ js-mode
               (list js--font-lock-keywords nil nil nil nil
                     '(font-lock-syntactic-face-function
                       . js-font-lock-syntactic-face-function)))
-  (setq-local syntax-propertize-function #'js-syntax-propertize)
-  (add-hook 'syntax-propertize-extend-region-functions
-            #'syntax-propertize-multiline 'append 'local)
-  (add-hook 'syntax-propertize-extend-region-functions
-            #'js--syntax-propertize-extend-region 'append 'local)
   (setq-local prettify-symbols-alist js--prettify-symbols-alist)
 
-  (setq-local parse-sexp-ignore-comments t)
   (setq-local which-func-imenu-joiner-function #'js--which-func-joiner)
 
   ;; Comments
@@ -3634,25 +3628,11 @@ js-mode
   (setq-local fill-paragraph-function #'js-fill-paragraph)
   (setq-local normal-auto-fill-function #'js-do-auto-fill)
 
-  ;; Parse cache
-  (add-hook 'before-change-functions #'js--flush-caches t t)
-
-  ;; Frameworks
-  (js--update-quick-match-re)
-
-  ;; Syntax extensions
-  (unless (js-jsx--detect-and-enable)
-    (add-hook 'after-change-functions #'js-jsx--detect-after-change nil t))
-  (js-use-syntactic-mode-name)
-
   ;; Imenu
   (setq imenu-case-fold-search nil)
   (setq imenu-create-index-function #'js--imenu-create-index)
 
   ;; for filling, pretend we're cc-mode
-  (c-foreign-init-lit-pos-cache)
-  (add-hook 'before-change-functions #'c-foreign-truncate-lit-pos-cache nil t)
-  (setq-local comment-line-break-function #'c-indent-new-comment-line)
   (setq-local comment-multi-line t)
   (setq-local electric-indent-chars
 	      (append "{}():;," electric-indent-chars)) ;FIXME: js2-mode adds "[]*".
@@ -3698,7 +3678,51 @@ js-mode
                       "function_declaration"
                       "lexical_declaration")))
   (setq-local treesit-font-lock-settings js--treesit-font-lock-settings)
-  (setq-local treesit-font-lock-feature-list '((minimal) (moderate) (full))))
+  (setq-local treesit-font-lock-feature-list '((minimal) (moderate) (full)))
+
+  (add-hook 'treesit-mode-hook #'js--treesit-mode-hook nil t)
+  (js--treesit-mode-hook))
+
+(defun js--treesit-mode-hook ()
+  (cond
+   (treesit-mode
+    (kill-local-variable 'syntax-propertize-function)
+    (remove-hook 'syntax-propertize-extend-region-functions
+                 #'syntax-propertize-multiline 'local)
+    (remove-hook 'syntax-propertize-extend-region-functions
+                 #'js--syntax-propertize-extend-region 'local)
+    (kill-local-variable 'parse-sexp-ignore-comments)
+    (remove-hook 'before-change-functions #'js--flush-caches t)
+
+    ;; Syntax extensions
+    (remove-hook 'after-change-functions #'js-jsx--detect-after-change t)
+    (js-use-syntactic-mode-name)        ;FIXME?
+
+    (remove-hook 'before-change-functions #'c-foreign-truncate-lit-pos-cache t)
+    (kill-local-variable 'comment-line-break-function)
+    )
+   (t
+    ;; Parse cache
+    (setq-local syntax-propertize-function #'js-syntax-propertize)
+    (add-hook 'syntax-propertize-extend-region-functions
+              #'syntax-propertize-multiline 'append 'local)
+    (add-hook 'syntax-propertize-extend-region-functions
+              #'js--syntax-propertize-extend-region 'append 'local)
+    (setq-local parse-sexp-ignore-comments t)
+    (add-hook 'before-change-functions #'js--flush-caches t t)
+
+    ;; Frameworks
+    (js--update-quick-match-re)
+
+    ;; Syntax extensions
+    (unless (js-jsx--detect-and-enable)
+      (add-hook 'after-change-functions #'js-jsx--detect-after-change nil t))
+    (js-use-syntactic-mode-name)
+
+    (c-foreign-init-lit-pos-cache)
+    (add-hook 'before-change-functions #'c-foreign-truncate-lit-pos-cache nil t)
+    (setq-local comment-line-break-function #'c-indent-new-comment-line)
+    )))
 
 (defvar js-json--treesit-font-lock-settings
   (treesit-font-lock-rules




  parent reply	other threads:[~2022-10-24 12:57 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-12  6:11 Average-user-facing interface for tree-sitter Yuan Fu
2022-10-13  0:54 ` [SPAM UNSURE] " Stephen Leake
2022-10-13  6:32   ` Stephen Leake
2022-10-13  6:22 ` Lars Ingebrigtsen
2022-10-13  9:18   ` Robert Pluim
2022-10-13  9:21     ` Lars Ingebrigtsen
2022-10-13  9:32     ` Po Lu
2022-10-13  9:42       ` Robert Pluim
2022-10-13 12:31         ` Stefan Kangas
2022-10-13  9:57     ` Daniel Martín
2022-10-13 10:01     ` [SPAM UNSURE] " Stephen Leake
2022-10-13 14:32   ` Jostein Kjønigsen
2022-10-13 16:14     ` Eli Zaretskii
2022-10-13 17:27     ` Lars Ingebrigtsen
2022-10-13 19:44   ` Yuan Fu
2022-10-14 11:02     ` Lars Ingebrigtsen
2022-10-14 11:22       ` Stephen Leake
2022-10-14 20:10         ` Yuan Fu
2022-10-14 20:19       ` Yuan Fu
2022-10-14 20:49     ` Stefan Monnier
2022-10-14 22:51       ` Yuan Fu
2022-10-15  3:26         ` Stefan Monnier
2022-10-15  5:05           ` Yuan Fu
2022-10-17  9:07             ` Yuan Fu
2022-10-17  9:15               ` Lars Ingebrigtsen
2022-10-18 20:54                 ` Yuan Fu
2022-10-18 21:48                   ` Stefan Monnier
2022-10-18 22:06                     ` Yuan Fu
2022-10-18 22:31                       ` Stefan Monnier
2022-10-18 23:06                         ` Yuan Fu
2022-10-19  2:52                           ` Stefan Monnier
2022-10-19  3:48                             ` [External] : " Drew Adams
2022-10-20  0:23                             ` Yuan Fu
2022-10-19  5:35                           ` Theodor Thornhill
2022-10-20  0:28                             ` Yuan Fu
2022-10-20  7:44                               ` Theodor Thornhill
2022-10-20 17:53                                 ` Stefan Monnier
2022-10-20 18:10                                   ` Theodor Thornhill
2022-10-20 18:11                                     ` Theodor Thornhill
2022-10-20 23:06                                     ` Yuan Fu
2022-10-21 22:10                                       ` Yuan Fu
2022-10-21 22:35                                         ` Stefan Monnier
2022-10-23  1:59                                           ` Fu Yuan
2022-10-23  4:59                                             ` Theodor Thornhill
2022-10-24 12:57                                             ` Stefan Monnier [this message]
2022-10-24 17:14                                               ` Stephen Leake
2022-10-24 21:07                                                 ` Stefan Monnier
2022-10-24 20:51                                               ` Yuan Fu
2022-10-24 23:55                                                 ` Stefan Monnier
2022-10-25 21:37                                                   ` Yuan Fu
2022-10-25 22:49                                                     ` Stefan Monnier
2022-10-27  1:56                                                       ` Yuan Fu
2022-10-27 15:21                                                         ` Stefan Monnier
2022-10-27 15:29                                                           ` Dmitry Gutov
2022-10-28  8:02                                                             ` Yuan Fu
2022-10-24 16:46                                             ` Stephen Leake
2022-10-18 20:49             ` Stefan Monnier
2022-10-18 20:58               ` Yuan Fu
2022-10-13 13:05 ` Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2022-10-15  9:49 Payas Relekar
2022-10-16 11:03 ` Katevan Lomidze
2022-10-16 11:43   ` Eli Zaretskii
2022-10-15 13:03 Ketevan Lomidze

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvlep52yky.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=casouri@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=larsi@gnus.org \
    --cc=theo@thornhill.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.