[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/typescript-mode e700af4703 092/222: Use syntax-propertize-
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/typescript-mode e700af4703 092/222: Use syntax-propertize-function to fontify regexps. |
Date: |
Sun, 6 Feb 2022 16:59:21 -0500 (EST) |
branch: elpa/typescript-mode
commit e700af4703fca4b89cfab9e532ef2294ed3a5cc7
Author: Louis-Dominique Dubeau <ldd@lddubeau.com>
Commit: Louis-Dominique Dubeau <ldd@lddubeau.com>
Use syntax-propertize-function to fontify regexps.
The complexity of regular expressions taxes the basic facilities for
funtification. We reuse the latest js.el code that handles
fontification to fix lingering issues with how typescript-mode has
handled regexp fontification so far.
We do not simply set `syntax-propertize-function` to
`js-syntax-propertize` because the latest version of js.el shipped
with Emacs has a buggy implementation. Our choices are: copy from the
latest version of js.el, come up with some other implementation, or
accept to live with buggy fontification.
Closes #44.
---
typescript-mode.el | 89 ++++++++++++++++++++++++++++++++++++++++++------------
1 file changed, 69 insertions(+), 20 deletions(-)
diff --git a/typescript-mode.el b/typescript-mode.el
index cea2487dcb..3265cae358 100644
--- a/typescript-mode.el
+++ b/typescript-mode.el
@@ -1880,21 +1880,73 @@ This performs fontification according to
`typescript--class-styles'."
typescript--font-lock-keywords-3)
"Font lock keywords for `typescript-mode'. See `font-lock-keywords'.")
-;; XXX: typescript can continue a regexp literal across lines so long
-;; as the newline is escaped with \. Account for that in the regexp
-;; below.
-(defconst typescript--regexp-literal
- "[=(,:]\\(?:\\s-\\|\n\\)*\\(/\\)\\(?:\\\\.\\|[^/]\\)+\\(/\\)"
- "Regexp matching a typescript regular expression literal.
-Match groups 1 and 2 are the characters forming the beginning and
-end of the literal.")
-
-;; we want to match regular expressions only at the beginning of
-;; expressions
-(defconst typescript-font-lock-syntactic-keywords
- `((,typescript--regexp-literal (1 "|") (2 "|")))
- "Syntactic font lock keywords matching regexps in typescript.
-See `font-lock-keywords'.")
+;;; Propertize
+
+;;
+;; The propertize code was adapted from:
+;;
https://github.com/emacs-mirror/emacs/blob/489d6466372f488adc53897435fff290394b62f7/lisp/progmodes/js.el
+;;
+
+(defconst typescript--syntax-propertize-regexp-regexp
+ (rx
+ ;; Start of regexp.
+ "/"
+ (0+ (or
+ ;; Match characters outside of a character class.
+ (not (any ?\[ ?/ ?\\))
+ ;; Match backslash quoted characters.
+ (and "\\" not-newline)
+ ;; Match character class.
+ (and
+ "["
+ (0+ (or
+ (not (any ?\] ?\\))
+ (and "\\" not-newline)))
+ "]")))
+ (group (zero-or-one "/")))
+ "Regular expression matching a JavaScript regexp literal.")
+
+(defun typescript-syntax-propertize-regexp (end)
+ (let ((ppss (syntax-ppss)))
+ (when (eq (nth 3 ppss) ?/)
+ ;; A /.../ regexp.
+ (goto-char (nth 8 ppss))
+ (when (looking-at typescript--syntax-propertize-regexp-regexp)
+ ;; Don't touch text after END.
+ (when (> end (match-end 1))
+ (setq end (match-end 1)))
+ (put-text-property (match-beginning 1) end
+ 'syntax-table (string-to-syntax "\"/"))
+ (goto-char end)))))
+
+(defun typescript-syntax-propertize (start end)
+ ;; JavaScript allows immediate regular expression objects, written /.../.
+ (goto-char start)
+ (typescript-syntax-propertize-regexp end)
+ (funcall
+ (syntax-propertize-rules
+ ;; Distinguish /-division from /-regexp chars (and from /-comment-starter).
+ ;; FIXME: Allow regexps after infix ops like + ...
+ ;; https://developer.mozilla.org/en/JavaScript/Reference/Operators
+ ;; We can probably just add +, -, <, >, %, ^, ~, ?, : at which
+ ;; point I think only * and / would be missing which could also be added,
+ ;; but need care to avoid affecting the // and */ comment markers.
+ ("\\(?:^\\|[=([{,:;|&!]\\|\\_<return\\_>\\)\\(?:[ \t]\\)*\\(/\\)[^/*]"
+ (1 (ignore
+ (forward-char -1)
+ (when (or (not (memq (char-after (match-beginning 0)) '(?\s ?\t)))
+ ;; If the / is at the beginning of line, we have to check
+ ;; the end of the previous text.
+ (save-excursion
+ (goto-char (match-beginning 0))
+ (forward-comment (- (point)))
+ (memq (char-before)
+ (eval-when-compile (append "=({[,:;" '(nil))))))
+ (put-text-property (match-beginning 1) (match-end 1)
+ 'syntax-table (string-to-syntax "\"/"))
+ (typescript-syntax-propertize-regexp end)))))
+ ("\\`\\(#\\)!" (1 "< b")))
+ (point) end))
;;; Indentation
@@ -2457,11 +2509,8 @@ Key bindings:
(setq-local beginning-of-defun-function 'typescript-beginning-of-defun)
(setq-local end-of-defun-function 'typescript-end-of-defun)
(setq-local open-paren-in-column-0-is-defun-start nil)
- (setq-local font-lock-defaults
- (list typescript--font-lock-keywords
- nil nil nil nil
- '(font-lock-syntactic-keywords
- . typescript-font-lock-syntactic-keywords)))
+ (setq-local font-lock-defaults (list typescript--font-lock-keywords))
+ (setq-local syntax-propertize-function #'typescript-syntax-propertize)
(setq-local parse-sexp-ignore-comments t)
(setq-local parse-sexp-lookup-properties t)
- [nongnu] elpa/typescript-mode 42b366e669 153/222: Merge pull request #111 from tam5/master, (continued)
- [nongnu] elpa/typescript-mode 42b366e669 153/222: Merge pull request #111 from tam5/master, ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 2405090403 152/222: Handle CI-failures correctly., ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 895cd7f425 173/222: Fix indentation infinite looping, ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 9db704ec1f 185/222: Fix duplicate test name, ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 4832652496 163/222: Support electric-layout-mode., ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 710f5425a4 189/222: Add tests for typescript-indent-list-items option, ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 1fd835e7f8 205/222: Fix typos in README, ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 30391a518a 079/222: Fix indentation issue with conditional operators., ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 2d382d8bfb 053/222: Support rule names and severity in tslint reports., ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 83579fac6e 070/222: Remove the explicit invocation of the mode hook., ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode e700af4703 092/222: Use syntax-propertize-function to fontify regexps.,
ELPA Syncer <=
- [nongnu] elpa/typescript-mode 1d32c6b3a8 066/222: Improve readability of the code., ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 46e032e932 054/222: Fix list of values compilation mode should use., ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 56a1ea861f 082/222: Merge pull request #55 from bkruczyk/fix-regex-font-locking, ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 0353bd72f8 133/222: Add bigint, ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 18dc5a36a6 140/222: Fixes typo in the README file., ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 36105abbb1 141/222: Add 26.1 to the versions we test on., ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 08ceb01ff3 142/222: We no longer support 24.3, fixes #100., ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode f33751e98f 145/222: Fix a case of bad indentation., ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 1ec94013e7 146/222: Run emacs --version prior to running cask., ELPA Syncer, 2022/02/06
- [nongnu] elpa/typescript-mode 871c23111a 157/222: rename to typescript-mode and move to root, ELPA Syncer, 2022/02/06