Skip to content

Commit 65644ef

Browse files
authored
[Rust] Add support for shebang and frontmatter (#4217)
Resolves #4164 This commit ensures to keep first_line_match and syntax highlighting sync. Push away from main is required for `embed_scope` to be correctly applied if Rust is embedded.
1 parent de74ca9 commit 65644ef

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

Rust/Rust.sublime-syntax

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ file_extensions:
99

1010
first_line_match: |-
1111
(?xi:
12-
^ \s* // .*? -\*- .*? \brust\b .*? -\*- # editorconfig
12+
^ \#! .* {{shebang_language}} # shebang
13+
| ^ \s* // .*? -\*- .*? \brust\b .*? -\*- # editorconfig
1314
)
1415
1516
variables:
17+
shebang_language: \b(?:cargo|rust)\b
1618
non_raw_ident: '[[:alpha:]][_[:alnum:]]*|_[_[:alnum:]]+'
1719
# include a word boundary at the end to ensure all possible characters are
1820
# consumed, to prevent catastrophic backtracking
@@ -91,7 +93,9 @@ variables:
9193
)
9294
contexts:
9395
main:
94-
- include: statements
96+
- meta_include_prototype: false
97+
- match: ''
98+
push: [statements, frontmatter, shebang]
9599

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

108+
shebang:
109+
- meta_include_prototype: false
110+
# allow extending syntaxes like Markdown to ignore indentation restrictions
111+
- match: \s*(\#!)
112+
captures:
113+
1: punctuation.definition.comment.rust
114+
set: shebang-body
115+
- match: ^|(?=\S) # Note: Ensure to highlight shebang if Rust is embedded.
116+
pop: 1
117+
118+
shebang-body:
119+
- meta_include_prototype: false
120+
- meta_scope: comment.line.shebang.rust
121+
- match: '{{shebang_language}}'
122+
scope: constant.language.shebang.rust
123+
- match: $\n?
124+
pop: 1
125+
126+
frontmatter:
127+
- meta_include_prototype: false
128+
- match: \s*(---)\s*$\n?
129+
captures:
130+
0: meta.frontmatter.rust
131+
1: punctuation.section.frontmatter.begin.rust
132+
embed: scope:source.toml
133+
embed_scope: meta.frontmatter.rust source.toml.embedded.rust
134+
escape: ^\s*(---)\s*$\n?
135+
escape_captures:
136+
0: meta.frontmatter.rust
137+
1: punctuation.section.frontmatter.end.rust
138+
pop: 1
139+
- match: ^|(?=\S) # Note: Ensure to highlight shebang if Rust is embedded.
140+
pop: 1
141+
104142
statements:
105143
# This isn't really just "statements", it also includes all types,
106144
# expressions, items, etc.

Rust/tests/syntax_test_frontmatter.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#! SYNTAX TEST "Packages/Markdown/Markdown.sublime-syntax"
2+
3+
1. shebang
4+
5+
```rust
6+
#!/usr/bin/env cargo
7+
#! <- markup.list.numbered.markdown markup.raw.code-fence.rust.markdown-gfm source.rust comment.line.shebang.rust punctuation.definition.comment.rust
8+
#!^^^^^^^^^^^^^^^^^^^ markup.list.numbered.markdown markup.raw.code-fence.rust.markdown-gfm source.rust comment.line.shebang.rust - punctuation
9+
#! ^^^^^ constant.language.shebang.rust
10+
```
11+
12+
2. frontmatter
13+
14+
```rust
15+
---
16+
#! <- markup.list.numbered.markdown markup.raw.code-fence.rust.markdown-gfm source.rust meta.frontmatter.rust punctuation.section.frontmatter.begin.rust
17+
[section]
18+
#! <- 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
19+
key = "value"
20+
#! <- 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
21+
---
22+
#! <- markup.list.numbered.markdown markup.raw.code-fence.rust.markdown-gfm source.rust meta.frontmatter.rust punctuation.section.frontmatter.end.rust
23+
24+
fn main() {}
25+
#! <- markup.list.numbered.markdown markup.raw.code-fence.rust.markdown-gfm source.rust meta.function.rust keyword.declaration.function.rust
26+
#!^^^^^^^^^^ source.rust meta.function.rust
27+
```

Rust/tests/syntax_test_frontmatter.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#! SYNTAX TEST "Rust.sublime-syntax"
2+
---
3+
#! <- meta.frontmatter.rust punctuation.section.frontmatter.begin.rust
4+
#!^ meta.frontmatter.rust punctuation.section.frontmatter.begin.rust
5+
#! ^ meta.frontmatter.rust - punctuation
6+
[section]
7+
#! <- meta.frontmatter.rust source.toml.embedded.rust source.toml meta.section.toml meta.brackets.toml punctuation.section.brackets.begin.toml
8+
key = "value"
9+
#! <- meta.frontmatter.rust source.toml.embedded.rust meta.mapping.key.toml meta.string.toml string.unquoted.toml
10+
---
11+
#! <- meta.frontmatter.rust punctuation.section.frontmatter.end.rust
12+
#!^ meta.frontmatter.rust punctuation.section.frontmatter.end.rust
13+
#! ^ meta.frontmatter.rust - punctuation
14+
15+
fn main() {}
16+
#! <- meta.function.rust keyword.declaration.function.rust
17+
#!^^^^^^^^^^ meta.function.rust

Rust/tests/syntax_test_shebang.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#! SYNTAX TEST "Rust.sublime-syntax"
2+
#! <- comment.line.shebang.rust punctuation.definition.comment.rust
3+
#! <- comment.line.shebang.rust punctuation.definition.comment.rust
4+
#!^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.shebang.rust - punctuation

0 commit comments

Comments
 (0)