Skip to content

Commit

Permalink
Improve JavaScript and TypeScript syntax highlighting (#25328)
Browse files Browse the repository at this point in the history
Release Notes:

  - Improved JavaScript and TypeScript syntax highlighting.

| Zed 0.174.6 | With this PR |
| --- | --- |
|
![Image](https://github.com/user-attachments/assets/a64f0abf-3b4e-4369-80c5-1d1381c925fc)
|
![Image](https://github.com/user-attachments/assets/f8224243-f4ab-4af9-8dd8-5062dd7ea743)
|

- `regex_flags`: `keyword.regex`, see the [Regex
PR](#25332) for other Regex
scopes
- `@`: `punctuation.special`, as in Python
- `jsx_text`: `text.jsx`
- `=`: `operator` -> `punctuation.jsx.delimiter`, `punctuation` as in
[VS
Code](https://github.com/microsoft/vscode/blob/0fe195613ed9901f669cd0f799fe807f0189d029/extensions/html/syntaxes/html.tmLanguage.json#L78)
and
[Atom](https://github.com/atom/language-html/blob/ee750a014a003c3d6f10b91e3cd5f9bfa0f051e6/grammars/tree-sitter-html.cson#L47)
- added `jsx` scope to target JSX tokens specifically

```javascript
/**
 * @Keyword comment
 */
@log
class X {
  render() {
    return (
      <div jsx_attribute="value">
        <Input onKeyPress={super.bind(this)}/>
        jsx_text
      </div>
    );
  }
}
const IDENTIFIER = true
```

---------

Co-authored-by: Marshall Bowers <[email protected]>
  • Loading branch information
chbk and maxdeviant authored Feb 21, 2025
1 parent 3e75a66 commit 8f40bcc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
21 changes: 13 additions & 8 deletions crates/languages/src/javascript/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
(escape_sequence) @string.escape

(regex) @string.regex
(regex_flags) @keyword.regex
(number) @number

; Tokens
Expand Down Expand Up @@ -210,6 +211,8 @@
"<" @punctuation.bracket
">" @punctuation.bracket)

(decorator "@" @punctuation.special)

; Keywords

[ "abstract"
Expand All @@ -229,11 +232,13 @@
] @keyword

; JSX elements
(jsx_opening_element (identifier) @tag (#match? @tag "^[a-z][^.]*$"))
(jsx_closing_element (identifier) @tag (#match? @tag "^[a-z][^.]*$"))
(jsx_self_closing_element (identifier) @tag (#match? @tag "^[a-z][^.]*$"))

(jsx_attribute (property_identifier) @attribute)
(jsx_opening_element (["<" ">"]) @punctuation.bracket)
(jsx_closing_element (["</" ">"]) @punctuation.bracket)
(jsx_self_closing_element (["<" "/>"]) @punctuation.bracket)
(jsx_opening_element (identifier) @tag.jsx (#match? @tag.jsx "^[a-z][^.]*$"))
(jsx_closing_element (identifier) @tag.jsx (#match? @tag.jsx "^[a-z][^.]*$"))
(jsx_self_closing_element (identifier) @tag.jsx (#match? @tag.jsx "^[a-z][^.]*$"))

(jsx_attribute (property_identifier) @attribute.jsx)
(jsx_opening_element (["<" ">"]) @punctuation.bracket.jsx)
(jsx_closing_element (["</" ">"]) @punctuation.bracket.jsx)
(jsx_self_closing_element (["<" "/>"]) @punctuation.bracket.jsx)
(jsx_attribute "=" @punctuation.delimiter.jsx)
(jsx_text) @text.jsx
21 changes: 13 additions & 8 deletions crates/languages/src/tsx/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
(escape_sequence) @string.escape

(regex) @string.regex
(regex_flags) @keyword.regex
(number) @number

; Tokens
Expand Down Expand Up @@ -221,6 +222,8 @@
"<" @punctuation.bracket
">" @punctuation.bracket)

(decorator "@" @punctuation.special)

; Keywords

[ "abstract"
Expand All @@ -240,11 +243,13 @@
] @keyword

; JSX elements
(jsx_opening_element (identifier) @tag (#match? @tag "^[a-z][^.]*$"))
(jsx_closing_element (identifier) @tag (#match? @tag "^[a-z][^.]*$"))
(jsx_self_closing_element (identifier) @tag (#match? @tag "^[a-z][^.]*$"))

(jsx_attribute (property_identifier) @attribute)
(jsx_opening_element (["<" ">"]) @punctuation.bracket)
(jsx_closing_element (["</" ">"]) @punctuation.bracket)
(jsx_self_closing_element (["<" "/>"]) @punctuation.bracket)
(jsx_opening_element (identifier) @tag.jsx (#match? @tag.jsx "^[a-z][^.]*$"))
(jsx_closing_element (identifier) @tag.jsx (#match? @tag.jsx "^[a-z][^.]*$"))
(jsx_self_closing_element (identifier) @tag.jsx (#match? @tag.jsx "^[a-z][^.]*$"))

(jsx_attribute (property_identifier) @attribute.jsx)
(jsx_opening_element (["<" ">"]) @punctuation.bracket.jsx)
(jsx_closing_element (["</" ">"]) @punctuation.bracket.jsx)
(jsx_self_closing_element (["<" "/>"]) @punctuation.bracket.jsx)
(jsx_attribute "=" @punctuation.delimiter.jsx)
(jsx_text) @text.jsx
3 changes: 3 additions & 0 deletions crates/languages/src/typescript/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
(escape_sequence) @string.escape

(regex) @string.regex
(regex_flags) @keyword.regex
(number) @number

; Tokens
Expand Down Expand Up @@ -199,6 +200,8 @@
"<" @punctuation.bracket
">" @punctuation.bracket)

(decorator "@" @punctuation.special)

; Keywords

[
Expand Down

0 comments on commit 8f40bcc

Please sign in to comment.