-
Notifications
You must be signed in to change notification settings - Fork 172
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
Add support for providing features on ERB files #1055
Comments
You may need a custom Engine to parse the ERB supported by Rails properly: https://github.com/Shopify/spoom/blob/at-deadcode/lib/spoom/deadcode/erb.rb#L8. The complexity is correctly mapping lines and columns to the original ERB source. |
Thanks for pointing that out. I suppose if Rails is available in the Gemfile, we'll probably be able to require their own custom engine and use it. |
Attempt at erasing the HTML # typed: strict
# frozen_string_literal: true
require "prism"
require "strscan"
original = File.read("index.html.erb")
scanner = StringScanner.new(original)
output = +""
until scanner.eos?
non_ruby_code = scanner.scan_until(/<%(-|=)?/)
break unless non_ruby_code
output << non_ruby_code.gsub(/[^\n]/, " ")
ruby_code = scanner.scan_until(/(-)?%>/)
break unless ruby_code
output << ruby_code[...-2]
output << " "
end
puts output
p Prism.parse(output) |
+1 for ERB support ERB support is really important for Rails projects |
We can use VS Code's embedded language features to forward HTML operations to the built-in language server and get features: https://code.visualstudio.com/api/language-extensions/embedded-languages. |
This issue is being marked as stale because there was no activity in the last 2 months |
🆙 anti-stale bump
Remember that ERB isn’t only about Rails and HTML. |
Initial support for ERB has been shipped in #2235, so I'm closing this issue in favour of more specific ones if we need them. |
I think there's a path to add support for ERB without a ton of effort. We can try to require
erb
from the main bundle and provide features only if it is a dependency.Basically, we can use the ERB gem to parse the
.erb
file and extract the Ruby code. Then we parse the Ruby code with Prism and provide features for it.In terms of the codebase, I think the best way forward would be turning creating an abstract parent class
Document
and turning our existing document class intoRubyDocument
. Then we can create anErbDocument
class to handle the specific details of ERB.Notes
Diagnostics, formatting and on type formatting need to happen differently on ERB files. For example, if some is using
[].each do %>
and breaks the line, we need to insert<% end %>
and not justend
. Similarly, we can see if erb lint is available to provide diagnostics and potentially format the code too.It might be worth separating these requests into
RubyFormatting
andErbFormatting
.The text was updated successfully, but these errors were encountered: