From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Average-user-facing interface for tree-sitter Date: Mon, 24 Oct 2022 08:57:02 -0400 Message-ID: References: <8BAAB6CC-C8BA-4255-9E60-8963A828BE31@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="30709"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) Cc: Theodor Thornhill , Lars Ingebrigtsen , emacs-devel To: Fu Yuan Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Oct 24 15:26:15 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 1omxT4-0007lr-Bz for ged-emacs-devel@m.gmane-mx.org; Mon, 24 Oct 2022 15:26:14 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1omx15-0004wE-Cf; Mon, 24 Oct 2022 08:57:19 -0400 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 1omx10-0004sY-UQ for emacs-devel@gnu.org; Mon, 24 Oct 2022 08:57:16 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1omx0u-0006R8-NW for emacs-devel@gnu.org; Mon, 24 Oct 2022 08:57:10 -0400 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 7F1FC100123; Mon, 24 Oct 2022 08:57:06 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 6E20A10001C; Mon, 24 Oct 2022 08:57:04 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1666616224; bh=8GUlb/dEzwGtV03l9Eouu4kegNiKayFA4tOKRTeKQZ0=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=Q2as25axkj2e4gz88tjVuPshLgUm1/KROBD/ZgfXUpVqH6UuhE4ZDkzqEG4CpbyMU Eo3NQImGLQXjId1ONkCcWP/58mt683gsSfwpuoOPqidcTWXl7R5heqL9NDyHQiXH0z 0pQWeNPWCPqd97DTHbyUryQRT/tF9Dd7OIs9+ooYwoizMvbAevfkMyxmidpKEnwXB3 DInCX/jw2ZtCl0qEGPd9emrfA7b1YdeEkhTxjsN+K7hxniENwdnMCSGrFebWSYhqje X91/ylTEjou5OIIhjQcAXzrttZpdJWiGYEMay7qwvEP9V+dtq1R642851j2uW0cOs4 nxK+ZnRpGc/lA== Original-Received: from pastel (65-110-220-202.cpe.pppoe.ca [65.110.220.202]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 0D5B2120516; Mon, 24 Oct 2022 08:57:04 -0400 (EDT) In-Reply-To: <8BAAB6CC-C8BA-4255-9E60-8963A828BE31@gmail.com> (Fu Yuan's message of "Sat, 22 Oct 2022 18:59:35 -0700") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=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: , Original-Sender: "Emacs-devel" Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:298366 Archived-At: > Here=E2=80=99s my thought (that didn=E2=80=99t 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 =E2=80=9Cbacke= nds=E2=80=9D > 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) =20 - (setq-local parse-sexp-ignore-comments t) (setq-local which-func-imenu-joiner-function #'js--which-func-joiner) =20 ;; 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) =20 - ;; 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) =20 ;; for filling, pretend we're cc-mode - (c-foreign-init-lit-pos-cache) - (add-hook 'before-change-functions #'c-foreign-truncate-lit-pos-cache ni= l 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-cac= he 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) + ))) =20 (defvar js-json--treesit-font-lock-settings (treesit-font-lock-rules