diff options
| author | Jostein Kjønigsen | 2023-09-05 21:29:27 +0200 |
|---|---|---|
| committer | Dmitry Gutov | 2023-09-13 01:54:45 +0300 |
| commit | 1fb2fb501f34d7c08462209a798e65904ea7b8da (patch) | |
| tree | fe985a69c2173f95445a352e97668d9980ba610a | |
| parent | 946b395e7e1cd3685ca094531d8dbd1b89b0a7e6 (diff) | |
| download | emacs-1fb2fb501f34d7c08462209a798e65904ea7b8da.tar.gz emacs-1fb2fb501f34d7c08462209a798e65904ea7b8da.zip | |
typescript-ts-mode, tsx-ts-mode: Fix syntax properties for regexp and jsx
Propertize regexps as strings and JSX elements as generic strings.
* lisp/progmodes/typescript-ts-mode.el (ts-ts--s-p-query)
(tsx-ts--s-p-query): New variables.
(ts-ts--syntax-propertize, tsx-ts--syntax-propertize)
(ts-ts--syntax-propertize-captures): New functions.
(typescript-ts-mode, tsx-ts-mode): Use them (bug#65470).
| -rw-r--r-- | lisp/progmodes/typescript-ts-mode.el | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index 96ea18523e3..57382c9cb31 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el | |||
| @@ -418,6 +418,7 @@ Argument LANGUAGE is either `typescript' or `tsx'." | |||
| 418 | (keyword string escape-sequence) | 418 | (keyword string escape-sequence) |
| 419 | (constant expression identifier number pattern property) | 419 | (constant expression identifier number pattern property) |
| 420 | (function bracket delimiter))) | 420 | (function bracket delimiter))) |
| 421 | (setq-local syntax-propertize-function #'ts-ts--syntax-propertize) | ||
| 421 | 422 | ||
| 422 | (treesit-major-mode-setup))) | 423 | (treesit-major-mode-setup))) |
| 423 | 424 | ||
| @@ -464,9 +465,47 @@ at least 3 (which is the default value)." | |||
| 464 | (keyword string escape-sequence) | 465 | (keyword string escape-sequence) |
| 465 | (constant expression identifier jsx number pattern property) | 466 | (constant expression identifier jsx number pattern property) |
| 466 | (function bracket delimiter))) | 467 | (function bracket delimiter))) |
| 468 | (setq-local syntax-propertize-function #'tsx-ts--syntax-propertize) | ||
| 467 | 469 | ||
| 468 | (treesit-major-mode-setup))) | 470 | (treesit-major-mode-setup))) |
| 469 | 471 | ||
| 472 | (defvar ts-ts--s-p-query | ||
| 473 | (when (treesit-available-p) | ||
| 474 | (treesit-query-compile 'typescript | ||
| 475 | '(((regex pattern: (regex_pattern) @regexp)))))) | ||
| 476 | |||
| 477 | (defvar tsx-ts--s-p-query | ||
| 478 | (when (treesit-available-p) | ||
| 479 | (treesit-query-compile 'tsx | ||
| 480 | '(((regex pattern: (regex_pattern) @regexp)) | ||
| 481 | ((variable_declarator value: (jsx_element) @jsx)) | ||
| 482 | ((assignment_expression right: (jsx_element) @jsx)) | ||
| 483 | ((arguments (jsx_element) @jsx)) | ||
| 484 | ((parenthesized_expression (jsx_element) @jsx)) | ||
| 485 | ((return_statement (jsx_element) @jsx)))))) | ||
| 486 | |||
| 487 | (defun ts-ts--syntax-propertize (beg end) | ||
| 488 | (let ((captures (treesit-query-capture 'typescript ts-ts--s-p-query beg end))) | ||
| 489 | (ts-ts--syntax-propertize-captures captures))) | ||
| 490 | |||
| 491 | (defun tsx-ts--syntax-propertize (beg end) | ||
| 492 | (let ((captures (treesit-query-capture 'tsx tsx-ts--s-p-query beg end))) | ||
| 493 | (ts-ts--syntax-propertize-captures captures))) | ||
| 494 | |||
| 495 | (defun ts-ts--syntax-propertize-captures (captures) | ||
| 496 | (pcase-dolist (`(,name . ,node) captures) | ||
| 497 | (let* ((ns (treesit-node-start node)) | ||
| 498 | (ne (treesit-node-end node)) | ||
| 499 | (syntax (pcase-exhaustive name | ||
| 500 | ('regexp | ||
| 501 | (cl-decf ns) | ||
| 502 | (cl-incf ne) | ||
| 503 | (string-to-syntax "\"/")) | ||
| 504 | ('jsx | ||
| 505 | (string-to-syntax "|"))))) | ||
| 506 | (put-text-property ns (1+ ns) 'syntax-table syntax) | ||
| 507 | (put-text-property (1- ne) ne 'syntax-table syntax)))) | ||
| 508 | |||
| 470 | (if (treesit-ready-p 'tsx) | 509 | (if (treesit-ready-p 'tsx) |
| 471 | (add-to-list 'auto-mode-alist '("\\.tsx\\'" . tsx-ts-mode))) | 510 | (add-to-list 'auto-mode-alist '("\\.tsx\\'" . tsx-ts-mode))) |
| 472 | 511 | ||