emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/typescript-mode b80ae97e49 3/3: Merge pull request #167 fr


From: ELPA Syncer
Subject: [nongnu] elpa/typescript-mode b80ae97e49 3/3: Merge pull request #167 from Fuco1/feature/fontify-type-guard
Date: Sun, 3 Jul 2022 06:58:55 -0400 (EDT)

branch: elpa/typescript-mode
commit b80ae97e497c80203e7360d68c5682a34f9aeb63
Merge: f2de3f4946 333a7c5f1a
Author: Jostein Kjønigsen <jostein@kjonigsen.net>
Commit: GitHub <noreply@github.com>

    Merge pull request #167 from Fuco1/feature/fontify-type-guard
    
    feat(fontlock): fontify the variable and type in typeguard
---
 typescript-mode-general-tests.el | 13 +++++++++++++
 typescript-mode.el               | 41 +++++++++++++++++++++-------------------
 2 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/typescript-mode-general-tests.el b/typescript-mode-general-tests.el
index ae3478f34d..62c4847205 100644
--- a/typescript-mode-general-tests.el
+++ b/typescript-mode-general-tests.el
@@ -506,6 +506,19 @@ function foo<Z, Y, Z & Y, Z | Y | Z, Y<X<X, Y>>>()\n"
       "type Thing = number;"
     (should (eq (get-face-at "Thing") 'font-lock-type-face))))
 
+(ert-deftest font-lock/fontify-type-guard ()
+  "The type guard syntax
+
+    var is Type
+
+should be fontified as variable, keyword and type."
+  (test-with-fontified-buffer
+      "function test(var: unknown): var is RetType {\n}"
+    (should (eq (get-face-at 30) 'font-lock-variable-name-face))
+    (should (eq (get-face-at "is") 'font-lock-keyword-face))
+    (should (eq (get-face-at "RetType") 'font-lock-type-face))))
+
+
 (ert-deftest font-lock/type-names-level4 ()
   "Typenames should be highlighted in declarations"
 
diff --git a/typescript-mode.el b/typescript-mode.el
index 830d79dc9e..2037bfd9bb 100644
--- a/typescript-mode.el
+++ b/typescript-mode.el
@@ -2013,10 +2013,13 @@ This performs fontification according to 
`typescript--class-styles'."
     ;; - private generic: SomeType<Foo>
     ;; - private genericArray: SomeType<Foo>[]
     ;; - function testFunc(): SomeType<> {
+    ;; - function testFunc(a): a is SomeType<> {
     ;; TODO: namespaced classes!
     ,(list
-      (concat ":\\s-\\(" typescript--type-name-re "\\)\\(<" 
typescript--type-name-re ">\\)?\\(\[\]\\)?\\([,;]\\)?\\s-*{?")
-      '(1 'font-lock-type-face))
+      (concat ":\\s-\\(?:\\s-*\\(" typescript--name-re 
"\\)\\s-*\\(is\\)\\s-*\\)?" "\\(" typescript--type-name-re "\\)\\(<" 
typescript--type-name-re ">\\)?\\(\[\]\\)?\\([,;]\\)?\\s-*{?")
+      '(1 'font-lock-variable-name-face nil t)
+      '(2 'font-lock-keyword-face nil t)
+      '(3 'font-lock-type-face))
 
     ;; type-casts
     ,(list
@@ -2094,7 +2097,7 @@ This performs fontification according to 
`typescript--class-styles'."
     ;; but need care to avoid affecting the // and */ comment markers.
     ("\\(?:^\\|[=([{,:;|&!]\\|\\_<return\\_>\\)\\(?:[ \t]\\)*\\(/\\)[^/*]"
      (1 (ignore
-        (forward-char -1)
+     (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.
@@ -2330,20 +2333,20 @@ the same column as the current line."
   (save-excursion
     (save-match-data
       (when (looking-at "\\s-*\\_<while\\_>")
-       (if (save-excursion
-             (skip-chars-backward "[ \t\n]*}")
-             (looking-at "[ \t\n]*}"))
-           (save-excursion
-             (backward-list) (forward-symbol -1) (looking-at "\\_<do\\_>"))
-         (typescript--re-search-backward "\\_<do\\_>" (point-at-bol) t)
-         (or (looking-at "\\_<do\\_>")
-             (let ((saved-indent (current-indentation)))
-               (while (and (typescript--re-search-backward "^\\s-*\\_<" nil t)
-                           (/= (current-indentation) saved-indent)))
-               (and (looking-at "\\s-*\\_<do\\_>")
-                    (not (typescript--re-search-forward
-                          "\\_<while\\_>" (point-at-eol) t))
-                    (= (current-indentation) saved-indent)))))))))
+    (if (save-excursion
+          (skip-chars-backward "[ \t\n]*}")
+          (looking-at "[ \t\n]*}"))
+        (save-excursion
+          (backward-list) (forward-symbol -1) (looking-at "\\_<do\\_>"))
+      (typescript--re-search-backward "\\_<do\\_>" (point-at-bol) t)
+      (or (looking-at "\\_<do\\_>")
+          (let ((saved-indent (current-indentation)))
+        (while (and (typescript--re-search-backward "^\\s-*\\_<" nil t)
+                (/= (current-indentation) saved-indent)))
+        (and (looking-at "\\s-*\\_<do\\_>")
+             (not (typescript--re-search-forward
+               "\\_<while\\_>" (point-at-eol) t))
+             (= (current-indentation) saved-indent)))))))))
 
 
 (defun typescript--ctrl-statement-indentation ()
@@ -2952,9 +2955,9 @@ Key bindings:
         comment-start-skip "\\(//+\\|/\\*+\\)\\s *")
 
   (setq-local electric-indent-chars
-             (append "{}():;," electric-indent-chars))
+          (append "{}():;," electric-indent-chars))
   (setq-local electric-layout-rules
-             '((?\; . after) (?\{ . after) (?\} . before)))
+          '((?\; . after) (?\{ . after) (?\} . before)))
 
   (let ((c-buffer-is-cc-mode t))
     ;; FIXME: These are normally set by `c-basic-common-init'.  Should



reply via email to

[Prev in Thread] Current Thread [Next in Thread]