unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Augustin Chéneau" <btuin@mailo.com>
To: Dmitry Gutov <dmitry@gutov.dev>
Cc: 65470@debbugs.gnu.org
Subject: bug#65470: 29.1.50; js-ts-mode: regex pattern can cause incorrect parenthesis matching
Date: Fri, 25 Aug 2023 09:17:07 +0200	[thread overview]
Message-ID: <96820db7-cd69-43eb-81d6-3822312cd928@mailo.com> (raw)
In-Reply-To: <3f955b21-1394-a7bd-ed35-064d8731e751@gutov.dev>

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

Le 25/08/2023 à 02:18, Dmitry Gutov a écrit :
> On 24/08/2023 22:47, Theodor Thornhill wrote:
>> Eli Zaretskii<eliz@gnu.org>  writes:
>>
>>>> Date: Wed, 23 Aug 2023 16:23:13 +0300
>>>> From: Dmitry Gutov<dmitry@gutov.dev>
>>>>
>>>> On 23/08/2023 12:05, Augustin Chéneau (BTuin) wrote:
>>>>> With the mode js-ts-mode, matching tokens (such as '()', '[]') can be
>>>>> incorrectly paired by `show-paren-mode`. This is trivially 
>>>>> reproducible
>>>>> with this simple example:
>>>>>
>>>>> (/foobar)/)
>>>>>
>>>>> The first parenthesis is matched with the second one, which is 
>>>>> inside a
>>>>> regular expression pattern (between slashes), and the last one is not
>>>>> paired.
>>>>>
>>>>> The behavior should be the same as for string, the content of the 
>>>>> regex
>>>>> pattern should have no influence on the structure of the code. The 
>>>>> first
>>>>> parenthesis should match with the third one. Here, the first 
>>>>> parenthesis
>>>>> is matched with the last one:
>>>>>
>>>>> ("foobar)")
>>>>>
>>>>> js-mode behaves correctly in both cases.
>>>> Sounds like js-ts-mode also needs a syntax-propertize-function, similar
>>>> to c-ts-mode, ruby-ts-mode and rust-ts-mode.
>>>>
>>>> Others (typescript-ts-mode?) probably need it as well, at least modes
>>>> for those languages that have dedicated regexp or heredoc syntax.
>>> Would someone please add syntax-propertize-function in modes that need
>>> it?  I think this should be done on the emacs-29 branch.
>>>
>>> TIA
>> I'll add it to my list, but if someone will grab it that's fine, as I'm
>> a little short on time the next couple of weeks 🙁
> 
> This one seems to work for js-ts-mode.
> 
> typescript is a bit more fiddly (two separate modes, one with jsx and 
> one without), but should be able to follow the similar pattern.

Thanks for the patch, but I still have some issues. I believe that the
START and END arguments of `put-text-property` are off by one. The
attached modified patch seems to work better.

[-- Attachment #2: js-ts--syntax-propertize-v2.diff --]
[-- Type: text/x-patch, Size: 1745 bytes --]

diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index 9d2990e7bc9..a74f67d4466 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3829,6 +3829,7 @@ js-ts-mode
                 (append "{}():;,<>/" electric-indent-chars)) ;FIXME: js2-mode adds "[]*".
     (setq-local electric-layout-rules
 	        '((?\; . after) (?\{ . after) (?\} . before)))
+    (setq-local syntax-propertize-function #'js-ts--syntax-propertize)
 
     ;; Tree-sitter setup.
     (treesit-parser-create 'javascript)
@@ -3864,6 +3865,26 @@ js-ts-mode
     (add-to-list 'auto-mode-alist
                  '("\\(\\.js[mx]\\|\\.har\\)\\'" . js-ts-mode))))
 
+(defvar js-ts--s-p-query
+  (when (treesit-available-p)
+    (treesit-query-compile 'javascript
+                           '(((regex pattern: (regex_pattern) @regexp))
+                             ((variable_declarator value: (jsx_element) @jsx))
+                             ((assignment_expression right: (jsx_element) @jsx))))))
+
+(defun js-ts--syntax-propertize (beg end)
+  (let ((captures (treesit-query-capture 'javascript js-ts--s-p-query beg end)))
+    (pcase-dolist (`(,name . ,node) captures)
+      (let ((syntax (pcase-exhaustive name
+                      ('regexp
+                       (string-to-syntax "\"/"))
+                      ('jsx
+                       (string-to-syntax "|")))))
+        (put-text-property (1- (treesit-node-start node)) (treesit-node-start node)
+                           'syntax-table syntax)
+        (put-text-property (treesit-node-end node) (1+ (treesit-node-end node))
+                           'syntax-table syntax)))))
+
 ;;;###autoload
 (define-derived-mode js-json-mode js-mode "JSON"
   (setq-local js-enabled-frameworks nil)

  parent reply	other threads:[~2023-08-25  7:17 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 [this message]
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
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=96820db7-cd69-43eb-81d6-3822312cd928@mailo.com \
    --to=btuin@mailo.com \
    --cc=65470@debbugs.gnu.org \
    --cc=dmitry@gutov.dev \
    /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).