GNU bug report logs - #77256
Treesit language-at-point

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: emacs; Reported by: Juri Linkov <juri@HIDDEN>; dated Tue, 25 Mar 2025 18:44:02 UTC; Maintainer for emacs is bug-gnu-emacs@HIDDEN.

Message received at 77256 <at> debbugs.gnu.org:


Received: (at 77256) by debbugs.gnu.org; 4 Apr 2025 16:15:49 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Apr 04 12:15:49 2025
Received: from localhost ([127.0.0.1]:39699 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1u0jht-00009h-Eu
	for submit <at> debbugs.gnu.org; Fri, 04 Apr 2025 12:15:49 -0400
Received: from relay3-d.mail.gandi.net ([2001:4b98:dc4:8::223]:43409)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <juri@HIDDEN>) id 1u0jhY-000069-Ag
 for 77256 <at> debbugs.gnu.org; Fri, 04 Apr 2025 12:15:28 -0400
Received: by mail.gandi.net (Postfix) with ESMTPSA id 7FF362047C;
 Fri,  4 Apr 2025 16:15:19 +0000 (UTC)
From: Juri Linkov <juri@HIDDEN>
To: Yuan Fu <casouri@HIDDEN>
Subject: Re: bug#77256: Treesit language-at-point
In-Reply-To: <8B15AB89-2A73-4260-AFD5-400B11342625@HIDDEN>
Organization: LINKOV.NET
References: <87a5990x6t.fsf@HIDDEN>
 <9271D97C-0820-4C70-9A58-88988F2E6BF9@HIDDEN>
 <87plhxw7fh.fsf@HIDDEN>
 <73DD04E6-D914-4A14-8DA9-876519BBEF4D@HIDDEN>
 <871pucdti6.fsf@HIDDEN>
 <8B15AB89-2A73-4260-AFD5-400B11342625@HIDDEN>
Date: Fri, 04 Apr 2025 19:14:11 +0300
Message-ID: <87y0wgx8l8.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu)
MIME-Version: 1.0
Content-Type: text/plain
X-GND-State: clean
X-GND-Score: -100
X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduledukeelucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesthdtredttdertdenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpeffgeetfeevlefhleejfeeuheeiudeitdffhfdutdekfeffgffhveehteegueekheenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepfedprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhm
X-GND-Sasl: juri@HIDDEN
X-Spam-Score: -0.7 (/)
X-Debbugs-Envelope-To: 77256
Cc: 77256 <at> debbugs.gnu.org, Vincenzo Pupillo <v.pupillo@HIDDEN>
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.7 (-)

>>> We should also update manual sections that mentions treesit-language-at.
>> 
>> Will do when everything is ok with this patch:
>
> Thanks! LGTM.

Unfortunately, I discovered that it causes treesit-node-outdated errors.

This is because overlays are updated by 'pre-redisplay-functions'
later than outline-minor-mode uses them by 'after-change-functions',
since 'after-change-functions' runs before 'pre-redisplay-functions'.

The reproducible test case: with the latest patch for treesit.el,
and another patch that removes 'mhtml-ts-mode--language-at-point'
from mhtml-ts-mode.el, open a simple html file like this:

<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
</body>
</html>

then type:

1. M-x load-library RET treesit-x RET
2. M-x liquid-generic-ts-mode RET
3. M-x outline-minor-mode RET
4. move point before </html>
5. type SPC

then with non-nil debug-on-error:

Debugger entered--Lisp error: (treesit-node-outdated #<treesit-node-outdated>)
  treesit-node-enclosed-p(#<treesit-node-outdated> #<treesit-node element in 17-82>)
  treesit-navigate-thing(73 1 beg html-ts-mode--outline-predicate)
  treesit-outline-search(#<marker at 82 in test.html>)
  outline-map-region(#<subr F616e6f6e796d6f75732d6c616d626461_anonymous_lambda_46> 74 82)
  outline--fix-up-all-buttons(74 82)
  outline--fix-buttons-after-change(74 75 0)
  self-insert-command(1 32)
  funcall-interactively(self-insert-command 1 32)
  command-execute(self-insert-command)




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#77256; Package emacs. Full text available.

Message received at 77256 <at> debbugs.gnu.org:


Received: (at 77256) by debbugs.gnu.org; 1 Apr 2025 23:59:24 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Tue Apr 01 19:59:24 2025
Received: from localhost ([127.0.0.1]:52521 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tzlVs-0000Ea-2R
	for submit <at> debbugs.gnu.org; Tue, 01 Apr 2025 19:59:24 -0400
Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]:45264)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <casouri@HIDDEN>) id 1tzlVp-0000Du-J5
 for 77256 <at> debbugs.gnu.org; Tue, 01 Apr 2025 19:59:22 -0400
Received: by mail-pl1-x632.google.com with SMTP id
 d9443c01a7336-22423adf751so114524375ad.2
 for <77256 <at> debbugs.gnu.org>; Tue, 01 Apr 2025 16:59:21 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1743551955; x=1744156755; darn=debbugs.gnu.org;
 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=yvheMTCFO5vEgT3FR4m7QCwshOXpLPqWZn06dBf6VdI=;
 b=NPSOUGRTqCV6SgwJo1XQgzGm4cS4gx4OUgR2LoY/bTXpl4LphJA1YexaEswoe7NokK
 K5KuT4FmGUihbOT8zDyvwGYtdlnMpLe0Jw9ew1dRYy4/ykvfkVSRqXdfjRklcHDaEnVb
 wayYzVwej0NOpWXHVpi34kNOJtMf7mcxL9+wY86+tpb4442Yxd4JyEVfcxCy4G7DjTnN
 R2zRcT4aIYAzYlph2FO9Q3rIFWfrtVsOklqGEczwIBfvgx6PLWK73dv7hCF2s9iWHGgF
 uq9a1Kc0r3/NwLdxGFHTMmlfTObOfejivi97FmqyTlOFR7Z0eph879zq0G9AA6gGIFYp
 KCgw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1743551955; x=1744156755;
 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=yvheMTCFO5vEgT3FR4m7QCwshOXpLPqWZn06dBf6VdI=;
 b=utoh2zjPnnzGMJxMHCAsEgo0amhTGwhtuwY2XGfhfWfcYdzBheYUy8muELCjYyaVFF
 LcXArChzF7MEJmohaWlzhuoUZYXrdhP4hspoSolqXn7Q7OSOTUcj+ONQKJvHx8zB/50X
 NdZN1e56IH2boZIk5c+8GCC3Zht/lITs95EO1pid+gURnJOA4P4WD20BLoVV+aM1Fxam
 F4IDxaspoPMwPTOIW62hRCMokIMW6+rsreEVaF23dcDMXfrYH8MVmFkKymAGxIbKupRy
 iEobYiearA6lse7/ugeQKqrGVUlz5okSI4X6au9I13VdKeG1teB9E0eL8TNW1mQNAYWb
 Eupg==
X-Gm-Message-State: AOJu0YxQnXRPGTet50/EmohFUw7lcR0qeYogXV4/GM3zbldRhpEdKxnY
 RDS0aTKAQJpzBKKph2+qDsZEBNxwIj/HzXGZQrqM9USnPOQD/YTgDP+zCw==
X-Gm-Gg: ASbGnctv5+bB0srSoCsfn7BLecd/yl1CEVVheGg7KGh2RQuKD9eb/yabguCvRTGi4M4
 1zjS3twADEmEfGKTiDIkwT9aeFzpKaXwv+PohkzdB6uF+iJ+33loQVLLlflKp+Xma7dVRAOT7KO
 5MdL1Ytq+7o+4D1Ehlznjffk0SHfncdP4o01KUgnKvc2RWKo4gwi5mrF0uuIx5JFbvf3FiDfcFP
 E+0YwK3pNpW0mdIYl3guN0oF94ykzEJt/9znyFleiv9yZ08fPtqfem+k/3wSr7XW1Ok4ukybATk
 2okRhJpZkSY7hXIRPbLbMry/2y4pG5w3f/KuKkChUhY0z1f6/5XRJjdQpIjOLbpqsAs=
X-Google-Smtp-Source: AGHT+IGkttlkMdBw+vxLkrJg5UyaPhRjCmTXeBLBaOpEDTgtr7N4T8xTVWEcW1I0kiBdYvCBXSNMDg==
X-Received: by 2002:a17:903:22c7:b0:224:1c41:a4c0 with SMTP id
 d9443c01a7336-2295be31750mr67648785ad.9.1743551955506; 
 Tue, 01 Apr 2025 16:59:15 -0700 (PDT)
Received: from smtpclient.apple ([2601:646:8f81:6120:7c76:5133:615:6915])
 by smtp.gmail.com with ESMTPSA id
 d9443c01a7336-2291f1cf131sm95763045ad.134.2025.04.01.16.59.14
 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128);
 Tue, 01 Apr 2025 16:59:14 -0700 (PDT)
Content-Type: text/plain;
	charset=utf-8
Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\))
Subject: Re: bug#77256: Treesit language-at-point
From: Yuan Fu <casouri@HIDDEN>
In-Reply-To: <871pucdti6.fsf@HIDDEN>
Date: Tue, 1 Apr 2025 16:59:03 -0700
Content-Transfer-Encoding: quoted-printable
Message-Id: <8B15AB89-2A73-4260-AFD5-400B11342625@HIDDEN>
References: <87a5990x6t.fsf@HIDDEN>
 <9271D97C-0820-4C70-9A58-88988F2E6BF9@HIDDEN>
 <87plhxw7fh.fsf@HIDDEN>
 <73DD04E6-D914-4A14-8DA9-876519BBEF4D@HIDDEN>
 <871pucdti6.fsf@HIDDEN>
