From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill Newsgroups: gmane.emacs.devel Subject: Re: Disable tree-sitter font-locking for smaller ranges Date: Mon, 17 Oct 2022 07:49:35 +0200 Message-ID: References: <877d109hz1.fsf@thornhill.no> <672FF8D1-E5FB-44D8-8E6D-38EB94843B3B@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="26871"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Yuan Fu Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Oct 17 07:57:22 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 1okJ7o-0006ni-Fi for ged-emacs-devel@m.gmane-mx.org; Mon, 17 Oct 2022 07:57:20 +0200 Original-Received: from localhost ([::1]:42778 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1okJ7m-0008Jv-Qg for ged-emacs-devel@m.gmane-mx.org; Mon, 17 Oct 2022 01:57:18 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:40616) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1okJ1F-0004Tg-Rm for emacs-devel@gnu.org; Mon, 17 Oct 2022 01:50:34 -0400 Original-Received: from out0.migadu.com ([2001:41d0:2:267::]:39510) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1okJ1D-0002J6-Is for emacs-devel@gnu.org; Mon, 17 Oct 2022 01:50:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thornhill.no; s=key1; t=1665985829; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8IrQ+M5YPViJI18BFFVR7MAy6TI6hBWzARJTKUCe538=; b=MPitvghCQMcEo9brjFfTIU3+uq8IYMEbpboPnnboknbBMAk7+jowhjZExgS3eT2NwLIxdQ +xlXYv9k9u/xD/GeQ34MqNkmxKrvY4pOvj4s5rhZ5YH5/0qEADXuydTfbwSSEELzM2ie7n gtomuw3OA6ysymqo/bZUhnAPcNeWOQi+dQbk6DkKbY9XjuU7eMkOALYUyX3BmzQdVnge+S 68WmhtAn98mgB8yfkyem+2SpJyFD+OLpa+JSGuwSkaGOSVmbikTRacrLyD7ePXc5eFOJUT ijLUYPBK1zjggMw0188mSKhHbwE8y/jU2LYWpHbDgFf4cEaacdh2CfV2uGI0Hg== X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. In-Reply-To: <672FF8D1-E5FB-44D8-8E6D-38EB94843B3B@gmail.com> X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=2001:41d0:2:267::; envelope-from=theo@thornhill.no; helo=out0.migadu.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-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: , 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:297913 Archived-At: On 17 October 2022 07:04:58 CEST, Yuan Fu wrote: > > >> On Oct 16, 2022, at 3:32 AM, Theodor Thornhill wr= ote: >>=20 >>=20 >> Hi Yuan! >>=20 >> I've been trying to tweak the font-locking for tree-sitter in >> js/ts-mode, and I'm still struggling with the template strings=2E Is >> there a way to _disable_ font-locking for smaller ranges? >>=20 >> Let's say you have this string: >> ``` >> `Some string with ${5+5} many template substitutions ${foo()=2Ebar()=2E= baz()}`; >> ``` >>=20 >> This string will match something like: >> ``` >> (template_string (template_substitution) :*) >> ``` >>=20 >> If you use this as a query: >> ``` >> (template_string) @font-lock-string-face >> ``` >>=20 >> Everything inside the string is in string-face=2E >>=20 >> If you add >> ``` >> (template_substitution >> "${" @font-lock-constant-face >> (_) >> "}" @font-lock-constant-face) >> ``` >>=20 >> You get font-locking in the ranges inside the squigglies, as expected= =2E >> However, if there isn't defined any rules for say, "(), =2E []" etc, th= e >> template_string capture will bleed into the substitution, because its >> range suggests it should=2E It would be nice to say something like: >>=20 >> ``` >> (defvar fonts () >> (treesit-font-lock-rules >> :language 'tsx >> :override t >> :feature 'basic >> '((template_string (_):* @disabled) @font-lock-string-face=20 >> (template_substitution ["${" "}"] @font-lock-constant-face))) >> ``` >>=20 >> to ensure that whatever is inside the wildcard match will _not_ be >> considered for the string face, but whatever is still inside the bigger >> range will=2E Is this currently possible? If not, is >> it possible to add? I guess I could make a function that would remove >> the range, but that seems fiddly and error prone=2E >>=20 >> I've tried fiddling with the :feature flag in 'treesit-font-lock-rules'= , >> but I never found a combination that did what I wanted=2E >>=20 >> Adding a small image of a stupid snipped just to make the point more vi= sual > >The image you attached looks perfectly fine to me=2E Do you not want to f= ont-lock what=E2=80=99s inside a substitution? I=E2=80=99m not exactly sure= what result you want to archive=2E > All the parens, braces, equal, semicolons should be white, as they would i= n the source file outside of the template string=2E But the string outside = of the ${=2E=2E=2E} should be string colored=2E=20 >If you don=E2=80=99t want font-lock in substitutions, you can put=20 > >> '((template_string) @font-lock-string-face=20 >> (template_substitution ["${" "}"] @font-lock-constant-face)) > > >After all other queries, and mark these two with :override t=2E That shou= ld do it=2E > Ill try that!=20 >Also, when I was browsing tsx=E2=80=99s grammar file, I didn=E2=80=99t fi= nd definition of template_substitution at all=2E That means at least some p= art of js and tsx=E2=80=99s grammar are not interchangeable=2E > >Yuan They do some inheritance shenanigans in their files=2E Most of the grammar= is from tree-sitter-Javascript=2E=20 Theo