From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Vincenzo Pupillo Newsgroups: gmane.emacs.bugs Subject: bug#64647: treesit-query-error due to a recent change to tree-sitter-javascript grammar definition Date: Thu, 20 Jul 2023 12:14:07 +0200 Message-ID: <1994797.PYKUYFuaPT@3-191.divsi.unimi.it> References: <2171209.irdbgypaU6@fedora> <7580204.EvYhyI6sBW@fedora> <87fs5keb9y.fsf@thornhill.no> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart22007411.EfDdHjke4D" Content-Transfer-Encoding: 7Bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="40676"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 64647@debbugs.gnu.org, jostein@kjonigsen.net To: Eli Zaretskii , Theodor Thornhill Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Jul 20 12:15:23 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1qMQgt-000AOW-Ds for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 20 Jul 2023 12:15:23 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qMQga-0001ij-7q; Thu, 20 Jul 2023 06:15:04 -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 1qMQgY-0001ar-MM for bug-gnu-emacs@gnu.org; Thu, 20 Jul 2023 06:15:02 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qMQgY-0003nl-BP for bug-gnu-emacs@gnu.org; Thu, 20 Jul 2023 06:15:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qMQgY-0000yt-64 for bug-gnu-emacs@gnu.org; Thu, 20 Jul 2023 06:15:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Vincenzo Pupillo Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 20 Jul 2023 10:15:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 64647 X-GNU-PR-Package: emacs Original-Received: via spool by 64647-submit@debbugs.gnu.org id=B64647.16898480583699 (code B ref 64647); Thu, 20 Jul 2023 10:15:02 +0000 Original-Received: (at 64647) by debbugs.gnu.org; 20 Jul 2023 10:14:18 +0000 Original-Received: from localhost ([127.0.0.1]:57716 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qMQfp-0000xa-Dh for submit@debbugs.gnu.org; Thu, 20 Jul 2023 06:14:18 -0400 Original-Received: from mail-ej1-x631.google.com ([2a00:1450:4864:20::631]:61505) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qMQfn-0000wu-4Q for 64647@debbugs.gnu.org; Thu, 20 Jul 2023 06:14:15 -0400 Original-Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-992e22c09edso95044666b.2 for <64647@debbugs.gnu.org>; Thu, 20 Jul 2023 03:14:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689848049; x=1690452849; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=goykyrOjhu0vQ/ryK1OjSWFJ6jbwwIkl+YvCdFHuGsI=; b=lAc3kfwtOu4X5WR2owwzZZZoCV49sxTOx1yFlSgJSOzxhnZtjeX5C4JhZKu3oOm4e7 J5ynS71t0u1KmBEi8/xc5OZvMw7Z1yTfHM/q2PxRfb/M1/8Pc1jOf1t88DNGcM/YPste rs7L6c+VM2Ydn6Xjkwt04x9+gG852FKyUODwjkprWF6yiOVMcHRj63PgywSkMnwR2N1b FzGbjYcczVDl/skW4WnXaVyUB5W79+fJVUAMIQLdcTWLxP6yF+nBpOiaOjVtfUVnjHte 3izOdUY8KhOW6MbjPfrDrt6PaJj3CYFyezwbU+o+oKLwf1FfWoveLaUQjCMOys/H1zfG KlbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689848049; x=1690452849; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=goykyrOjhu0vQ/ryK1OjSWFJ6jbwwIkl+YvCdFHuGsI=; b=YrFA47ji3jdhWrzuOGOn8T5Yb6pBiRjp9FDHrhzBA6Y4vB1e681/xlXNLWhHmdozwO xb0/qAbjBpiFeK8YVqyevpjE9zyk2pEhlcnMitHeJZQVvYaHtY20jXwMQCD8qGChpfRZ MceRFKtiSIjldi9tJCql570GgFB4OH1diNsCuv8RXILeIiDEBjlYWSnbVem0pZ7DYg6s JeyEaMoXexYltjb/vgYchbCv3rCsvvJP0iFP+7O4yDJnEO1UGO3MsSB3cNMXmpnPKAOT ki5qLlPnIaGtHjnIFHEhDTjOqcMTbLt222MsCYF87o8q/LhsVOP6nr5zvyU9XlV32kdk wM1A== X-Gm-Message-State: ABy/qLYwo1yATP+8E3F2oD7RbxjNJ+8EkpaE8RQflNkQwMbizDf/ytdP nSxkARopN3Y4qmpGHtJK94c= X-Google-Smtp-Source: APBJJlGe1SoXDjpeWheaHuu0sjm9iTNe0t0R8rs/+yIWqIU01ZVVUibJTMC9unqHKUYHoxyMz2a8Lg== X-Received: by 2002:a17:906:51d9:b0:993:a379:6158 with SMTP id v25-20020a17090651d900b00993a3796158mr4975663ejk.17.1689848049124; Thu, 20 Jul 2023 03:14:09 -0700 (PDT) Original-Received: from 3-191.divsi.unimi.it (3-191.divsi.unimi.it. [159.149.3.191]) by smtp.gmail.com with ESMTPSA id e27-20020a170906045b00b0098e38d2e584sm486667eja.43.2023.07.20.03.14.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 03:14:08 -0700 (PDT) In-Reply-To: <87fs5keb9y.fsf@thornhill.no> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:265582 Archived-At: This is a multi-part message in MIME format. --nextPart22007411.EfDdHjke4D Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" In data mercoled=EC 19 luglio 2023 07:11:05 CEST, Theodor Thornhill ha scri= tto: > For Typescript these changes should go into 'tsx-ts-mode, not > 'typescript-ts-mode. That may be why you are seeing some strange results? No, exactly the same problem occurs, unfortunately. After all, the sources = of=20 libtree-sitter-tsx and libtree-sitter-typescript come from the same reposit= ory=20 and the content of tree-sitter-typescript/tsx/grammar.js is just that: const defineGrammar =3D require('../common/define-grammar'); module.exports =3D defineGrammar('tsx'); > > --- >=20 > "... function to handle ..." fixed > "Indent rules helper, to handle different releases of > tree-sitter-javascript." fixed >=20 > The indentation here looks off. Can you format this? >=20 fixed >=20 > This seems to not be properly converted to tsx from javascript, both in > the docstring and code. Also, I think the name is wrong. Maybe it > should describe its intent a little more closely, something like > "tsx-ts-mode--indent-compatibility-b893426"? Sorry for the error. I fixed them, tested and fixed the function names acco= rding to your instructions (also in js.el) Hope the patches are better now. Thanks. Vincenzo --nextPart22007411.EfDdHjke4D Content-Disposition: attachment; filename="0002-Updated-TSX-support-due-to-changes-in-tree-sitter-ty.patch" Content-Transfer-Encoding: 7Bit Content-Type: text/x-patch; charset="x-UTF_8J"; name="0002-Updated-TSX-support-due-to-changes-in-tree-sitter-ty.patch" >From 99ab9ca3e75f756c7fc9965528db6d882a383150 Mon Sep 17 00:00:00 2001 From: Vincenzo Pupillo Date: Thu, 20 Jul 2023 12:06:21 +0200 Subject: [PATCH 2/2] Updated TSX support due to changes in tree-sitter-typescript A recent change in tree-sitter-typescript grammar support for TSX (commit b893426), changed two things: 1. renamed nested_identifier to member_expression 2. removed jsx_fragment, jsx_text is used instead * lisp/progmodes/typescript-ts-mode.el: (tsx-ts-mode--indent-compatibility-b893426): indent helper function for handle different tree-sitter-typescript version * lisp/progmodes/typescript-ts-mode.el: (typescript-ts-mode--indent-rules): use the new function to handle both jsx_fragment and jsx_text * lisp/progmodes/typescript-ts-mode.el: (tsx-ts-mode--font-lock-compatibility-bb1f97b): font lock helper function for handle different tree-sitter-typescript version * lisp/progmodes/typescript-ts-mode.el: (typescript-ts-mode--font-lock-settings): use the new function to handle both nested_identifier and member_expression --- lisp/progmodes/typescript-ts-mode.el | 78 ++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index 5df34de0472..173ec52f209 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -75,6 +75,18 @@ table) "Syntax table for `typescript-ts-mode'.") +(defun tsx-ts-mode--indent-compatibility-b893426 () + "Indent rules helper, to handle different releases of tree-sitter-tsx. +Check if a node type is available, then return the right indent rules." + ;; handle commit b893426 + (condition-case nil + (progn (treesit-query-capture 'tsx '((jsx_fragment) @capture)) + `(((match "<" "jsx_fragment") parent 0) + ((parent-is "jsx_fragment") parent typescript-ts-mode-indent-offset))) + (error + `(((match "<" "jsx_text") parent 0) + ((parent-is "jsx_text") parent typescript-ts-mode-indent-offset))))) + (defun typescript-ts-mode--indent-rules (language) "Rules used for indentation. Argument LANGUAGE is either `typescript' or `tsx'." @@ -110,16 +122,15 @@ Argument LANGUAGE is either `typescript' or `tsx'." ((parent-is "binary_expression") parent-bol typescript-ts-mode-indent-offset) ,@(when (eq language 'tsx) - `(((match "<" "jsx_fragment") parent 0) - ((parent-is "jsx_fragment") parent typescript-ts-mode-indent-offset) - ((node-is "jsx_closing_element") parent 0) - ((match "jsx_element" "statement") parent typescript-ts-mode-indent-offset) - ((parent-is "jsx_element") parent typescript-ts-mode-indent-offset) - ((parent-is "jsx_text") parent-bol typescript-ts-mode-indent-offset) - ((parent-is "jsx_opening_element") parent typescript-ts-mode-indent-offset) - ((parent-is "jsx_expression") parent-bol typescript-ts-mode-indent-offset) - ((match "/" "jsx_self_closing_element") parent 0) - ((parent-is "jsx_self_closing_element") parent typescript-ts-mode-indent-offset))) + (append (tsx-ts-mode--indent-compatibility-b893426) + `(((node-is "jsx_closing_element") parent 0) + ((match "jsx_element" "statement") parent typescript-ts-mode-indent-offset) + ((parent-is "jsx_element") parent typescript-ts-mode-indent-offset) + ((parent-is "jsx_text") parent-bol typescript-ts-mode-indent-offset) + ((parent-is "jsx_opening_element") parent typescript-ts-mode-indent-offset) + ((parent-is "jsx_expression") parent-bol typescript-ts-mode-indent-offset) + ((match "/" "jsx_self_closing_element") parent 0) + ((parent-is "jsx_self_closing_element") parent typescript-ts-mode-indent-offset)))) ;; FIXME(Theo): This no-node catch-all should be removed. When is it needed? (no-node parent-bol 0)))) @@ -142,6 +153,38 @@ Argument LANGUAGE is either `typescript' or `tsx'." "&&" "||" "!" "?.") "TypeScript operators for tree-sitter font-locking.") +(defun tsx-ts-mode--font-lock-compatibility-bb1f97b () + "Font lock rules helper, to handle different releases of tree-sitter-tsx. +Check if a node type is available, then return the right font lock rules." + ;; handle commit bb1f97b + ;; Warning: treesitter-query-capture says both node types are valid, + ;; but then raises an error if the wrong node type is used. So it is + ;; important to check with the new node type (member_expression) + (condition-case nil + (progn (treesit-query-capture 'tsx '((member_expression) @capture)) + '((jsx_opening_element + [(member_expression (identifier)) (identifier)] + @typescript-ts-jsx-tag-face) + + (jsx_closing_element + [(member_expression (identifier)) (identifier)] + @typescript-ts-jsx-tag-face) + + (jsx_self_closing_element + [(member_expression (identifier)) (identifier)] + @typescript-ts-jsx-tag-face))) + (error '((jsx_opening_element + [(nested_identifier (identifier)) (identifier)] + @typescript-ts-jsx-tag-face) + + (jsx_closing_element + [(nested_identifier (identifier)) (identifier)] + @typescript-ts-jsx-tag-face) + + (jsx_self_closing_element + [(nested_identifier (identifier)) (identifier)] + @typescript-ts-jsx-tag-face))))) + (defun typescript-ts-mode--font-lock-settings (language) "Tree-sitter font-lock settings. Argument LANGUAGE is either `typescript' or `tsx'." @@ -293,19 +336,8 @@ Argument LANGUAGE is either `typescript' or `tsx'." :language language :feature 'jsx - `((jsx_opening_element - [(nested_identifier (identifier)) (identifier)] - @typescript-ts-jsx-tag-face) - - (jsx_closing_element - [(nested_identifier (identifier)) (identifier)] - @typescript-ts-jsx-tag-face) - - (jsx_self_closing_element - [(nested_identifier (identifier)) (identifier)] - @typescript-ts-jsx-tag-face) - - (jsx_attribute (property_identifier) @typescript-ts-jsx-attribute-face)) + (append (tsx-ts-mode--font-lock-compatibility-bb1f97b) + `((jsx_attribute (property_identifier) @typescript-ts-jsx-attribute-face))) :language language :feature 'number -- 2.41.0 --nextPart22007411.EfDdHjke4D Content-Disposition: attachment; filename="0001-Updated-JSX-support-due-to-changes-in-tree-sitter-ja.patch" Content-Transfer-Encoding: 7Bit Content-Type: text/x-patch; charset="x-UTF_8J"; name="0001-Updated-JSX-support-due-to-changes-in-tree-sitter-ja.patch" >From 3294c731f4a231ece6f1a82e5ea31dd1c0ef0c10 Mon Sep 17 00:00:00 2001 From: Vincenzo Pupillo Date: Thu, 20 Jul 2023 12:03:11 +0200 Subject: [PATCH 1/2] Updated JSX support due to changes in tree-sitter-javascript A recent change in tree-sitter-javascript grammar support for JSX (commit bb1f97b), changed two things: 1. renamed nested_identifier to member_expression 2. removed jsx_fragment, jsx_text is used instead * lisp/progmodes/js.el: (js-jsx--treesit-indent-compatibility-bb1f97b): indent helper function for handle different tree-sitter-javascript version * lisp/progmodes/js.el: (js--treesit-indent-rules): use the new function to handle both jsx_fragment and jsx_text * lisp/progmodes/js.el: (js-jsx--treesit-font-lock-compatibility-bb1f97b): font lock helper function for handle different tree-sitter-javascript version * lisp/progmodes/js.el: (js--treesit-font-lock-settings): use the new function to handle both nested_identifier and member_expression --- lisp/progmodes/js.el | 61 ++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index a05bd758dbc..70048e5d26c 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -3427,6 +3427,18 @@ This function is intended for use in `after-change-functions'." ;;; Tree sitter integration +(defun js-jsx--treesit-indent-compatibility-bb1f97b () + "Indent rules helper, to handle different releases of tree-sitter-javascript. +Check if a node type is available, then return the right indent rules." + ;; handle commit bb1f97b + (condition-case nil + (progn (treesit-query-capture 'javascript '((jsx_fragment) @capture)) + `(((match "<" "jsx_fragment") parent 0) + ((parent-is "jsx_fragment") parent js-indent-level))) + (error + `(((match "<" "jsx_text") parent 0) + ((parent-is "jsx_text") parent js-indent-level))))) + (defvar js--treesit-indent-rules (let ((switch-case (rx "switch_" (or "case" "default")))) `((javascript @@ -3462,8 +3474,7 @@ This function is intended for use in `after-change-functions'." ((parent-is "statement_block") parent-bol js-indent-level) ;; JSX - ((match "<" "jsx_fragment") parent 0) - ((parent-is "jsx_fragment") parent js-indent-level) + (js-jsx--treesit-indent-compatibility-bb1f97b) ((node-is "jsx_closing_element") parent 0) ((match "jsx_element" "statement") parent js-indent-level) ((parent-is "jsx_element") parent js-indent-level) @@ -3490,6 +3501,35 @@ This function is intended for use in `after-change-functions'." "&&" "||" "!") "JavaScript operators for tree-sitter font-locking.") +(defun js-jsx--treesit-font-lock-compatibility-bb1f97b () + "Font lock rules helper, to handle different releases of tree-sitter-javascript. +Check if a node type is available, then return the right font lock rules." + ;; handle commit bb1f97b + (condition-case nil + (progn (treesit-query-capture 'javascript '((member_expression) @capture)) + '((jsx_opening_element + [(member_expression (identifier)) (identifier)] + @font-lock-function-call-face) + + (jsx_closing_element + [(member_expression (identifier)) (identifier)] + @font-lock-function-call-face) + + (jsx_self_closing_element + [(member_expression (identifier)) (identifier)] + @font-lock-function-call-face))) + (error '((jsx_opening_element + [(nested_identifier (identifier)) (identifier)] + @font-lock-function-call-face) + + (jsx_closing_element + [(nested_identifier (identifier)) (identifier)] + @font-lock-function-call-face) + + (jsx_self_closing_element + [(nested_identifier (identifier)) (identifier)] + @font-lock-function-call-face))))) + (defvar js--treesit-font-lock-settings (treesit-font-lock-rules @@ -3599,21 +3639,8 @@ This function is intended for use in `after-change-functions'." :language 'javascript :feature 'jsx - '((jsx_opening_element - [(nested_identifier (identifier)) (identifier)] - @font-lock-function-call-face) - - (jsx_closing_element - [(nested_identifier (identifier)) (identifier)] - @font-lock-function-call-face) - - (jsx_self_closing_element - [(nested_identifier (identifier)) (identifier)] - @font-lock-function-call-face) - - (jsx_attribute - (property_identifier) - @font-lock-constant-face)) + (append (js-jsx--treesit-font-lock-compatibility-bb1f97b) + '((jsx_attribute (property_identifier) @font-lock-constant-face))) :language 'javascript :feature 'number -- 2.41.0 --nextPart22007411.EfDdHjke4D--