To: Juri Linkov <juri@HIDDEN>
X-Mailer: Apple Mail (2.3826.400.131.1.6)
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 77256
Cc: 77256 <at> debbugs.gnu.org, Vincenzo Pupillo <v.pupillo@HIDDEN>
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)



> On Apr 1, 2025, at 9:53=E2=80=AFAM, Juri Linkov <juri@HIDDEN> =
wrote:
>=20
>>> @@ -185,15 +185,15 @@ treesit-language-at
>>> This function assumes that parser ranges are up-to-date.  It
>>> returns the return value of `treesit-language-at-point-function'
>>> if it's non-nil, otherwise it returns the language of the first
>>> -parser in `treesit-parser-list', or nil if there is no parser.
>>> +parser from `treesit-parsers-at', or the primary parser.
>>=20
>> If we handle the fallback case in treesit-language-at directly, =
rather than
>> defining a separate default function, IMO we should describe the =
fallback
>> behavior as the default. So something like =E2=80=9CReturn the =
language at
>> POS. When there are multiple parsers that covers POS, determine the =
most
>> relevant parser (hence language) by their embed level. If
>> treesit-language-at-point-function is non-nil, return the return =
value of
>> that function instead.=E2=80=9D (Not saying we should use this exact =
docstring but
>> to illustrate the point.)
>=20
> Now fixed in the next patch.
>=20
>>> +(defun treesit-parsers-at (&optional pos language with-host =
local-only)
>>> +  "Return all the non-primary parsers at POS.
>>=20
>> I get why you used treesit-parsers-at rather than a more technically
>> correct name like treesit-non-primary-parsers-at, or
>> treesit-embeded-parsers-at. But this is confusing for not much =
benefit
>> IMO. I suggest either use treesit-parsers-at, and add an optional =
argument
>> exclude-primary; or use a more correct name and don=E2=80=99t include =
the primary
>> parser. Also, instead of using LOCAL-ONLY, we might be more =
future-prove to
>> use a ONLY parameter, and let user pass =E2=80=98local to mean =
local-only. This way
>> we can add the option of returning only non-local non-primary parsers =
in
>> the future, should the need arises (it already kinda does in
>> treesit-node-at and treesit-simple-imenu, technically global-parser =
should
>> be picked from non-local parsers).
>=20
> I can't find a better function signature than to add the argument ONLY
> with the following combinations of symbols for its list, wrt existing =
uses
> of treesit-parsers-at:
>=20
>  nil                treesit-language-at (nil returns all parsers)
>  (local)            treesit-local-parsers-at
>  (primary global)   treesit-node-at, treesit-simple-imenu
>  (global local)     treesit-outline-level, treesit-up-list
>=20
>> We should also update manual sections that mentions =
treesit-language-at.
>=20
> Will do when everything is ok with this patch:
>=20

Thanks! LGTM.

Yuan=




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#77256; Package emacs. Full text available.

Message received at 77256 <at> debbugs.gnu.org:


Received: (at 77256) by debbugs.gnu.org; 1 Apr 2025 16:55:47 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Tue Apr 01 12:55:47 2025
Received: from localhost ([127.0.0.1]:50761 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tzett-0001gl-Hv
	for submit <at> debbugs.gnu.org; Tue, 01 Apr 2025 12:55:47 -0400
Received: from relay8-d.mail.gandi.net ([2001:4b98:dc4:8::228]:48033)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <juri@HIDDEN>) id 1tzetq-0001fD-JB
 for 77256 <at> debbugs.gnu.org; Tue, 01 Apr 2025 12:55:43 -0400
Received: by mail.gandi.net (Postfix) with ESMTPSA id DE25444440;
 Tue,  1 Apr 2025 16:55:29 +0000 (UTC)
From: Juri Linkov <juri@HIDDEN>
To: Yuan Fu <casouri@HIDDEN>
Subject: Re: bug#77256: Treesit language-at-point
In-Reply-To: <73DD04E6-D914-4A14-8DA9-876519BBEF4D@HIDDEN>
Organization: LINKOV.NET
References: <87a5990x6t.fsf@HIDDEN>
 <9271D97C-0820-4C70-9A58-88988F2E6BF9@HIDDEN>
 <87plhxw7fh.fsf@HIDDEN>
 <73DD04E6-D914-4A14-8DA9-876519BBEF4D@HIDDEN>
Date: Tue, 01 Apr 2025 19:53:37 +0300
Message-ID: <871pucdti6.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-GND-State: clean
X-GND-Score: -100
X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddukeeffedvucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesmhdtreertderjeenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpedtiedthfehhfetteefgfefgffftedttdefjeefveekfefgteettddutefgleethfenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepfedprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhm
X-GND-Sasl: juri@HIDDEN
X-Spam-Score: -0.0 (/)
X-Debbugs-Envelope-To: 77256
Cc: 77256 <at> debbugs.gnu.org, Vincenzo Pupillo <v.pupillo@HIDDEN>
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

--=-=-=
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

>> @@ -185,15 +185,15 @@ treesit-language-at
>> This function assumes that parser ranges are up-to-date.  It
>> returns the return value of `treesit-language-at-point-function'
>> if it's non-nil, otherwise it returns the language of the first
>> -parser in `treesit-parser-list', or nil if there is no parser.
>> +parser from `treesit-parsers-at', or the primary parser.
>
> If we handle the fallback case in treesit-language-at directly, rather than
> defining a separate default function, IMO we should describe the fallback
> behavior as the default. So something like “Return the language at
> POS. When there are multiple parsers that covers POS, determine the most
> relevant parser (hence language) by their embed level. If
> treesit-language-at-point-function is non-nil, return the return value of
> that function instead.” (Not saying we should use this exact docstring but
> to illustrate the point.)

Now fixed in the next patch.

>> +(defun treesit-parsers-at (&optional pos language with-host local-only)
>> +  "Return all the non-primary parsers at POS.
>
> I get why you used treesit-parsers-at rather than a more technically
> correct name like treesit-non-primary-parsers-at, or
> treesit-embeded-parsers-at. But this is confusing for not much benefit
> IMO. I suggest either use treesit-parsers-at, and add an optional argument
> exclude-primary; or use a more correct name and don’t include the primary
> parser. Also, instead of using LOCAL-ONLY, we might be more future-prove to
> use a ONLY parameter, and let user pass ‘local to mean local-only. This way
> we can add the option of returning only non-local non-primary parsers in
> the future, should the need arises (it already kinda does in
> treesit-node-at and treesit-simple-imenu, technically global-parser should
> be picked from non-local parsers).

I can't find a better function signature than to add the argument ONLY
with the following combinations of symbols for its list, wrt existing uses
of treesit-parsers-at:

  nil                treesit-language-at (nil returns all parsers)
  (local)            treesit-local-parsers-at
  (primary global)   treesit-node-at, treesit-simple-imenu
  (global local)     treesit-outline-level, treesit-up-list

> We should also update manual sections that mentions treesit-language-at.

Will do when everything is ok with this patch:


--=-=-=
Content-Type: text/x-diff
Content-Disposition: inline; filename=treesit-parsers-at.patch

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 54c29326df2..4ba46e5b7ce 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -182,18 +182,14 @@ treesit-language-at-point-function
 (defun treesit-language-at (position)
   "Return the language at POSITION.
 
-This function assumes that parser ranges are up-to-date.  It
-returns the return value of `treesit-language-at-point-function'
-if it's non-nil, otherwise it returns the language of the first
-parser in `treesit-parser-list', or nil if there is no parser.
-
-In a multi-language buffer, make sure
-`treesit-language-at-point-function' is implemented!  Otherwise
-`treesit-language-at' wouldn't return the correct result."
+When there are multiple parsers that covers POSITION, determine
+the most relevant parser (hence language) by their embed level.
+If `treesit-language-at-point-function' is non-nil, return
+the return value of that function instead."
   (if treesit-language-at-point-function
       (funcall treesit-language-at-point-function position)
-    (when-let* ((parser (car (treesit-parser-list))))
-      (treesit-parser-language parser))))
+    (treesit-parser-language
+     (car (treesit-parsers-at position)))))
 
 ;;; Node API supplement
 
@@ -247,8 +243,9 @@ treesit-node-at
                 (parser-or-lang
                  (let* ((local-parser (car (treesit-local-parsers-at
                                             pos parser-or-lang)))
-                        (global-parser (car (treesit-parser-list
-                                             nil parser-or-lang)))
+                        (global-parser (car (treesit-parsers-at
+                                             pos parser-or-lang nil
+                                             '(primary global))))
                         (parser (or local-parser global-parser)))
                    (when parser
                      (treesit-parser-root-node parser))))
@@ -267,13 +264,10 @@ treesit-node-at
                         (local-parser
                          ;; Find the local parser with highest
                          ;; embed-level at point.
-                         (car (seq-sort-by #'treesit-parser-embed-level
-                                           (lambda (a b)
-                                             (> (or a 0) (or b 0)))
-                                           (treesit-local-parsers-at
-                                            pos lang))))
-                        (global-parser (car (treesit-parser-list
-                                             nil lang)))
+                         (car (treesit-local-parsers-at pos lang)))
+                        (global-parser (car (treesit-parsers-at
+                                             pos lang nil
+                                             '(primary global))))
                         (parser (or local-parser global-parser)))
                    (when parser
                      (treesit-parser-root-node parser))))))
@@ -851,6 +845,56 @@ treesit--clip-ranges
            if (<= start (car range) (cdr range) end)
            collect range))
 
+(defun treesit-parsers-at (&optional pos language with-host only)
+  "Return all parsers at POS.
+
+POS defaults to point.  The returned parsers are sorted by
+the decreasing embed level.
+
+If LANGUAGE is non-nil, only return parsers for LANGUAGE.
+
+If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER)
+instead.  HOST-PARSER is the host parser which created the PARSER.
+
+If ONLY is nil, return all parsers including the primary parser.
+
+The argument ONLY can be a list of symbols that specify what
+parsers to include in the return value.
+
+If ONLY contains the symbol `local', include local parsers.
+Local parsers are those which only parse a limited region marked
+by an overlay with non-nil `treesit-parser' property.
+
+If ONLY contains the symbol `global', include non-local parsers
+excluding the primary parser.
+
+If ONLY contains the symbol `primary', include the primary parser."
+  (let ((res nil))
+    ;; Refer to (ref:local-parser-overlay) for more explanation of local
+    ;; parser overlays.
+    (dolist (ov (overlays-at (or pos (point))))
+      (when-let* ((parser (overlay-get ov 'treesit-parser))
+                  (host-parser (or (null with-host)
+                                   (overlay-get ov 'treesit-host-parser)))
+                  (_ (or (null language)
+                         (eq (treesit-parser-language parser)
+                             language)))
+                  (_ (or (null only)
+                         (and (memq 'local only) (memq 'global only))
+                         (and (memq 'local only)
+                              (overlay-get ov 'treesit-parser-local-p))
+                         (and (memq 'global only)
+                              (not (overlay-get ov 'treesit-parser-local-p))))))
+        (push (if with-host (cons parser host-parser) parser) res)))
+    (when (or (null only) (memq 'primary only))
+      (setq res (cons treesit-primary-parser res)))
+    (seq-sort-by (lambda (p)
+                   (treesit-parser-embed-level
+                    (or (car-safe p) p)))
+                 (lambda (a b)
+                   (> (or a 0) (or b 0)))
+                 res)))
+
 (defun treesit-local-parsers-at (&optional pos language with-host)
   "Return all the local parsers at POS.
 
@@ -862,19 +906,7 @@ treesit-local-parsers-at
 If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER)
 instead.  HOST-PARSER is the host parser which created the local
 PARSER."
-  (let ((res nil))
-    ;; Refer to (ref:local-parser-overlay) for more explanation of local
-    ;; parser overlays.
-    (dolist (ov (overlays-at (or pos (point))))
-      (let ((parser (overlay-get ov 'treesit-parser))
-            (host-parser (overlay-get ov 'treesit-host-parser))
-            (local-p (overlay-get ov 'treesit-parser-local-p)))
-        (when (and parser host-parser local-p
-                   (or (null language)
-                       (eq (treesit-parser-language parser)
-                           language)))
-          (push (if with-host (cons parser host-parser) parser) res))))
-    (nreverse res)))
+  (treesit-parsers-at pos language with-host '(local)))
 
 (defun treesit-local-parsers-on (&optional beg end language with-host)
   "Return the list of local parsers that cover the region between BEG and END.
@@ -3135,9 +3173,7 @@ treesit-up-list
           (setq parent (treesit-parent-until parent pred)))
 
         (unless parent
-          (let ((parsers (seq-keep (lambda (o)
-                                     (overlay-get o 'treesit-host-parser))
-                                   (overlays-at (point) t))))
+          (let ((parsers (mapcar #'cdr (treesit-parsers-at (point) nil t '(global local)))))
             (while (and (not parent) parsers)
               (setq parent (treesit-parent-until
                             (treesit-node-at (point) (car parsers)) pred)
@@ -3887,9 +3943,8 @@ treesit-simple-imenu
       (lambda (entry)
         (let* ((lang (car entry))
                (settings (cdr entry))
-               (global-parser (car (treesit-parser-list nil lang)))
-               (local-parsers
-                (treesit-parser-list nil lang 'embedded)))
+               (global-parser (car (treesit-parsers-at nil lang nil '(primary global))))
+               (local-parsers (treesit-local-parsers-at nil lang)))
           (cons (treesit-language-display-name lang)
                 ;; No one says you can't have both global and local
                 ;; parsers for the same language.  E.g., Rust uses
@@ -4029,9 +4087,7 @@ treesit-outline-level
       (setq level (1+ level)))
 
     ;; Continue counting the host nodes.
-    (dolist (parser (seq-keep (lambda (o)
-                                (overlay-get o 'treesit-host-parser))
-                              (overlays-at (point) t)))
+    (dolist (parser (mapcar #'cdr (treesit-parsers-at (point) nil t '(global local))))
       (let* ((node (treesit-node-at (point) parser))
              (lang (treesit-parser-language parser))
              (pred (alist-get lang treesit-aggregated-outline-predicate)))

--=-=-=--




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#77256; Package emacs. Full text available.

Message received at 77256 <at> debbugs.gnu.org:


Received: (at 77256) by debbugs.gnu.org; 1 Apr 2025 00:27:44 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Mon Mar 31 20:27:44 2025
Received: from localhost ([127.0.0.1]:43570 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tzPTi-0006Zq-Cc
	for submit <at> debbugs.gnu.org; Mon, 31 Mar 2025 20:27:44 -0400
Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]:54364)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <casouri@HIDDEN>) id 1tzPTf-0006Yc-2L
 for 77256 <at> debbugs.gnu.org; Mon, 31 Mar 2025 20:27:40 -0400
Received: by mail-pl1-x62e.google.com with SMTP id
 d9443c01a7336-2295d78b45cso3729955ad.0
 for <77256 <at> debbugs.gnu.org>; Mon, 31 Mar 2025 17:27:39 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1743467253; x=1744072053; darn=debbugs.gnu.org;
 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=brq+Pb+/MwrPSdOVi0p6fHfy6DtUWfBsxybcULKimYc=;
 b=JFHPyUfUYRCjAAgwCozUtsP6nnjO+oDhqqYRhNA4ck0dGiiqYW9ZXWIa4jCtGkZ//B
 EPdJ2Lkt4ohXp2lAa14+5x+bnfDItjcRQQeHcIEYNudgaSi/lIAe3TNHX4QlL4p4YWcK
 3FxCLlgfIIwl/iCii/DmuCYxbJvhF1bSDBimAdM6pszN2lfHDacDEdos99Xv45ourCzS
 P5D6tR11IQ6KhS5ka49rBsUSUQZLpcBFM44B53k6pUxzj0EIsM9Oh8iSbtVbzPovHQpb
 ZflhBtYAagHH+KpBiJG2IzwOmlAJlMsCzyGv+Mj+gooKd56XImFVBOr5RbC4PXzrgCuX
 nucg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1743467253; x=1744072053;
 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=brq+Pb+/MwrPSdOVi0p6fHfy6DtUWfBsxybcULKimYc=;
 b=dQvM7PuHm/XqAT+DvDsHcW9nA//mX2bW6SloLXlz5VUyW9PxlfzQ6m1ueSbeiZPxdJ
 UHITsvivRa+EJn1wX4QrvPWYsc9RcTLEXABDqOeBCaQPIOV7Q7ZohMZe12k8wNKQQTz3
 W46i1XOI0kY0ogFpunnbq6g4exxTnosnRtY/+lFW7cMsMZyhZxvZRYCL263sYI/rp0lZ
 yjwxax3YPNltRrwANQmdS+3pRE3NUW+waU6QEpL7MhFTS7HA/fJaoIOSXuqDObp5qPhb
 hqrO4rmYg16i9XRxvECIWUX9NQ5FAQ1P48agpAvKMc2HX1IUGSGA+88P/4K5XkV/P08E
 nbKQ==
X-Gm-Message-State: AOJu0YyDZGuNO6sek92m0PuwAW5WotnNdTcnaWYfJsebRMI6VxysK9lT
 p2aSM9kMqwUr4wBiBcUjpBpAX4po7jgqDo7ER/HNFTFjSg1Fcjqq
X-Gm-Gg: ASbGncsmzGny8KSo/cjKd40sd2RUkPvWqGh3qsq98ZjnFd65JoBYSpXqhy+vVF7tkHZ
 FmY0w0h4O7e8g36H/dDnspJH/IDlT8QHUvOzksktiCiVA0YT0CWeuYhx2Fg49uS1233kW+sT58e
 nDEDBL4FLYuhFnaW+w7DxXkb0LqsQwEtrd6nTHcLjuq+27MZ9MgPXggcz0AiD64Qm331ZKzHOdL
 84LeVV+7ftpRoUjgMlbLdWk+Q9BxklH4pzwDNzGC+u/bUza3e1Fi8RCEi1KAR114njfO2FdEfdT
 hhdoejH+xqWFfSkRMnjufUZUht1norprpEGj7zNRSL1g1WYob9/MOseMm9WUGbS3E/i6WzwLvnc
 tKQ==
X-Google-Smtp-Source: AGHT+IED0JzlkIMztFDzYN1VM/p0erbcF/mqsB6sk/rRxaUIvTt3EG3lelvNherw/fmLWpj+qf8oiA==
X-Received: by 2002:a05:6a21:9214:b0:1f5:7873:304b with SMTP id
 adf61e73a8af0-2009f75b1f2mr18224964637.26.1743467252543; 
 Mon, 31 Mar 2025 17:27:32 -0700 (PDT)
Received: from smtpclient.apple ([2601:646:8f81:6120:7c76:5133:615:6915])
 by smtp.gmail.com with ESMTPSA id
 41be03b00d2f7-af93ba0df86sm6951517a12.71.2025.03.31.17.27.31
 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128);
 Mon, 31 Mar 2025 17:27:31 -0700 (PDT)
Content-Type: text/plain;
	charset=utf-8
Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\))
Subject: Re: bug#77256: Treesit language-at-point
From: Yuan Fu <casouri@HIDDEN>
In-Reply-To: <87plhxw7fh.fsf@HIDDEN>
Date: Mon, 31 Mar 2025 17:27:20 -0700
Content-Transfer-Encoding: quoted-printable
Message-Id: <73DD04E6-D914-4A14-8DA9-876519BBEF4D@HIDDEN>
References: <87a5990x6t.fsf@HIDDEN>
 <9271D97C-0820-4C70-9A58-88988F2E6BF9@HIDDEN>
 <87plhxw7fh.fsf@HIDDEN>
To: Juri Linkov <juri@HIDDEN>
X-Mailer: Apple Mail (2.3826.400.131.1.6)
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 77256
Cc: 77256 <at> debbugs.gnu.org, Vincenzo Pupillo <v.pupillo@HIDDEN>
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)



> On Mar 31, 2025, at 9:53=E2=80=AFAM, Juri Linkov <juri@HIDDEN> =
wrote:
>=20
>>> Do we still need such complicated functions as
>>> mhtml-ts-mode--language-at-point, js--treesit-language-at-point, =
etc.
>>> that duplicate the rules from 'treesit-range-rules'
>>> when now the default language-at-point function could be implemented
>>> just as
>>>=20
>>> (treesit-parser-language
>>>  (or (seq-some (lambda (o) (overlay-get o 'treesit-parser))
>>>                (overlays-at (point) t))
>>>      treesit-primary-parser))
>>=20
>> Yeah, we can provide a default language-at-point function now that
>> determines the =E2=80=9Cmost relevant parser=E2=80=9D by embed level. =
But we should keep
>> treesit-language-at-point-function because a) it=E2=80=99s already in =
a release
>> version and b) we want major modes to be able to customize what =
parser to
>> pick at any given point.
>=20
> Ok, so here is a complete patch:

Thanks for working on this!

>=20
> diff --git a/lisp/textmodes/mhtml-ts-mode.el =
b/lisp/textmodes/mhtml-ts-mode.el
> index 22c0455a4ee..fdb286fc8c4 100644
> --- a/lisp/textmodes/mhtml-ts-mode.el
> +++ b/lisp/textmodes/mhtml-ts-mode.el
> @@ -221,21 +222,6 @@ mhtml-ts-mode-menu
>   "Menu bar for `mhtml-ts-mode'."
>   css-mode--menu)
>=20
> -;; To enable some basic treesiter functionality, you should define
> -;; a function that recognizes which grammar is used at-point.
> -;; This function should be assigned to =
`treesit-language-at-point-function'
> -(defun mhtml-ts-mode--language-at-point (point)
> -  "Return the language at POINT assuming the point is within a HTML =
buffer."
> -  (let* ((node (treesit-node-at point 'html))
> -         (parent (treesit-node-parent node))
> -         (node-query (format "(%s (%s))"
> -                             (treesit-node-type parent)
> -                             (treesit-node-type node))))
> -    (cond
> -     ((equal "(script_element (raw_text))" node-query) =
(js--treesit-language-at-point point))
> -     ((equal "(style_element (raw_text))" node-query) 'css)
> -     (t 'html))))
> -
> ;; Custom font-lock function that's used to apply color to css color
> ;; The signature of the function should be conforming to signature
> ;; QUERY-SPEC required by `treesit-font-lock-rules'.
> @@ -448,7 +434,7 @@ mhtml-ts-mode-flymake-mhtml
>=20
> ;;;###autoload
> (define-derived-mode mhtml-ts-mode html-ts-mode
> -  '("HTML+" (:eval (let ((lang (mhtml-ts-mode--language-at-point =
(point))))
> +  '("HTML+" (:eval (let ((lang (treesit-language-at (point))))
>                      (cond ((eq lang 'html) "")
>                            ((eq lang 'javascript) "JS")
>                            ((eq lang 'css) "CSS")))))
> @@ -527,10 +520,6 @@ mhtml-ts-mode
>       (setq-local c-ts-common--comment-regexp
>                   js--treesit-jsdoc-comment-regexp))
>=20
> -
> -    ;; Many treesit functions need to know the language at-point.
> -    ;; So you should define such a function.
> -    (setq-local treesit-language-at-point-function =
#'mhtml-ts-mode--language-at-point)
>     (setq-local prettify-symbols-alist =
mhtml-ts-mode--prettify-symbols-alist)
>=20
>     ;; Indent.
> diff --git a/lisp/treesit.el b/lisp/treesit.el
> index 54c29326df2..75c04912216 100644
> --- a/lisp/treesit.el
> +++ b/lisp/treesit.el
> @@ -185,15 +185,15 @@ treesit-language-at
> This function assumes that parser ranges are up-to-date.  It
> returns the return value of `treesit-language-at-point-function'
> if it's non-nil, otherwise it returns the language of the first
> -parser in `treesit-parser-list', or nil if there is no parser.
> +parser from `treesit-parsers-at', or the primary parser.

If we handle the fallback case in treesit-language-at directly, rather =
than defining a separate default function, IMO we should describe the =
fallback behavior as the default. So something like =E2=80=9CReturn the =
language at POS. When there are multiple parsers that covers POS, =
determine the most relevant parser (hence language) by their embed =
level. If treesit-language-at-point-function is non-nil, return the =
return value of that function instead.=E2=80=9D (Not saying we should =
use this exact docstring but to illustrate the point.)

>=20
> -In a multi-language buffer, make sure
> -`treesit-language-at-point-function' is implemented!  Otherwise
> -`treesit-language-at' wouldn't return the correct result."
> +In a multi-language buffer, optionally you can implement
> +`treesit-language-at-point-function' to return more correct result."
>   (if treesit-language-at-point-function
>       (funcall treesit-language-at-point-function position)
> -    (when-let* ((parser (car (treesit-parser-list))))
> -      (treesit-parser-language parser))))
> +    (treesit-parser-language
> +     (or (car (treesit-parsers-at position))
> +         treesit-primary-parser))))
>=20
> ;;; Node API supplement
>=20
> @@ -247,8 +247,9 @@ treesit-node-at
>                 (parser-or-lang
>                  (let* ((local-parser (car (treesit-local-parsers-at
>                                             pos parser-or-lang)))
> -                        (global-parser (car (treesit-parser-list
> -                                             nil parser-or-lang)))
> +                        (global-parser (or (car (treesit-parsers-at
> +                                                 pos parser-or-lang))
> +                                           treesit-primary-parser))
>                         (parser (or local-parser global-parser)))
>                    (when parser
>                      (treesit-parser-root-node parser))))
> @@ -267,13 +268,10 @@ treesit-node-at
>                         (local-parser
>                          ;; Find the local parser with highest
>                          ;; embed-level at point.
> -                         (car (seq-sort-by =
#'treesit-parser-embed-level
> -                                           (lambda (a b)
> -                                             (> (or a 0) (or b 0)))
> -                                           (treesit-local-parsers-at
> -                                            pos lang))))
> -                        (global-parser (car (treesit-parser-list
> -                                             nil lang)))
> +                         (car (treesit-local-parsers-at pos lang)))
> +                        (global-parser (or (car (treesit-parsers-at
> +                                                 pos lang))
> +                                           treesit-primary-parser))
>                         (parser (or local-parser global-parser)))
>                    (when parser
>                      (treesit-parser-root-node parser))))))
> @@ -851,6 +849,38 @@ treesit--clip-ranges
>            if (<=3D start (car range) (cdr range) end)
>            collect range))
>=20
> +(defun treesit-parsers-at (&optional pos language with-host =
local-only)
> +  "Return all the non-primary parsers at POS.

I get why you used treesit-parsers-at rather than a more technically =
correct name like treesit-non-primary-parsers-at, or =
treesit-embeded-parsers-at. But this is confusing for not much benefit =
IMO. I suggest either use treesit-parsers-at, and add an optional =
argument exclude-primary; or use a more correct name and don=E2=80=99t =
include the primary parser. Also, instead of using LOCAL-ONLY, we might =
be more future-prove to use a ONLY parameter, and let user pass =E2=80=98l=
ocal to mean local-only. This way we can add the option of returning =
only non-local non-primary parsers in the future, should the need arises =
(it already kinda does in treesit-node-at and treesit-simple-imenu, =
technically global-parser should be picked from non-local parsers).

> +
> +POS defaults to point.
> +If LANGUAGE is non-nil, only return parsers for LANGUAGE.
> +
> +If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER)
> +instead.  HOST-PARSER is the host parser which created the PARSER.
> +
> +If LOCAL-ONLY is non-nil, return only local parsers.
> +Local parsers are those which only parse a limited region marked
> +by an overlay with non-nil `treesit-parser' property."
> +  (let ((res nil))
> +    ;; Refer to (ref:local-parser-overlay) for more explanation of =
local
> +    ;; parser overlays.
> +    (dolist (ov (overlays-at (or pos (point))))
> +      (when-let* ((parser (overlay-get ov 'treesit-parser))
> +                  (host-parser (or (null with-host)
> +                                   (overlay-get ov =
'treesit-host-parser)))
> +                  (_ (or (null local-only)
> +                         (overlay-get ov 'treesit-parser-local-p)))
> +                  (_ (or (null language)
> +                         (eq (treesit-parser-language parser)
> +                             language))))
> +        (push (if with-host (cons parser host-parser) parser) res)))
> +    (seq-sort-by (lambda (p)
> +                   (treesit-parser-embed-level
> +                    (or (car-safe p) p)))
> +                 (lambda (a b)
> +                   (> (or a 0) (or b 0)))
> +                 res)))
> +
> (defun treesit-local-parsers-at (&optional pos language with-host)
>   "Return all the local parsers at POS.
>=20
> @@ -862,19 +892,7 @@ treesit-local-parsers-at
> If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER)
> instead.  HOST-PARSER is the host parser which created the local
> PARSER."
> -  (let ((res nil))
> -    ;; Refer to (ref:local-parser-overlay) for more explanation of =
local
> -    ;; parser overlays.
> -    (dolist (ov (overlays-at (or pos (point))))
> -      (let ((parser (overlay-get ov 'treesit-parser))
> -            (host-parser (overlay-get ov 'treesit-host-parser))
> -            (local-p (overlay-get ov 'treesit-parser-local-p)))
> -        (when (and parser host-parser local-p
> -                   (or (null language)
> -                       (eq (treesit-parser-language parser)
> -                           language)))
> -          (push (if with-host (cons parser host-parser) parser) =
res))))
> -    (nreverse res)))
> +  (treesit-parsers-at pos language with-host t))
>=20
> (defun treesit-local-parsers-on (&optional beg end language with-host)
>   "Return the list of local parsers that cover the region between BEG =
and END.
> @@ -3135,9 +3159,7 @@ treesit-up-list
>           (setq parent (treesit-parent-until parent pred)))
>=20
>         (unless parent
> -          (let ((parsers (seq-keep (lambda (o)
> -                                     (overlay-get o =
'treesit-host-parser))
> -                                   (overlays-at (point) t))))
> +          (let ((parsers (mapcar #'cdr (treesit-parsers-at (point) =
nil t))))
>             (while (and (not parent) parsers)
>               (setq parent (treesit-parent-until
>                             (treesit-node-at (point) (car parsers)) =
pred)
> @@ -3887,9 +3929,9 @@ treesit-simple-imenu
>       (lambda (entry)
>         (let* ((lang (car entry))
>                (settings (cdr entry))
> -               (global-parser (car (treesit-parser-list nil lang)))
> -               (local-parsers
> -                (treesit-parser-list nil lang 'embedded)))
> +               (global-parser (or (car (treesit-parsers-at nil lang))
> +                                  treesit-primary-parser))
> +               (local-parsers (treesit-local-parsers-at nil lang)))
>           (cons (treesit-language-display-name lang)
>                 ;; No one says you can't have both global and local
>                 ;; parsers for the same language.  E.g., Rust uses
> @@ -4029,9 +4074,7 @@ treesit-outline-level
>       (setq level (1+ level)))
>=20
>     ;; Continue counting the host nodes.
> -    (dolist (parser (seq-keep (lambda (o)
> -                                (overlay-get o 'treesit-host-parser))
> -                              (overlays-at (point) t)))
> +    (dolist (parser (mapcar #'cdr (treesit-parsers-at (point) nil =
t)))
>       (let* ((node (treesit-node-at (point) parser))
>              (lang (treesit-parser-language parser))
>              (pred (alist-get lang =
treesit-aggregated-outline-predicate)))

We should also update manual sections that mentions treesit-language-at.

Yuan





Information forwarded to bug-gnu-emacs@HIDDEN:
bug#77256; Package emacs. Full text available.

Message received at 77256 <at> debbugs.gnu.org:


Received: (at 77256) by debbugs.gnu.org; 31 Mar 2025 17:04:44 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Mon Mar 31 13:04:44 2025
Received: from localhost ([127.0.0.1]:42664 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tzIZ1-00007C-DN
	for submit <at> debbugs.gnu.org; Mon, 31 Mar 2025 13:04:44 -0400
Received: from relay5-d.mail.gandi.net ([217.70.183.197]:51633)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <juri@HIDDEN>) id 1tzIYy-00006B-MC
 for 77256 <at> debbugs.gnu.org; Mon, 31 Mar 2025 13:04:41 -0400
Received: by mail.gandi.net (Postfix) with ESMTPSA id 578514433C;
 Mon, 31 Mar 2025 17:04:31 +0000 (UTC)
From: Juri Linkov <juri@HIDDEN>
To: Yuan Fu <casouri@HIDDEN>
Subject: Re: bug#77256: Treesit language-at-point
In-Reply-To: <9271D97C-0820-4C70-9A58-88988F2E6BF9@HIDDEN>
Organization: LINKOV.NET
References: <87a5990x6t.fsf@HIDDEN>
 <9271D97C-0820-4C70-9A58-88988F2E6BF9@HIDDEN>
Date: Mon, 31 Mar 2025 19:53:14 +0300
Message-ID: <87plhxw7fh.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-GND-State: clean
X-GND-Score: -100
X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddukedtgeejucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesmhdtreertderjeenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpedtiedthfehhfetteefgfefgffftedttdefjeefveekfefgteettddutefgleethfenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepfedprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhm
X-GND-Sasl: juri@HIDDEN
X-Spam-Score: -0.7 (/)
X-Debbugs-Envelope-To: 77256
Cc: 77256 <at> debbugs.gnu.org, Vincenzo Pupillo <v.pupillo@HIDDEN>
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.7 (-)

--=-=-=
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

>> Do we still need such complicated functions as
>> mhtml-ts-mode--language-at-point, js--treesit-language-at-point, etc.
>> that duplicate the rules from 'treesit-range-rules'
>> when now the default language-at-point function could be implemented
>> just as
>> 
>>  (treesit-parser-language
>>   (or (seq-some (lambda (o) (overlay-get o 'treesit-parser))
>>                 (overlays-at (point) t))
>>       treesit-primary-parser))
>
> Yeah, we can provide a default language-at-point function now that
> determines the “most relevant parser” by embed level. But we should keep
> treesit-language-at-point-function because a) it’s already in a release
> version and b) we want major modes to be able to customize what parser to
> pick at any given point.

Ok, so here is a complete patch:


--=-=-=
Content-Type: text/x-diff
Content-Disposition: inline; filename=treesit-parsers-at.patch

diff --git a/lisp/textmodes/mhtml-ts-mode.el b/lisp/textmodes/mhtml-ts-mode.el
index 22c0455a4ee..fdb286fc8c4 100644
--- a/lisp/textmodes/mhtml-ts-mode.el
+++ b/lisp/textmodes/mhtml-ts-mode.el
@@ -221,21 +222,6 @@ mhtml-ts-mode-menu
   "Menu bar for `mhtml-ts-mode'."
   css-mode--menu)
 
-;; To enable some basic treesiter functionality, you should define
-;; a function that recognizes which grammar is used at-point.
-;; This function should be assigned to `treesit-language-at-point-function'
-(defun mhtml-ts-mode--language-at-point (point)
-  "Return the language at POINT assuming the point is within a HTML buffer."
-  (let* ((node (treesit-node-at point 'html))
-         (parent (treesit-node-parent node))
-         (node-query (format "(%s (%s))"
-                             (treesit-node-type parent)
-                             (treesit-node-type node))))
-    (cond
-     ((equal "(script_element (raw_text))" node-query) (js--treesit-language-at-point point))
-     ((equal "(style_element (raw_text))" node-query) 'css)
-     (t 'html))))
-
 ;; Custom font-lock function that's used to apply color to css color
 ;; The signature of the function should be conforming to signature
 ;; QUERY-SPEC required by `treesit-font-lock-rules'.
@@ -448,7 +434,7 @@ mhtml-ts-mode-flymake-mhtml
 
 ;;;###autoload
 (define-derived-mode mhtml-ts-mode html-ts-mode
-  '("HTML+" (:eval (let ((lang (mhtml-ts-mode--language-at-point (point))))
+  '("HTML+" (:eval (let ((lang (treesit-language-at (point))))
                      (cond ((eq lang 'html) "")
                            ((eq lang 'javascript) "JS")
                            ((eq lang 'css) "CSS")))))
@@ -527,10 +520,6 @@ mhtml-ts-mode
       (setq-local c-ts-common--comment-regexp
                   js--treesit-jsdoc-comment-regexp))
 
-
-    ;; Many treesit functions need to know the language at-point.
-    ;; So you should define such a function.
-    (setq-local treesit-language-at-point-function #'mhtml-ts-mode--language-at-point)
     (setq-local prettify-symbols-alist mhtml-ts-mode--prettify-symbols-alist)
 
     ;; Indent.
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 54c29326df2..75c04912216 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -185,15 +185,15 @@ treesit-language-at
 This function assumes that parser ranges are up-to-date.  It
 returns the return value of `treesit-language-at-point-function'
 if it's non-nil, otherwise it returns the language of the first
-parser in `treesit-parser-list', or nil if there is no parser.
+parser from `treesit-parsers-at', or the primary parser.
 
-In a multi-language buffer, make sure
-`treesit-language-at-point-function' is implemented!  Otherwise
-`treesit-language-at' wouldn't return the correct result."
+In a multi-language buffer, optionally you can implement
+`treesit-language-at-point-function' to return more correct result."
   (if treesit-language-at-point-function
       (funcall treesit-language-at-point-function position)
-    (when-let* ((parser (car (treesit-parser-list))))
-      (treesit-parser-language parser))))
+    (treesit-parser-language
+     (or (car (treesit-parsers-at position))
+         treesit-primary-parser))))
 
 ;;; Node API supplement
 
@@ -247,8 +247,9 @@ treesit-node-at
                 (parser-or-lang
                  (let* ((local-parser (car (treesit-local-parsers-at
                                             pos parser-or-lang)))
-                        (global-parser (car (treesit-parser-list
-                                             nil parser-or-lang)))
+                        (global-parser (or (car (treesit-parsers-at
+                                                 pos parser-or-lang))
+                                           treesit-primary-parser))
                         (parser (or local-parser global-parser)))
                    (when parser
                      (treesit-parser-root-node parser))))
@@ -267,13 +268,10 @@ treesit-node-at
                         (local-parser
                          ;; Find the local parser with highest
                          ;; embed-level at point.
-                         (car (seq-sort-by #'treesit-parser-embed-level
-                                           (lambda (a b)
-                                             (> (or a 0) (or b 0)))
-                                           (treesit-local-parsers-at
-                                            pos lang))))
-                        (global-parser (car (treesit-parser-list
-                                             nil lang)))
+                         (car (treesit-local-parsers-at pos lang)))
+                        (global-parser (or (car (treesit-parsers-at
+                                                 pos lang))
+                                           treesit-primary-parser))
                         (parser (or local-parser global-parser)))
                    (when parser
                      (treesit-parser-root-node parser))))))
@@ -851,6 +849,38 @@ treesit--clip-ranges
            if (<= start (car range) (cdr range) end)
            collect range))
 
+(defun treesit-parsers-at (&optional pos language with-host local-only)
+  "Return all the non-primary parsers at POS.
+
+POS defaults to point.
+If LANGUAGE is non-nil, only return parsers for LANGUAGE.
+
+If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER)
+instead.  HOST-PARSER is the host parser which created the PARSER.
+
+If LOCAL-ONLY is non-nil, return only local parsers.
+Local parsers are those which only parse a limited region marked
+by an overlay with non-nil `treesit-parser' property."
+  (let ((res nil))
+    ;; Refer to (ref:local-parser-overlay) for more explanation of local
+    ;; parser overlays.
+    (dolist (ov (overlays-at (or pos (point))))
+      (when-let* ((parser (overlay-get ov 'treesit-parser))
+                  (host-parser (or (null with-host)
+                                   (overlay-get ov 'treesit-host-parser)))
+                  (_ (or (null local-only)
+                         (overlay-get ov 'treesit-parser-local-p)))
+                  (_ (or (null language)
+                         (eq (treesit-parser-language parser)
+                             language))))
+        (push (if with-host (cons parser host-parser) parser) res)))
+    (seq-sort-by (lambda (p)
+                   (treesit-parser-embed-level
+                    (or (car-safe p) p)))
+                 (lambda (a b)
+                   (> (or a 0) (or b 0)))
+                 res)))
+
 (defun treesit-local-parsers-at (&optional pos language with-host)
   "Return all the local parsers at POS.
 
@@ -862,19 +892,7 @@ treesit-local-parsers-at
 If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER)
 instead.  HOST-PARSER is the host parser which created the local
 PARSER."
-  (let ((res nil))
-    ;; Refer to (ref:local-parser-overlay) for more explanation of local
-    ;; parser overlays.
-    (dolist (ov (overlays-at (or pos (point))))
-      (let ((parser (overlay-get ov 'treesit-parser))
-            (host-parser (overlay-get ov 'treesit-host-parser))
-            (local-p (overlay-get ov 'treesit-parser-local-p)))
-        (when (and parser host-parser local-p
-                   (or (null language)
-                       (eq (treesit-parser-language parser)
-                           language)))
-          (push (if with-host (cons parser host-parser) parser) res))))
-    (nreverse res)))
+  (treesit-parsers-at pos language with-host t))
 
 (defun treesit-local-parsers-on (&optional beg end language with-host)
   "Return the list of local parsers that cover the region between BEG and END.
@@ -3135,9 +3159,7 @@ treesit-up-list
           (setq parent (treesit-parent-until parent pred)))
 
         (unless parent
-          (let ((parsers (seq-keep (lambda (o)
-                                     (overlay-get o 'treesit-host-parser))
-                                   (overlays-at (point) t))))
+          (let ((parsers (mapcar #'cdr (treesit-parsers-at (point) nil t))))
             (while (and (not parent) parsers)
               (setq parent (treesit-parent-until
                             (treesit-node-at (point) (car parsers)) pred)
@@ -3887,9 +3929,9 @@ treesit-simple-imenu
       (lambda (entry)
         (let* ((lang (car entry))
                (settings (cdr entry))
-               (global-parser (car (treesit-parser-list nil lang)))
-               (local-parsers
-                (treesit-parser-list nil lang 'embedded)))
+               (global-parser (or (car (treesit-parsers-at nil lang))
+                                  treesit-primary-parser))
+               (local-parsers (treesit-local-parsers-at nil lang)))
           (cons (treesit-language-display-name lang)
                 ;; No one says you can't have both global and local
                 ;; parsers for the same language.  E.g., Rust uses
@@ -4029,9 +4074,7 @@ treesit-outline-level
       (setq level (1+ level)))
 
     ;; Continue counting the host nodes.
-    (dolist (parser (seq-keep (lambda (o)
-                                (overlay-get o 'treesit-host-parser))
-                              (overlays-at (point) t)))
+    (dolist (parser (mapcar #'cdr (treesit-parsers-at (point) nil t)))
       (let* ((node (treesit-node-at (point) parser))
              (lang (treesit-parser-language parser))
              (pred (alist-get lang treesit-aggregated-outline-predicate)))

--=-=-=--




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#77256; Package emacs. Full text available.

Message received at 77256 <at> debbugs.gnu.org:


Received: (at 77256) by debbugs.gnu.org; 29 Mar 2025 08:26:05 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sat Mar 29 04:26:05 2025
Received: from localhost ([127.0.0.1]:56688 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tyRW0-00012K-NW
	for submit <at> debbugs.gnu.org; Sat, 29 Mar 2025 04:26:05 -0400
Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]:55451)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <casouri@HIDDEN>) id 1tyRVz-00011h-48
 for 77256 <at> debbugs.gnu.org; Sat, 29 Mar 2025 04:26:03 -0400
Received: by mail-pj1-x102f.google.com with SMTP id
 98e67ed59e1d1-301e05b90caso5189232a91.2
 for <77256 <at> debbugs.gnu.org>; Sat, 29 Mar 2025 01:26:03 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1743236757; x=1743841557; darn=debbugs.gnu.org;
 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=dc3H3BQYLJt/SYMfyz0rpgpmWhLw32naL4Dc0knb6GM=;
 b=NAmFz6T0klCWANlfDKk4j4Vl6SflSrhgMZwj0QwFh4zGX7s0CQ3pt6+Jw5BPOgDtGz
 wLK9Dwg6MYwv3k53u8Cq9PDxVisrcNq937EP97aYa5xC7V0yeBKor2U+rbrWh59Fsh/u
 eUXaXzY9uiK/6NE0KSq7gBqFkt2NCsy0xweA4UGUqCCUZVimFLbaUceMBL5EbNePbWhy
 UgaRKAbd5erG4T4pBtxNIMgL4PGDAd/gUzTrJQ08BPMCxET3pVFS0+0F/rC3QzXQN2ie
 lUdHcSEdPPGsSF9CYAA9goADtspuk9OrlTKvSL/BrgWBLxjQWDIbJbhwuyabPcxwSPl+
 wRfA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1743236757; x=1743841557;
 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=dc3H3BQYLJt/SYMfyz0rpgpmWhLw32naL4Dc0knb6GM=;
 b=t/eTIxlMcalRtP5nxdF/ISeDArwtihp+IfrbdIP3Oph5eg9Ja2nGHIU+nVLMSJqcal
 NYfi+CrjlYITlXnSBJPdwTElKDMJpumsl7MlVGHWocL1IsHQZK6xmvdzqd5HfhqRkoPp
 CqQZFgax2CtzHq7PGPhc2a3G3TQYh1yAtUmKIzQDyIiQasjdodfYY5EFARrhRN8hVgE9
 TPrtx6O4ZKHHhuHn8H0MLh1o9nHa2BFmxY25l4GaUFGJ1JFp5z3VAEkOICtTgL62UFON
 ZQzf80LXMlrNpwwh09goYHuoO8rZMTBgrIuuL6YaxsOYErwzhgnf0VuBZg1vhWDCUZEd
 p0Dg==
X-Gm-Message-State: AOJu0YyY9aOpqyM4W2XO+n3OCdcSovr+XaGVtI2s5MQkdYA79M9tQlN4
 68HttSLUNK8y0ZM7amxTSn2SEzkWoKU9hb6iXZaCR/uHc4vZOQl9
X-Gm-Gg: ASbGncsCwkCc86GkC6xhbCuziVJfQD9fbg7jSDHAUKhQLcTHT8eyjp8B3npsOX8x4m5
 OqrND1dcW8uQLONjQTNC/vroqAMmwxrrRk+3cMc+0Ne7FjnIMjFBkdHu6CMQtRSh+g4xFUaKTFn
 08LCh+nz2/J4/IKfGc7Rw0z5Q6UzVqvNuH7CjCZF2RrovMQ69FswomEWT620EuCcRFzFEd1JKi0
 3JRKUVSxeuAWTDQwYsCEYUoo40wrty+ho6juMY2eBWAgKWjAOAHc/092ZbpB+QXewTNoXxRrFpa
 RIoChxqFU2dcg8av0N8Kx6toOmjsbUkeD0lxoTa//W5ZBeoAaX/W6SQjcmN+BUkHVfBr
X-Google-Smtp-Source: AGHT+IFmF9lr2L/GrfaXGtoSRzIiLg5uHOR7SRBgpl9KQzhKiLEiPLv8cmiav9W79u0RiN7ppyLTKw==
X-Received: by 2002:a17:90b:53c3:b0:305:2d9b:f8d1 with SMTP id
 98e67ed59e1d1-30532164b3cmr3780219a91.24.1743236756733; 
 Sat, 29 Mar 2025 01:25:56 -0700 (PDT)
Received: from smtpclient.apple ([2601:646:8f81:6120:b008:a4e4:1666:e0f6])
 by smtp.gmail.com with ESMTPSA id
 98e67ed59e1d1-30516d623a5sm3188537a91.25.2025.03.29.01.25.55
 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128);
 Sat, 29 Mar 2025 01:25:56 -0700 (PDT)
Content-Type: text/plain;
	charset=utf-8
Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\))
Subject: Re: bug#77256: Treesit language-at-point
From: Yuan Fu <casouri@HIDDEN>
In-Reply-To: <87a5990x6t.fsf@HIDDEN>
Date: Sat, 29 Mar 2025 01:25:44 -0700
Content-Transfer-Encoding: quoted-printable
Message-Id: <9271D97C-0820-4C70-9A58-88988F2E6BF9@HIDDEN>
References: <87a5990x6t.fsf@HIDDEN>
To: Juri Linkov <juri@HIDDEN>
X-Mailer: Apple Mail (2.3826.400.131.1.6)
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 77256
Cc: 77256 <at> debbugs.gnu.org, Vincenzo Pupillo <v.pupillo@HIDDEN>
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)



> On Mar 25, 2025, at 11:42=E2=80=AFAM, Juri Linkov <juri@HIDDEN> =
wrote:
>=20
> Do we still need such complicated functions as
> mhtml-ts-mode--language-at-point, js--treesit-language-at-point, etc.
> that duplicate the rules from 'treesit-range-rules'
> when now the default language-at-point function could be implemented
> just as
>=20
>  (treesit-parser-language
>   (or (seq-some (lambda (o) (overlay-get o 'treesit-parser))
>                 (overlays-at (point) t))
>       treesit-primary-parser))

Yeah, we can provide a default language-at-point function now that =
determines the =E2=80=9Cmost relevant parser=E2=80=9D by embed level. =
But we should keep treesit-language-at-point-function because a) it=E2=80=99=
s already in a release version and b) we want major modes to be able to =
customize what parser to pick at any given point.

Yuan=




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#77256; Package emacs. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 25 Mar 2025 18:43:45 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Tue Mar 25 14:43:45 2025
Received: from localhost ([127.0.0.1]:39995 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tx9FY-0003TX-Pq
	for submit <at> debbugs.gnu.org; Tue, 25 Mar 2025 14:43:45 -0400
Received: from lists.gnu.org ([2001:470:142::17]:56234)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <juri@HIDDEN>) id 1tx9FV-0003TA-Qd
 for submit <at> debbugs.gnu.org; Tue, 25 Mar 2025 14:43:42 -0400
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 <juri@HIDDEN>) id 1tx9FO-0001qO-O2
 for bug-gnu-emacs@HIDDEN; Tue, 25 Mar 2025 14:43:34 -0400
Received: from relay2-d.mail.gandi.net ([2001:4b98:dc4:8::222])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <juri@HIDDEN>) id 1tx9FL-0001ja-F1
 for bug-gnu-emacs@HIDDEN; Tue, 25 Mar 2025 14:43:34 -0400
Received: by mail.gandi.net (Postfix) with ESMTPSA id B5304441C0
 for <bug-gnu-emacs@HIDDEN>; Tue, 25 Mar 2025 18:43:23 +0000 (UTC)
From: Juri Linkov <juri@HIDDEN>
To: bug-gnu-emacs@HIDDEN
Subject: Treesit language-at-point
Organization: LINKOV.NET
X-Debbugs-Cc: Yuan Fu <casouri@HIDDEN>,  Vincenzo Pupillo
 <v.pupillo@HIDDEN>
Date: Tue, 25 Mar 2025 20:42:02 +0200
Message-ID: <87a5990x6t.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu)
MIME-Version: 1.0
Content-Type: text/plain
X-GND-State: clean
X-GND-Score: 0
X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduieefgedtucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecunecujfgurhephffvufhofffkfgggtgesthdtredttdertdenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpeejgffgtddthfelteejkeejueegvdekgfdufffgtdehvddtkeetveehgfffjeeuteenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepuddprhgtphhtthhopegsuhhgqdhgnhhuqdgvmhgrtghssehgnhhurdhorhhg
X-GND-Sasl: juri@HIDDEN
Received-SPF: pass client-ip=2001:4b98:dc4:8::222;
 envelope-from=juri@HIDDEN; helo=relay2-d.mail.gandi.net
X-Spam_score_int: -18
X-Spam_score: -1.9
X-Spam_bar: -
X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001,
 SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: 0.7 (/)
X-Debbugs-Envelope-To: submit
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -0.3 (/)

Do we still need such complicated functions as
mhtml-ts-mode--language-at-point, js--treesit-language-at-point, etc.
that duplicate the rules from 'treesit-range-rules'
when now the default language-at-point function could be implemented
just as

  (treesit-parser-language
   (or (seq-some (lambda (o) (overlay-get o 'treesit-parser))
                 (overlays-at (point) t))
       treesit-primary-parser))




Acknowledgement sent to Juri Linkov <juri@HIDDEN>:
New bug report received and forwarded. Copy sent to casouri@HIDDEN, v.pupillo@HIDDEN, bug-gnu-emacs@HIDDEN. Full text available.
Report forwarded to casouri@HIDDEN, v.pupillo@HIDDEN, bug-gnu-emacs@HIDDEN:
bug#77256; Package emacs. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Fri, 4 Apr 2025 16:30:03 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.