Skip to content

[Rust] Add support for shebang and frontmatter #4217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions Rust/Rust.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ file_extensions:

first_line_match: |-
(?xi:
^ \s* // .*? -\*- .*? \brust\b .*? -\*- # editorconfig
^ \#! .* {{shebang_language}} # shebang
| ^ \s* // .*? -\*- .*? \brust\b .*? -\*- # editorconfig
)

variables:
shebang_language: \b(?:cargo|rust)\b
non_raw_ident: '[[:alpha:]][_[:alnum:]]*|_[_[:alnum:]]+'
# include a word boundary at the end to ensure all possible characters are
# consumed, to prevent catastrophic backtracking
Expand Down Expand Up @@ -91,7 +93,9 @@ variables:
)
contexts:
main:
- include: statements
- meta_include_prototype: false
- match: ''
push: [statements, frontmatter, shebang]

prototype:
# Macro metavariables. Ideally we would do this as a with_prototype,
Expand All @@ -101,6 +105,40 @@ contexts:
- match: '\${{identifier}}'
scope: variable.other.rust

shebang:
- meta_include_prototype: false
# allow extending syntaxes like Markdown to ignore indentation restrictions
- match: \s*(\#!)
captures:
1: punctuation.definition.comment.rust
set: shebang-body
- match: ^|(?=\S) # Note: Ensure to highlight shebang if Rust is embedded.
pop: 1

shebang-body:
- meta_include_prototype: false
- meta_scope: comment.line.shebang.rust
- match: '{{shebang_language}}'
scope: constant.language.shebang.rust
- match: $\n?
pop: 1

frontmatter:
- meta_include_prototype: false
- match: \s*(---)\s*$\n?
captures:
0: meta.frontmatter.rust
1: punctuation.section.frontmatter.begin.rust
embed: scope:source.toml
embed_scope: meta.frontmatter.rust source.toml.embedded.rust
escape: ^\s*(---)\s*$\n?
escape_captures:
0: meta.frontmatter.rust
1: punctuation.section.frontmatter.end.rust
pop: 1
- match: ^|(?=\S) # Note: Ensure to highlight shebang if Rust is embedded.
pop: 1

statements:
# This isn't really just "statements", it also includes all types,
# expressions, items, etc.
Expand Down
27 changes: 27 additions & 0 deletions Rust/tests/syntax_test_frontmatter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#! SYNTAX TEST "Packages/Markdown/Markdown.sublime-syntax"

1. shebang

```rust
#!/usr/bin/env cargo
#! <- markup.list.numbered.markdown markup.raw.code-fence.rust.markdown-gfm source.rust comment.line.shebang.rust punctuation.definition.comment.rust
#!^^^^^^^^^^^^^^^^^^^ markup.list.numbered.markdown markup.raw.code-fence.rust.markdown-gfm source.rust comment.line.shebang.rust - punctuation
#! ^^^^^ constant.language.shebang.rust
```

2. frontmatter

```rust
---
#! <- markup.list.numbered.markdown markup.raw.code-fence.rust.markdown-gfm source.rust meta.frontmatter.rust punctuation.section.frontmatter.begin.rust
[section]
#! <- markup.list.numbered.markdown markup.raw.code-fence.rust.markdown-gfm source.rust meta.frontmatter.rust source.toml.embedded.rust source.toml meta.section.toml meta.brackets.toml punctuation.section.brackets.begin.toml
key = "value"
#! <- markup.list.numbered.markdown markup.raw.code-fence.rust.markdown-gfm source.rust meta.frontmatter.rust source.toml.embedded.rust meta.mapping.key.toml meta.string.toml string.unquoted.toml
---
#! <- markup.list.numbered.markdown markup.raw.code-fence.rust.markdown-gfm source.rust meta.frontmatter.rust punctuation.section.frontmatter.end.rust

fn main() {}
#! <- markup.list.numbered.markdown markup.raw.code-fence.rust.markdown-gfm source.rust meta.function.rust keyword.declaration.function.rust
#!^^^^^^^^^^ source.rust meta.function.rust
```
17 changes: 17 additions & 0 deletions Rust/tests/syntax_test_frontmatter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#! SYNTAX TEST "Rust.sublime-syntax"
---
#! <- meta.frontmatter.rust punctuation.section.frontmatter.begin.rust
#!^ meta.frontmatter.rust punctuation.section.frontmatter.begin.rust
#! ^ meta.frontmatter.rust - punctuation
[section]
#! <- meta.frontmatter.rust source.toml.embedded.rust source.toml meta.section.toml meta.brackets.toml punctuation.section.brackets.begin.toml
key = "value"
#! <- meta.frontmatter.rust source.toml.embedded.rust meta.mapping.key.toml meta.string.toml string.unquoted.toml
---
#! <- meta.frontmatter.rust punctuation.section.frontmatter.end.rust
#!^ meta.frontmatter.rust punctuation.section.frontmatter.end.rust
#! ^ meta.frontmatter.rust - punctuation

fn main() {}
#! <- meta.function.rust keyword.declaration.function.rust
#!^^^^^^^^^^ meta.function.rust
4 changes: 4 additions & 0 deletions Rust/tests/syntax_test_shebang.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! SYNTAX TEST "Rust.sublime-syntax"
#! <- comment.line.shebang.rust punctuation.definition.comment.rust
#! <- comment.line.shebang.rust punctuation.definition.comment.rust
#!^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.shebang.rust - punctuation