unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Jostein Kjønigsen" <jostein@secure.kjonigsen.net>
To: Dmitry Gutov <dmitry@gutov.dev>
Cc: "Yuan Fu" <casouri@gmail.com>,
	65470@debbugs.gnu.org, "Theodor Thornhill" <theo@thornhill.no>,
	btuin@mailo.com, "Eli Zaretskii" <eliz@gnu.org>,
	"Jostein Kjønigsen" <jostein@kjonigsen.net>
Subject: bug#65470: 29.1.50; js-ts-mode: regex pattern can cause incorrect parenthesis matching
Date: Mon, 11 Sep 2023 21:37:33 +0200	[thread overview]
Message-ID: <ECA4978F-D5D5-4137-8050-315EE9BAE90D@secure.kjonigsen.net> (raw)
In-Reply-To: <2f525ebe-74e5-dcbe-4403-5e9ae001795c@gutov.dev>

[-- Attachment #1: Type: text/plain, Size: 1992 bytes --]



> On 7 Sep 2023, at 14:09, Dmitry Gutov <dmitry@gutov.dev> wrote:
> 
> On 05/09/2023 22:31, Jostein Kjønigsen wrote:
> > The patch so far is attached. It’s intentionally -not- optimized to leave room for TSX cases which may arise, so hopefully no need to nitpick this yet.
> 
> I was going to nitpick it, but then read the rest of your message ;-(
> 
>> However the patch for js-ts-mode has lots of … interesting stuff about jsx I have not included, for no other reason than not understanding what type of use-cases they are meant to support.
>> If someone can provide me some examples for the JSX use-cases, I can try to make room for TSX-variants of the same code.
> 
> The idea was to "enclose" every TSX in "generic string" syntax so that whatever unpaired characters are inside (such as (, ", ...), won't affect syntax-ppss status on the outside. I'm not sure how critical that is, but I guess some users might encounter such situations.
> 
> Examples like:
> 
>  ReactDOM.render(
>    <div className="">
>      <h1>Hello, Welcome to React and TypeScript ;-(</h1>
>    </div>,
>    // type closing paren here and see that it's matched to opener above
>    document.getElementById("root")
>  );
> 

Ok. That makes sense, and explains everything. I’ve tried implementing the same thing for tsx-ts-mode too in the attached patch. (You may start nitpicking now).

I first tried using a more naive and wide approach only trying to capture full (jsx_element) blocks without any further constraints.
What I found was that when having nested elements (which one tend to have all the time), the syntax-propertizing would cancel itself for every second nesting.

So to fix that, I’ve tried to anchor the query to top-level constructs which typically does not nest. Looking at the query for js.el, I can only assume that you were trying to too solve the same problem, Dmitry?

But without further ado… Here’s the patch. Do your worst, gentlemen :D


[-- Attachment #2: 0001-typescript-ts-mode-Fix-syntax-properties-for-regexp-.patch --]
[-- Type: application/octet-stream, Size: 2910 bytes --]

From 2f08456a4aa6a093cbda4359af31edcd5864a44c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jostein=20Kj=C3=B8nigsen?= <jostein@kjonigsen.net>
Date: Tue, 5 Sep 2023 21:29:27 +0200
Subject: [PATCH] typescript-ts-mode: Fix syntax properties for regexp
 expressions.

- Also handle JSX-elements in TSX-files as generic strings.
---
 lisp/progmodes/typescript-ts-mode.el | 36 ++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index 441cdc1f7aa..dd61baf5e3c 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -473,6 +473,7 @@ typescript-ts-mode
                   (keyword string escape-sequence)
                   (constant expression identifier number pattern property)
                   (function bracket delimiter)))
+    (setq-local syntax-propertize-function #'ts-ts--syntax-propertize)
 
     (treesit-major-mode-setup)))
 
@@ -529,9 +530,44 @@ tsx-ts-mode
                   (keyword string escape-sequence)
                   (constant expression identifier jsx number pattern property)
                   (function bracket delimiter)))
+    (setq-local syntax-propertize-function #'tsx-ts--syntax-propertize)
 
     (treesit-major-mode-setup)))
 
