Skip to content

ERB injection for other source languages #113

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

Conversation

joeldrapper
Copy link
Contributor

@joeldrapper joeldrapper commented Jun 23, 2025

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.

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.

@cla-bot cla-bot bot added the cla-signed label Jun 23, 2025
@kaspth
Copy link

kaspth commented Jun 23, 2025

I wonder if @marcoroth has thoughts on this given his HERB work.

@joeldrapper
Copy link
Contributor Author

I wonder if unmatched .erb files should just be plain text outside of the ERB tags. I would much rather my .rb.erb file was like that than treated as an HTML/ERB file.

@joeldrapper
Copy link
Contributor Author

Here’s what that looks like.

Screenshot 2025-06-23 at 17 29 44

I think this is a good solution for Ruby and other files, but I still haven't figured out how to enable the YAML language server.

@joeldrapper
Copy link
Contributor Author

I think because YAML is a built-in language server, it will actually need to be associated in this file. https://github.com/zed-industries/zed/blob/a067c16c823354da63966273ac15d1aa93c0e922/crates/languages/src/lib.rs#L98

@joeldrapper joeldrapper force-pushed the erb-for-other-source-languages branch from bd59c30 to 2848538 Compare June 23, 2025 17:07
@joeldrapper
Copy link
Contributor Author

joeldrapper commented Jun 23, 2025

There’s one more problem that’s come up. The language ID is being passed through to Ruby LSP as HTML/ERB or YAML/ERB and RubyLSP doesn’t support those languages. It only supports Ruby or ERB.

We will need a way to map YAML/ERB and HTML/ERB to ERB because as that’s all the language server cares about.

I think this will require upstream changes in the zed_extension_api crate.

Here is an example of the RPS message with the wrong languageId.

{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"file:///Users/joeldrapper/src/zed-ruby/example.html.erb","languageId":"html/erb","version":0,"text":""}}}

@joeldrapper
Copy link
Contributor Author

Looks like this was already supported, I just need to add the mappings.

So the final issue is that the YAML language server isn’t being enabled, but that has to be fixed in the main Zed repo.

@joeldrapper joeldrapper force-pushed the erb-for-other-source-languages branch from cbf7fe6 to 1f93916 Compare June 23, 2025 18:04
@joeldrapper joeldrapper marked this pull request as ready for review June 23, 2025 18:05
@joeldrapper joeldrapper force-pushed the erb-for-other-source-languages branch from 1f93916 to 9b7fc09 Compare June 23, 2025 18:06
@vitallium
Copy link
Collaborator

@joeldrapper Thanks! This awesome improvement works like a charm!

@vitallium vitallium merged commit 14a913e into zed-extensions:main Jun 27, 2025
3 checks passed
@cole-miller cole-miller mentioned this pull request Jun 27, 2025
cole-miller added a commit that referenced this pull request Jun 27, 2025
There was a bug in zed_extension_cli that prevented debug adapters in
extensions from being successfully registered. zed_extension_cli has
been fixed (zed-industries/extensions#2927), but
we need to bump the version of this and the Swift extension so they'll
get rebuild and republished using the fixed CLI.

This version includes:

- #119
- #111
- #113
cole-miller added a commit to zed-industries/extensions that referenced this pull request Jun 27, 2025
This version includes:

- zed-extensions/ruby#119
- zed-extensions/ruby#111
- zed-extensions/ruby#113

Also, as a result of rebuilding the extension with #2927, the new rdbg
debug adapter will be registered successfully.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants