Skip to content
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 ERB Support #1184

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/ruby_lsp/document.rb
Expand Up @@ -180,6 +180,11 @@ def sorbet_sigil_is_true_or_higher
end
end

sig { returns(T::Boolean) }
def typechecker_enabled?
DependencyDetector.instance.typechecker_for_uri?(uri) && sorbet_sigil_is_true_or_higher
end

class Scanner
extend T::Sig

Expand Down
40 changes: 40 additions & 0 deletions lib/ruby_lsp/erb_document.rb
@@ -0,0 +1,40 @@
# typed: strict
# frozen_string_literal: true

require "strscan"

module RubyLsp
class ERBDocument < Document
sig { override.returns(Prism::ParseResult) }
def parse
return @parse_result unless @needs_parsing

@needs_parsing = false

@parse_result = Prism.parse(scan(@source))
end

private

sig { params(source: String).returns(String) }
def scan(source)
scanner = StringScanner.new(source)
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

output
end
end
end