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: Mon, 17 Jul 2023 23:24:20 +0200 Message-ID: <7580204.EvYhyI6sBW@fedora> References: <2171209.irdbgypaU6@fedora> <1861187.tdWV9SEqCh@fedora> <83h6q390sl.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart3783623.kQq0lBPeGt" Content-Transfer-Encoding: 7Bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="27340"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 64647@debbugs.gnu.org, jostein@kjonigsen.net, theo@thornhill.no To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Jul 17 23:25:21 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 1qLVia-0006vJ-VS for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 17 Jul 2023 23:25:21 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qLViL-0000kC-1N; Mon, 17 Jul 2023 17:25:05 -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 1qLViJ-0000jw-Bl for bug-gnu-emacs@gnu.org; Mon, 17 Jul 2023 17:25:03 -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 1qLViJ-0005TD-3g for bug-gnu-emacs@gnu.org; Mon, 17 Jul 2023 17:25:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qLViI-00013S-IS for bug-gnu-emacs@gnu.org; Mon, 17 Jul 2023 17:25: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: Mon, 17 Jul 2023 21:25: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.16896290734012 (code B ref 64647); Mon, 17 Jul 2023 21:25:02 +0000 Original-Received: (at 64647) by debbugs.gnu.org; 17 Jul 2023 21:24:33 +0000 Original-Received: from localhost ([127.0.0.1]:51059 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qLVho-00012e-BJ for submit@debbugs.gnu.org; Mon, 17 Jul 2023 17:24:33 -0400 Original-Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]:47584) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qLVhk-00012O-CW for 64647@debbugs.gnu.org; Mon, 17 Jul 2023 17:24:31 -0400 Original-Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-3fbc63c2e84so49691825e9.3 for <64647@debbugs.gnu.org>; Mon, 17 Jul 2023 14:24:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689629062; x=1692221062; 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=7/NPkgvRJg64lIFrhJ/mAyOMbdOlAYnQPn8FWpDZk0Q=; b=ojRGE0CdLdT8poMXh06IfpExtee9RDnzk7fKhTgHGR5f5gBi4xTs+n40JmpYddxZhe TtgI9uwegaqkc7457gB47vhYZb0ODsAE1BUfYULwRMlHnpsgx82k47jM7QkGyRDnh58o T+fHl6YuH+ckO5t6kseSUWj5mPRqs/sMUkx9X1Pp60oB/NNQ1digCxADBkwN7m5Zp2zD KSAjbbDZgrK+Rd4JEeNbFQ5CtaRYRb3TcQE22LHn7E/1lUf46Rx/e44Tp3WQoUmTHFtx x2WOhZ8Bt946yNVsHB+j6WFfufD4OdDutBb2aWHcrO7Ek3Ej5F36H0xrqPM12Q8Rz2ar Wjmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689629062; x=1692221062; 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=7/NPkgvRJg64lIFrhJ/mAyOMbdOlAYnQPn8FWpDZk0Q=; b=LjureqF93ocNI3ndcAr02HXwya/iZlSBSlfql6kC4ZVyG0PRNUkSdVMFjCR/wpeTY7 xIvvrtYF2csKKI7VrfcezHEMuAe3M5J5+/y7uTdhQ1/jv0HgF8tce7BXAD711yGOfxFm oC5T6q39CyRExAAZebivlzUzectdzfCLD6swReg7nDBKfLAR0+lbJvTgyob+T5JTV3ZY 6+dCnQdGsJnOlbd9ciwAQ7IcHahR8BKinWdBBXDucB4yIiE0FvGWt7GA6NOziI/ip2+h /W8SqwaZOZM7aBsOJm+ZZTAfMCUaeWo4TRZqtG3xM85lb4h9GPAs3fQoHxeO1ce3c7W/ O8fg== X-Gm-Message-State: ABy/qLaM5WQ8OqanhdVFKtRtklPkAjhHJVjwNw55ADZ7w0o6qO67q9sw SPhuS2lbPUuPuT+Kl5eMnAw= X-Google-Smtp-Source: APBJJlHzZYPes+5N/bIR/eO35b7ZjwPhtPl6KXtZt36SnPky1EiPbpQrj/JWc7SS94wsINbjsrhMUg== X-Received: by 2002:a05:600c:364a:b0:3f6:2ae:230e with SMTP id y10-20020a05600c364a00b003f602ae230emr409124wmq.3.1689629061748; Mon, 17 Jul 2023 14:24:21 -0700 (PDT) Original-Received: from fedora.localnet (2-230-139-124.ip202.fastwebnet.it. [2.230.139.124]) by smtp.gmail.com with ESMTPSA id r1-20020adfdc81000000b0031412b685d2sm471917wrj.32.2023.07.17.14.24.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 14:24:21 -0700 (PDT) In-Reply-To: <83h6q390sl.fsf@gnu.org> 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:265415 Archived-At: This is a multi-part message in MIME format. --nextPart3783623.kQq0lBPeGt Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Hi, In data domenica 16 luglio 2023 20:19:38 CEST, Eli Zaretskii ha scritto: > > From: Vincenzo Pupillo > > Cc: 64647@debbugs.gnu.org, jostein@kjonigsen.net > > Date: Sun, 16 Jul 2023 20:00:43 +0200 > > > > In my patch for java-ts-mode I used treesit-query-capture to figure out > > whether a symbol was defined or not. Check out the > > java-ts-mode--string-highlight- helper function. > > Can you do something similar in this case? That would be good enough > for Emacs 29.1. In attachment you can find the new version of the patches (similar to the patch that i made for java-ts-mode). The patches were made on the branch emacs-29. Both work with the latest grammar. The javascript version is reliable, the typescrypt version seems reliable. In fact, drum roll, with typescrypt both tests: 1. (treesit-query-capture 'typescript '((member_expression) @capture)) ;; the new node type 2. (treesit-query-capture 'typescript '((nested_identifier) @capture)) ;; the old node type both return nil !!! If you use #2, then treesitter-ts-mode font lock gives an error! This happens only for font-lock, while for indentation everything works correctly. No problem for javascript, it works as expected for font-lock. The old node type returns an error with treesit-query-capture. @Theo: I am not sure if the indentation works if I add the new rules to treesit-simple-indent-rules in the Init function. I am writing a major-mode for php and, unless I am mistaken or due to problems with the various treesitter parsers, the order seems to be important. P.S. I opened two issues (one moth ago) for tree-sitter-php because it flags variables with names in non-Western characters as errors. tree-sitter-html, after they rewrote the parse from C++ to C, it crashed when used in conjunction with other parsers. Sorry for the length of this email (and for my English) Vincenzo --nextPart3783623.kQq0lBPeGt 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 c6a93b510378756f2eff01a11ef4f9127a5e5d17 Mon Sep 17 00:00:00 2001 From: Vincenzo Pupillo Date: Mon, 17 Jul 2023 22:20:44 +0200 Subject: [PATCH] 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--treesit-indent-helper): indent helper function for handle different tree-sitter-javascript version * lisp/progmodes/js.el: (js--treesit-indent-rules): use the new function * lisp/progmodes/js.el: (js--treesit-font-lock-helper): font lock helper function for handle different tree-sitter-javascript version * lisp/progmodes/js.el: (js--treesit-font-lock-settings): use the new function --- lisp/progmodes/js.el | 65 ++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 17 deletions(-) diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index a05bd758dbc..f5158195500 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--treesit-indent-helper () + "Indent rules helper, for handle different release 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,9 @@ 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) + ;; ((match "<" "jsx_fragment") parent 0) + ;; ((parent-is "jsx_fragment") parent js-indent-level) + (js--treesit-indent-helper) ((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 +3503,36 @@ This function is intended for use in `after-change-functions'." "&&" "||" "!") "JavaScript operators for tree-sitter font-locking.") +(defun js--treesit-font-lock-helper () + "Font lock rules helper, for handle different release 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 +3642,9 @@ 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--treesit-font-lock-helper) + '((jsx_attribute (property_identifier) @font-lock-constant-face))) :language 'javascript :feature 'number -- 2.41.0 --nextPart3783623.kQq0lBPeGt Content-Disposition: attachment; filename="0002-Updated-JSX-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-JSX-support-due-to-changes-in-tree-sitter-ty.patch" >From 263c9f0eca3a7df7cb29306297d32358f0e6537c Mon Sep 17 00:00:00 2001 From: Vincenzo Pupillo Date: Mon, 17 Jul 2023 22:32:13 +0200 Subject: [PATCH] Updated JSX support due to changes in tree-sitter-typescript A recent change in tree-sitter-typescript grammar support for JSX (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: (typescript-ts-mode--indent-helper): indent helper function for handle different tree-sitter-javascript version * lisp/progmodes/typescript-ts-mode.el: (typescript-ts-mode--indent-rules): use the new function * lisp/progmodes/typescript-ts-mode.el: (typescript-ts-mode--font-lock-helper): font lock helper function for handle different tree-sitter-javascript version * lisp/progmodes/typescript-ts-mode.el: (typescript-ts-mode--font-lock-settings): use the new function --- lisp/progmodes/typescript-ts-mode.el | 64 +++++++++++++++++++++------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index 5df34de0472..2de7587e43a 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 typescript-ts-mode--indent-helper () + "Indent rules helper, for handle different release of tree-sitter-typescript. +Check if a node type is available, then return the right indent rules." + ;; handle commit b893426 + (condition-case nil + (progn (treesit-query-capture 'javascript '((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,8 +122,7 @@ 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) + `((typescript-ts-mode--indent-helper) ((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) @@ -142,6 +153,39 @@ Argument LANGUAGE is either `typescript' or `tsx'." "&&" "||" "!" "?.") "TypeScript operators for tree-sitter font-locking.") +(defun typescript-ts-mode--font-lock-helper () + "Font lock rules helper, for handle different release of tree-sitter-typescript. +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 'typescript '((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 +337,9 @@ 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 + (typescript-ts-mode--font-lock-helper) + `((jsx_attribute (property_identifier) @typescript-ts-jsx-attribute-face))) :language language :feature 'number -- 2.41.0 --nextPart3783623.kQq0lBPeGt--