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: Tree-sitter api Date: Sun, 8 Aug 2021 19:06:55 -0500 Message-ID: <8FA7E590-BE8A-4EA8-9E7A-A228BAB0E666@gmail.com> References: <83r1f7hydn.fsf@gnu.org> <95F37923-5BF9-4D81-B361-267CF119FBCA@gmail.com> <68AF72EA-8662-4508-B46C-0ACA94A20E86@gmail.com> Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.60.0.2.21\)) 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="17500"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , Stephen Leake , =?utf-8?Q?Cl=C3=A9ment_Pit-Claudel?= , emacs-devel To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Aug 09 02:07:37 2021 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 1mCspN-0004Lq-3x for ged-emacs-devel@m.gmane-mx.org; Mon, 09 Aug 2021 02:07:37 +0200 Original-Received: from localhost ([::1]:54458 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mCspL-0001dF-PJ for ged-emacs-devel@m.gmane-mx.org; Sun, 08 Aug 2021 20:07:35 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59776) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mCsom-0000QJ-4a for emacs-devel@gnu.org; Sun, 08 Aug 2021 20:07:00 -0400 Original-Received: from mail-ot1-x332.google.com ([2607:f8b0:4864:20::332]:45692) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mCsok-0002OY-JH; Sun, 08 Aug 2021 20:06:59 -0400 Original-Received: by mail-ot1-x332.google.com with SMTP id r17-20020a0568302371b0290504f3f418fbso1843736oth.12; Sun, 08 Aug 2021 17:06:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=YrWifx0D5S3TFcQYm/G16tb3z0xmMqPRMQ+qQad7A0Y=; b=B3N2SkW9tl/4HBKXX2XdI6yFjeCGQxpSOPdP9EHWLqGpgmqcBitpbf1o+QmwJ94cRf z3Tec8F56xMQ0GLpB8TXJ+YA6nIMy/HDuBthhUmTpgtKS4955tZxn9dCam4LsdURKJSb prP0OwzvqdUa4Xd4nq97353SugfEc+aFAwDpcqUiigbo2BHQIG00eXglqVZQ/ERW2Jfy eq/2SAbf2DqxqmcZsRpQVjaZMXWzOFM1jAM7BLsqWmJeV48scAwK0CNIAROq7AAztIu7 2DSrZBiDPxh+REbuoWOvbLj6aaddNdnGNi4/Rdzwxgr9JYaGK30z2Vh38ZkOZH9d4+0N UQ/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=YrWifx0D5S3TFcQYm/G16tb3z0xmMqPRMQ+qQad7A0Y=; b=ryDLs3lsX9tbyw/5BvEUJ5zxXC66Edbrq/QIFndhMv5lng6kzr8aY2aPsyJaPSan6V WuITkbVnaE2oDnqK8vWjFB9W6QfHPw6v5TUqg6D1wNATsnOMHhj95WV690JvJ6u/HzLC LgTiWM2fxum3Octdz3OyOsKV4PaER/e2mKmwRrz4NUFkMVElvhpubTjbxHM+xkOMD/G7 nzLb4YaIyrJbrWMvnliSRsMmC12rmCYkJpcFYGP6pENzIu9HqlMRiX3at0Ptq4SNdpGy T+Gyg+VQkctdMqJaIPWOEfztpw7eCzewbnmiRgn3C70JjDaz8Hh2j8ewc6Nweo15BhdG +CnA== X-Gm-Message-State: AOAM532IshCYzBpZM6n4ra7aCIiNXe+QaVhWvyGCQvJq6Y2HAUqOFmXq 9MGtuxABRG0BhBVSRE4c5Lk= X-Google-Smtp-Source: ABdhPJy0eDQHcZ8w14TCEZL5QAdTtpqt55kNikOJErdSfd40qe86tYtpO0ciwFDRYmEBSbnrpshnyw== X-Received: by 2002:a05:6830:1d46:: with SMTP id p6mr15219031oth.274.1628467616785; Sun, 08 Aug 2021 17:06:56 -0700 (PDT) Original-Received: from [192.168.140.114] ([209.36.134.66]) by smtp.gmail.com with ESMTPSA id 9sm2981805oir.13.2021.08.08.17.06.56 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Aug 2021 17:06:56 -0700 (PDT) In-Reply-To: X-Mailer: Apple Mail (2.3654.60.0.2.21) Received-SPF: pass client-ip=2607:f8b0:4864:20::332; envelope-from=casouri@gmail.com; helo=mail-ot1-x332.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.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:272219 Archived-At: > On Aug 8, 2021, at 6:24 PM, Stefan Monnier = wrote: >=20 > Yuan Fu [2021-08-08 17:56:33] wrote: >>> On Aug 7, 2021, at 10:47 AM, Stefan Monnier = wrote: >>>> We should have a user option to control tree-sitter on major mode >>>> level. Maybe an alist where each car is a major node symbol and = each cdr is >>>> a Boolean value toggling tree-sitter for that node. >>> The more traditional approach is to use a buffer-local var set by = the >>> major mode or set via (add-hook '-hook ...). >> The major-mode would setup things like font-lock-defaults and >> tree-sitter-defaults before major-mode-hook runs, so I think >> enabling/disabling tree-sitter in the hook is too late, no? >=20 > I don't see why. Presumably the major mode would set some vars = relevant > to the tree-sitter support, but then whether those vars are used will > depend on the buffer-local boolean var (let's call it = `tree-sitter-mode`). >=20 > I'm sure there will be issues w.r.t initialization order, e.g. in case > `font-lock-mode` is enabled before `tree-sitter-mode`, but that = doesn't > seem very serious (`font-lock-mode` doesn't do much anyway, since the > real work is postponed until the next redisplay by jit-lock, so we = could > "refresh" font-lock settings fairly cheaply within = `tree-sitter-mode`). Instead of a tree-sitter-mode, I made font-lock use tree-sitter features = in addition to using keywords. Basically I added another fontification = pass (tree-sitter pass) in addition to the current two, syntactic pass = and regex pass.=20 (Syntactic pass is probably unnecessary if tree-sitter is enabled, tho). = This way someone can still add regexp-based fontification even he uses = tree-sitter for =E2=80=9Cstandard=E2=80=9D fontification. And under this scheme, a major-mode would want something like this in = the major-mode definition: (if (tree-sitter-should-enable-p) (progn (setq-local font-lock-tree-sitter-defaults '((ts-c-tree-sitter-settings-1))) (setq-local font-lock-defaults (ignore t nil nil nil))) (setq-local font-lock-defaults '((c-font-lock-keywords c-font-lock-keywords-1 c-font-lock-keywords-2 c-font-lock-keywords-3) nil nil ((95 . "w") (36 . "w")) c-beginning-of-syntax (font-lock-mark-block-function . c-mark-function)))) In this scheme, changing whether to enable tree-sitter is too late in = major-mode-hook (not impossible, of course). Yuan=