-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide code navigation features to erb files (#2235)
* Provide features to erb files This commit allows Ruby LSP to start handling requests for ERB files, such as definition, completion, hover...etc., which will give users the same level of code navigation features in ERB and Ruby. However, certain requests are not supported: - formatting - on_type_formatting - diagnostics - code_actions Co-authored-by: Vinicius Stock <vinicius.stock@shopify.com> * Treat top-level method calls in erb files as receiver unknown Currently their receivers will be assumed as `Object`, which means most helper methods like `link_to` won't have completion even though we do have it in the index and are sure that `Object` doesn't define it. By avoiding setting the receiver as `Object`, we can greatly increase the number methods that can have definition support. * Handle windows newline characters correctly * Avoid passing document to listeners * Update lib/ruby_lsp/store.rb Co-authored-by: Vinicius Stock <vinistock@users.noreply.github.com> --------- Co-authored-by: Vinicius Stock <vinicius.stock@shopify.com> Co-authored-by: Vinicius Stock <vinistock@users.noreply.github.com>
- Loading branch information
1 parent
5e16484
commit 9f22f36
Showing
17 changed files
with
433 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
class ERBDocument < Document | ||
extend T::Sig | ||
|
||
sig { override.returns(Prism::ParseResult) } | ||
def parse | ||
return @parse_result unless @needs_parsing | ||
|
||
@needs_parsing = false | ||
scanner = ERBScanner.new(@source) | ||
scanner.scan | ||
@parse_result = Prism.parse(scanner.ruby) | ||
end | ||
|
||
sig { override.returns(T::Boolean) } | ||
def syntax_error? | ||
@parse_result.failure? | ||
end | ||
|
||
sig { override.returns(LanguageId) } | ||
def language_id | ||
LanguageId::ERB | ||
end | ||
|
||
class ERBScanner | ||
extend T::Sig | ||
|
||
sig { returns(String) } | ||
attr_reader :ruby, :html | ||
|
||
sig { params(source: String).void } | ||
def initialize(source) | ||
@source = source | ||
@html = T.let(+"", String) | ||
@ruby = T.let(+"", String) | ||
@current_pos = T.let(0, Integer) | ||
@inside_ruby = T.let(false, T::Boolean) | ||
end | ||
|
||
sig { void } | ||
def scan | ||
while @current_pos < @source.length | ||
scan_char | ||
@current_pos += 1 | ||
end | ||
end | ||
|
||
private | ||
|
||
sig { void } | ||
def scan_char | ||
char = @source[@current_pos] | ||
|
||
case char | ||
when "<" | ||
if next_char == "%" | ||
@inside_ruby = true | ||
@current_pos += 1 | ||
push_char(" ") | ||
|
||
if next_char == "=" && @source[@current_pos + 2] == "=" | ||
@current_pos += 2 | ||
push_char(" ") | ||
elsif next_char == "=" || next_char == "-" | ||
@current_pos += 1 | ||
push_char(" ") | ||
end | ||
else | ||
push_char(T.must(char)) | ||
end | ||
when "-" | ||
if @inside_ruby && next_char == "%" && | ||
@source[@current_pos + 2] == ">" | ||
@current_pos += 2 | ||
push_char(" ") | ||
@inside_ruby = false | ||
else | ||
push_char(T.must(char)) | ||
end | ||
when "%" | ||
if @inside_ruby && next_char == ">" | ||
@inside_ruby = false | ||
@current_pos += 1 | ||
push_char(" ") | ||
else | ||
push_char(T.must(char)) | ||
end | ||
when "\r" | ||
@ruby << char | ||
@html << char | ||
|
||
if next_char == "\n" | ||
@ruby << next_char | ||
@html << next_char | ||
@current_pos += 1 | ||
end | ||
when "\n" | ||
@ruby << char | ||
@html << char | ||
else | ||
push_char(T.must(char)) | ||
end | ||
end | ||
|
||
sig { params(char: String).void } | ||
def push_char(char) | ||
if @inside_ruby | ||
@ruby << char | ||
@html << " " * char.length | ||
else | ||
@ruby << " " * char.length | ||
@html << char | ||
end | ||
end | ||
|
||
sig { returns(String) } | ||
def next_char | ||
@source[@current_pos + 1] || "" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.