From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.devel Subject: Re: Average-user-facing interface for tree-sitter Date: Mon, 24 Oct 2022 13:51:37 -0700 Message-ID: <41838564-B3F1-46C8-A316-EE888C4B9968@gmail.com> References: <8BAAB6CC-C8BA-4255-9E60-8963A828BE31@gmail.com> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) 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="12961"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Theodor Thornhill , Lars Ingebrigtsen , emacs-devel To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Oct 24 23:29:00 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 1on50G-00039c-R2 for ged-emacs-devel@m.gmane-mx.org; Mon, 24 Oct 2022 23:29:00 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1on4QC-0000ud-RJ; Mon, 24 Oct 2022 16:51:44 -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 1on4QB-0000tU-Al for emacs-devel@gnu.org; Mon, 24 Oct 2022 16:51:43 -0400 Original-Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1on4Q9-0008Qi-K2 for emacs-devel@gnu.org; Mon, 24 Oct 2022 16:51:43 -0400 Original-Received: by mail-pl1-x634.google.com with SMTP id d24so9475709pls.4 for ; Mon, 24 Oct 2022 13:51:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=g57s3up9emBC4PqB1TWp4ynG6Gy1dNTXUnD1unAW440=; b=RVPZFFlWVxRH6jDTBKGHgyI4V5hd3Sjiky5KQStn3flHTQEvQthoEJ1aebhP4QcpBG sZAhFxXTsO5aa9H0Utyi4SmZifh8e0TwiIMYkUtSY/wcGDPnoZYezNAwucKrffyfQvy0 AFiY+WPNX1I6re0H6FF3hGYGs3GNUYVIsH2qoOksZUF9k+00E6/tWpdbsGv5lGqTGtKy zh6qIITjQnZmx3ogG4oUUFwfs31S46nHP5c6zGSeT+6S3JhubkvkYUugbi0Lyzm/oR/3 9jLTV2BHhsmJHM/5NZyTIB2TsCJqjUIwtiTAD8bM2o66oxFdtFHpIcD+EZ3Reb+TCkg3 MFUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=g57s3up9emBC4PqB1TWp4ynG6Gy1dNTXUnD1unAW440=; b=fhXw201Q8CnxrcbEt0v5qgjMKY53RhvufafN2UNXKMLU0ne72qy3DtpymQ40J86pLN Nu7ivBTawTTvhRnYxtzda79N6sgcc6gwmYUAU5kbBHuR0n+v2CQ1wJPCgOABEMcu3jLk //TvtSUuoM7B3ZQt//QTIwpYNHlulkb/pkd3iUB74pqXlDBvSJZjom0Vwc+oHSzITRmi DYNWiRP/ulxw05BHvW0Le7cPyeaAlsGPG9NtJbyp9eesDSqX8WgAo7/U9EkdhIwNkucj Cb7XH/YOxzhNHv/rmCcTQue8JbYPe5KYMxX+feFP6GfrlPmBIpmYlG5rVDvIwq63U84p dbKw== X-Gm-Message-State: ACrzQf0evKTJbHOySrE4YZxRyPZqQJJ8T8o9SQeFb0k0C3FQ36dldREn p7HTAVIXktm15lPKmi98o2A= X-Google-Smtp-Source: AMsMyM4lAhd0SkrPDGAuJN6m5VkTaCoYxtWRT8NdmSP+U5QwWfAQWCam2yVanFdT1yvjE1m9lUWFpw== X-Received: by 2002:a17:902:6847:b0:186:61be:ef82 with SMTP id f7-20020a170902684700b0018661beef82mr24676484pln.135.1666644699884; Mon, 24 Oct 2022 13:51:39 -0700 (PDT) Original-Received: from smtpclient.apple (cpe-172-117-161-177.socal.res.rr.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id om9-20020a17090b3a8900b0020de216d0f7sm281065pjb.18.2022.10.24.13.51.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Oct 2022 13:51:39 -0700 (PDT) In-Reply-To: X-Mailer: Apple Mail (2.3696.120.41.1.1) Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=casouri@gmail.com; helo=mail-pl1-x634.google.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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, 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:298425 Archived-At: > On Oct 24, 2022, at 5:57 AM, Stefan Monnier = wrote: >=20 >> 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? >=20 > 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). I agree, tho I think the risk of side effect is a bit exaggerated.=20 >=20 >> It would be also nice to leave room for inclusion of other = =E2=80=9Cbackends=E2=80=9D >> besides elisp and tree-sitter in the future. >=20 > 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. >=20 > 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: >=20 > (add-hook 'treesit-mode-hook #'js--treesit-mode-hook nil t) > (js--treesit-mode-hook) >=20 > 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`). >=20 > Sample completely untested patch below. >=20 > 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. >=20 > Note that it's very similar to a "backend" function. But it's only > meant to choose between "treesit activated" and "treesit not = activated=E2=80=9D. Now the minor mode scheme is getting more and more complicated that I = wonder if the benefit is worth it. Maybe we should revisit the central = variable scheme, which I still think is pretty clean and convenient: >=20 >>=20 >> This function would handle derived mode ok, but I don=E2=80=99t know = what are >> the other problems you are think of, maybe you can tell me what this >> function falls short for. And we can go from there. >=20 > The function is one thing. Another question is when it is run, and > hence how it interacts with other ways to enable/set/control the thing > you want to control (in this case enabling `treesit-mode`, IIUC). There will not be treesit-mode and global-treesit-mode, only the central = variable treesit-settings. Enabling tree-sitter only requires adding = (mode t) to treesit-setttings, and M-x revert-buffer, or reopen the = file, or rerun the major mode. Actually, 99% of the time people would = just set that setting, enabling tree-sitter for a mode (or all supported = modes) and be done with it. Ie, there is not a lot of toggling that = calls for a minor mode. Minor mode are still nice since one can add it = to the major mode hook, but we=E2=80=99ve seen that it brings more = trouble than benefit. Letting major modes to branch on a global variable is also cleaner and = easier to write than adding code to enable/disable a bunch of setups, = IMO. At this point, I think we see how minor modes clearly are a bit awkward = to control major mode behavior with. And AFAIK the central variable = doesn=E2=80=99t have any visible issues. So I think central variable is = better. Yuan