Extend syntax highlighting to TypeScript, JSX, and .mjs/.cjs - #9
Merged
Conversation
syntect's default pack covers Rust, Python, JavaScript, and CSS but not TypeScript, TSX, JSX, or the .mjs/.cjs Node.js variants — those rendered plain. Pull in `two-face` for the TS/TSX (and SCSS, etc.) grammars, and alias .mjs/.cjs/.jsx → js and .mts/.cts → ts so Node.js and JSX sources highlight via the existing JavaScript/TypeScript syntaxes.
The two-face syntax pack already covers JSON, YAML, TOML, Markdown, HTML, Bash, Go, Java, C/C++, Ruby, SQL, Kotlin, Swift, Scala, Dockerfile, GraphQL, SCSS, Vue, Svelte, Protobuf, Terraform, Lua, Dart, Elixir and many more — add a regression test that asserts every one of them highlights. Alias the two notable JSON dialects (jsonc, json5) to json so commented JSON files still render.
There was a problem hiding this comment.
Pull request overview
This PR expands the diff renderer’s syntax highlighting by switching from syntect’s default syntax pack to two-face’s extended syntax set, and by aliasing several file extensions (notably Node.js and TS variants) to syntaxes that syntect can highlight.
Changes:
- Replace
SyntaxSet::load_defaults_newlines()withtwo_face::syntax::extra_newlines()to gain additional syntaxes (e.g., TypeScript/TSX, SCSS). - Add an
alias_extension()mapping so.mjs/.cjs/.jsxhighlight as JavaScript and.mts/.ctshighlight as TypeScript (plus JSONC/JSON5 → JSON). - Add a broad test intended to validate highlighting across many extensions.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/render/highlight.rs |
Uses two-face syntaxes, introduces extension aliasing, and adds new highlighting tests. |
Cargo.toml |
Adds the two-face dependency with syntect “fancy” highlighting support. |
Cargo.lock |
Locks the new two-face transitive dependency set. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+25
to
+27
| # Extra syntect syntaxes covering languages syntect ships without — notably | ||
| # TypeScript/TSX, JSX, and modern Node.js extensions (.mjs/.cjs). | ||
| two-face = { version = "0.5", default-features = false, features = ["syntect-fancy"] } |
Comment on lines
+117
to
+125
| fn highlights_the_supported_languages() { | ||
| let h = Highlighter::new(); | ||
| // syntect's default pack covers Rust, Python, JavaScript, CSS, JSON, | ||
| // YAML, TOML, Markdown, HTML, Bash, Go, Java, C/C++, Ruby, SQL, etc. | ||
| // `two-face` adds TypeScript/TSX, SCSS, Dockerfile, GraphQL, Vue, | ||
| // Svelte, Protobuf, Terraform, Kotlin, Swift, Zig, Dart, Elixir and | ||
| // more. The remaining extensions are aliased to a sibling syntax so | ||
| // Node.js (.mjs/.cjs), JSX, and JSON-with-comments still highlight. | ||
| for (ext, line) in [ |
Comment on lines
+15
to
+17
| /// Map extensions syntect (even with `two-face`'s extras) doesn't recognise to | ||
| /// a sibling syntax — keeping JSX and modern Node.js extensions highlighted as | ||
| /// JavaScript. |
- Cargo.toml: tighten the two-face comment to only claim what the crate actually adds (TypeScript/TSX); the JSX and .mjs/.cjs cases are aliased, not provided by the syntax pack. - highlight: expand the alias_extension doc comment to cover .mts/.cts and jsonc/json5, not just JSX and Node.js. - highlight: narrow the language-coverage test to the extensions whose support this module is responsible for (ts/tsx plus the aliased ones, with rs as a baseline). The previous list reached into syntect/two-face's bundled grammars (lua, dart, elixir, svelte, …), which would have made the test brittle to dependency reshuffles.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
syntect's default pack covers Rust, Python, JavaScript, and CSS but not
TypeScript, TSX, JSX, or the .mjs/.cjs Node.js variants — those rendered
plain. Pull in
two-facefor the TS/TSX (and SCSS, etc.) grammars, andalias .mjs/.cjs/.jsx → js and .mts/.cts → ts so Node.js and JSX sources
highlight via the existing JavaScript/TypeScript syntaxes.