Skip to content

Commit 14a913e

Browse files
authored
ERB injection for other source languages (#113)
ERB can be used to template any kind of source file — not just HTML. However, it’s most commonly used for generating HTML, YAML and Ruby source. This PR updates the `ERB` language to be more generic, treating `.erb` files as plain text with Ruby injections within the ERB template tags. It then introduces two new languages: `HTML/ERB` and `YAML/ERB`, which handle switching between HTML and Ruby and YAML and Ruby respectively. I attempted to make a specialised language for `Ruby/ERB`, but couldn’t get past some basic syntax highlighting issues. I think it’s quite reasonable to highlight `.rb.erb` files as plain text with Ruby inside the ERB tags since it would be very difficult to guess at how the generated Ruby will look structurally. It’s certainly an improvement on treating it like HTML. I’ve configured mappings from `ERB`, `HTML/ERB` and `YAML/ERB` for Ruby LSP, which will handle them all as `erb`. I’ve also updated the Herb LSP to handle `HTML/ERB` files specifically. ### Next steps I would be nice if the YAML language server was configured to run on `YAML/ERB` files. I believe this can be done by updating [this file](https://github.com/zed-industries/zed/blob/a067c16c823354da63966273ac15d1aa93c0e922/crates/languages/src/lib.rs). However, I don’t think this is a blocker as most YAML/ERB files are actually named `.yml` not `.yml.erb` and so will be treated as regular old YAML files.
1 parent aefcb77 commit 14a913e

File tree

9 files changed

+57
-7
lines changed

9 files changed

+57
-7
lines changed

extension.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ languages = ["Ruby"]
1212

1313
[language_servers.ruby-lsp]
1414
name = "Ruby LSP"
15-
languages = ["Ruby", "ERB"]
15+
languages = ["Ruby", "ERB", "HTML/ERB", "YAML/ERB"]
16+
17+
[language_servers.ruby-lsp.language_ids]
18+
"Ruby" = "ruby"
19+
"ERB" = "erb"
20+
"HTML/ERB" = "erb"
21+
"YAML/ERB" = "erb"
1622

1723
[language_servers.rubocop]
1824
name = "Rubocop"
@@ -28,7 +34,7 @@ languages = ["Ruby"]
2834

2935
[language_servers.herb]
3036
name = "Herb"
31-
languages = ["ERB"]
37+
languages = ["HTML/ERB"]
3238

3339
[grammars.ruby]
3440
repository = "https://github.com/tree-sitter/tree-sitter-ruby"

languages/erb/config.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
name = "ERB"
22
grammar = "embedded_template"
33
path_suffixes = ["erb"]
4-
autoclose_before = ">})"
5-
brackets = [{ start = "<", end = ">", close = true, newline = true }]
6-
block_comment = ["<%#", "%>"]
7-
word_characters = ["?", "!"]

languages/erb/injections.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
(#set! "combined"))
44

55
((content) @content
6-
(#set! "language" "html")
6+
(#set! "language" "plaintext")
77
(#set! "combined"))

languages/html-erb/config.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name = "HTML/ERB"
2+
grammar = "embedded_template"
3+
path_suffixes = ["html.erb"]
4+
autoclose_before = ">})"
5+
brackets = [{ start = "<", end = ">", close = true, newline = true }]
6+
block_comment = ["<%#", "%>"]
7+
word_characters = ["?", "!"]

languages/html-erb/highlights.scm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(comment_directive) @comment
2+
3+
[
4+
"<%#"
5+
"<%"
6+
"<%="
7+
"<%_"
8+
"<%-"
9+
"%>"
10+
"-%>"
11+
"_%>"
12+
] @keyword

languages/html-erb/injections.scm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
((code) @content
2+
(#set! "language" "ruby")
3+
(#set! "combined"))
4+
5+
((content) @content
6+
(#set! "language" "html")
7+
(#set! "combined"))

languages/yaml-erb/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name = "YAML/ERB"
2+
grammar = "embedded_template"
3+
path_suffixes = ["yml.erb", "yaml.erb"]

languages/yaml-erb/highlights.scm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(comment_directive) @comment
2+
3+
[
4+
"<%#"
5+
"<%"
6+
"<%="
7+
"<%_"
8+
"<%-"
9+
"%>"
10+
"-%>"
11+
"_%>"
12+
] @keyword

languages/yaml-erb/injections.scm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
((code) @content
2+
(#set! "language" "ruby")
3+
(#set! "combined"))
4+
5+
((content) @content
6+
(#set! "language" "yaml")
7+
(#set! "combined"))

0 commit comments

Comments
 (0)