Improve TypeScript highlights (#18525)

狐狸 and Marshall Bowers created

- Move function queries under constant queries to avoid uppercase
functions highlighted as constants
- Merge keywords and remove duplicates
- Highlights type aliases on import
- Highlights literal built-in types (null, undefined, true, false) as
`@type.builtin`

Confused about case-based queries, should they be rewritten?

Release Notes:

- N/A

---------

Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>

Change summary

crates/languages/src/typescript/highlights.scm | 147 +++++++++++--------
1 file changed, 84 insertions(+), 63 deletions(-)

Detailed changes

crates/languages/src/typescript/highlights.scm 🔗

@@ -2,6 +2,38 @@
 
 (identifier) @variable
 
+; Special identifiers
+
+((identifier) @type
+ (#match? @type "^[A-Z]"))
+(type_identifier) @type
+(predefined_type) @type.builtin
+
+(import_specifier
+  "type"
+  name: (identifier) @type
+  alias: (identifier) @type
+)
+
+(import_statement
+  "type"
+  (import_clause
+    (named_imports
+      (import_specifier
+        name: (identifier) @type
+        alias: (identifier) @type
+      )
+    )
+  )
+)
+
+([
+  (identifier)
+  (shorthand_property_identifier)
+  (shorthand_property_identifier_pattern)
+ ] @constant
+ (#match? @constant "^_*[A-Z_][A-Z\\d_]*$"))
+
 ; Properties
 
 (property_identifier) @property
@@ -43,23 +75,6 @@
   left: (identifier) @function
   right: [(function_expression) (arrow_function)])
 
-; Special identifiers
-
-((identifier) @constructor
- (#match? @constructor "^[A-Z]"))
-
-((identifier) @type
- (#match? @type "^[A-Z]"))
-(type_identifier) @type
-(predefined_type) @type.builtin
-
-([
-  (identifier)
-  (shorthand_property_identifier)
-  (shorthand_property_identifier_pattern)
- ] @constant
- (#match? @constant "^_*[A-Z_][A-Z\\d_]*$"))
-
 ; Literals
 
 (this) @variable.special
@@ -75,6 +90,15 @@
   (false)
 ] @boolean
 
+(literal_type
+  [
+    (null)
+    (undefined)
+    (true)
+    (false)
+  ] @type.builtin
+)
+
 (comment) @comment
 
 [
@@ -90,15 +114,6 @@
 
 ; Tokens
 
-[
-  ";"
-  "?."
-  "."
-  ","
-  ":"
-  "?"
-] @punctuation.delimiter
-
 [
   "..."
   "-"
@@ -147,6 +162,22 @@
   "??="
 ] @operator
 
+(ternary_expression
+  [
+    "?"
+    ":"
+  ] @operator
+)
+
+[
+  ";"
+  "?."
+  "."
+  ","
+  ":"
+  "?"
+] @punctuation.delimiter
+
 [
   "("
   ")"
@@ -156,14 +187,22 @@
   "}"
 ]  @punctuation.bracket
 
-(ternary_expression
-  [
-    "?"
-    ":"
-  ] @operator
-)
+(template_substitution
+  "${" @punctuation.special
+  "}" @punctuation.special) @embedded
+
+(template_type
+  "${" @punctuation.special
+  "}" @punctuation.special) @embedded
+
+(type_arguments
+  "<" @punctuation.bracket
+  ">" @punctuation.bracket)
+
+; Keywords
 
 [
+  "abstract"
   "as"
   "async"
   "await"
@@ -174,10 +213,12 @@
   "const"
   "continue"
   "debugger"
+  "declare"
   "default"
   "delete"
   "do"
   "else"
+  "enum"
   "export"
   "extends"
   "finally"
@@ -186,13 +227,23 @@
   "function"
   "get"
   "if"
+  "implements"
   "import"
   "in"
+  "infer"
   "instanceof"
+  "interface"
   "is"
+  "keyof"
   "let"
+  "namespace"
   "new"
   "of"
+  "override"
+  "private"
+  "protected"
+  "public"
+  "readonly"
   "return"
   "satisfies"
   "set"
@@ -201,6 +252,7 @@
   "target"
   "throw"
   "try"
+  "type"
   "typeof"
   "using"
   "var"
@@ -209,34 +261,3 @@
   "with"
   "yield"
 ] @keyword
-
-(template_substitution
-  "${" @punctuation.special
-  "}" @punctuation.special) @embedded
-
-(template_type
-  "${" @punctuation.special
-  "}" @punctuation.special) @embedded
-
-(type_arguments
-  "<" @punctuation.bracket
-  ">" @punctuation.bracket)
-
-; Keywords
-
-[ "abstract"
-  "declare"
-  "enum"
-  "export"
-  "implements"
-  "infer"
-  "interface"
-  "keyof"
-  "namespace"
-  "private"
-  "protected"
-  "public"
-  "type"
-  "readonly"
-  "override"
-] @keyword