+(defvar ts-ts--s-p-query
+  (when (treesit-available-p)
+    (treesit-query-compile 'typescript
+                           '(((regex pattern: (regex_pattern) @regexp))))))
+
+(defvar tsx-ts--s-p-query
+  (when (treesit-available-p)
+    (treesit-query-compile 'tsx
+                           '(((regex pattern: (regex_pattern) @regexp))
+                             ((arguments (jsx_element) @jsx))
+                             ((parenthesized_expression (jsx_element) @jsx))))))
+
+(defun ts-ts--syntax-propertize (beg end)
+  (let ((captures (treesit-query-capture 'typescript ts-ts--s-p-query beg end)))
+    (ts-ts--syntax-propertize-captures captures)))
+
+(defun tsx-ts--syntax-propertize (beg end)
+  (let ((captures (treesit-query-capture 'tsx tsx-ts--s-p-query beg end)))
+    (ts-ts--syntax-propertize-captures captures)))
+
+(defun ts-ts--syntax-propertize-captures (captures)
+  (pcase-dolist (`(,name . ,node) captures)
+    (let* ((ns (treesit-node-start node))
+           (ne (treesit-node-end node))
+           (syntax (pcase-exhaustive name
+                     ('regexp
+                      (cl-decf ns)
+                      (cl-incf ne)
+                      (string-to-syntax "\"/"))
+                     ('jsx
+                      (string-to-syntax "|")))))
+      (put-text-property ns (1+ ns) 'syntax-table syntax)
+      (put-text-property (1- ne) ne 'syntax-table syntax))))
+
 (if (treesit-ready-p 'tsx)
     (add-to-list 'auto-mode-alist '("\\.tsx\\'" . tsx-ts-mode)))
 
-- 
2.39.2 (Apple Git-143)


[-- Attachment #3: Type: text/plain, Size: 16 bytes --]



—
Jostein

  reply	other threads:[~2023-09-11 19:37 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-23  9:05 bug#65470: 29.1.50; js-ts-mode: regex pattern can cause incorrect parenthesis matching Augustin Chéneau
2023-08-23 13:23 ` Dmitry Gutov
2023-08-24  5:59   ` Eli Zaretskii
2023-08-24 19:31     ` Augustin Chéneau
2023-08-24 19:47     ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-25  0:18       ` Dmitry Gutov
2023-08-25  5:30         ` Eli Zaretskii
2023-08-25  7:17         ` Augustin Chéneau
2023-08-26  1:52           ` Dmitry Gutov
2023-08-25  6:59       ` Jostein Kjønigsen
2023-08-25 18:27         ` Yuan Fu
2023-08-26  9:22           ` Jostein Kjønigsen
2023-08-26 15:29             ` Fu Yuan
2023-08-26 21:13               ` Jostein Kjønigsen
2023-08-26 21:45                 ` Dmitry Gutov
2023-08-31  9:41                   ` Eli Zaretskii
2023-08-31 11:15                     ` Dmitry Gutov
2023-08-31 12:53                       ` Eli Zaretskii
2023-09-01  1:42                         ` Dmitry Gutov
2023-09-05 19:31                           ` Jostein Kjønigsen
2023-09-07  8:59                             ` Eli Zaretskii
2023-09-07  9:02                               ` Stefan Kangas
2023-09-07 12:09                             ` Dmitry Gutov
2023-09-11 19:37                               ` Jostein Kjønigsen [this message]
2023-09-11 22:23                                 ` Dmitry Gutov
2023-09-12  6:29                                   ` Jostein Kjønigsen
2023-09-12 23:14                                     ` Dmitry Gutov
2023-09-15 12:11                                       ` Jostein Kjønigsen
2023-09-15 13:35                                         ` Dmitry Gutov
2023-09-16  5:54                                           ` Eli Zaretskii
2023-09-16 11:20                                             ` Eli Zaretskii
2023-09-16 11:40                                               ` Eli Zaretskii
2023-09-16 20:07                                                 ` Dmitry Gutov
2023-09-16 13:59                                             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-16 20:05                                               ` Dmitry Gutov
2023-09-17  5:22                                                 ` Eli Zaretskii
2023-09-01 15:45                       ` Augustin Chéneau
2023-09-01 15:58                         ` Eli Zaretskii
2023-09-01 19:21                         ` Dmitry Gutov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ECA4978F-D5D5-4137-8050-315EE9BAE90D@secure.kjonigsen.net \
    --to=jostein@secure.kjonigsen.net \
    --cc=65470@debbugs.gnu.org \
    --cc=btuin@mailo.com \
    --cc=casouri@gmail.com \
    --cc=dmitry@gutov.dev \
    --cc=eliz@gnu.org \
    --cc=jostein@kjonigsen.net \
    --cc=theo@thornhill.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).