-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
88 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ class LanguageId < T::Enum | |
enums do | ||
Ruby = new("ruby") | ||
ERB = new("erb") | ||
RBS = new("rbs") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
class RBSDocument < Document | ||
extend T::Sig | ||
extend T::Generic | ||
|
||
ParseResultType = type_member { { fixed: T::Array[RBS::AST::Declarations::Base] } } | ||
|
||
sig { params(source: String, version: Integer, uri: URI::Generic, encoding: Encoding).void } | ||
def initialize(source:, version:, uri:, encoding: Encoding::UTF_8) | ||
@syntax_error = T.let(false, T::Boolean) | ||
super | ||
end | ||
|
||
sig { override.returns(ParseResultType) } | ||
def parse | ||
return @parse_result unless @needs_parsing | ||
|
||
@needs_parsing = false | ||
|
||
_, _, declarations = RBS::Parser.parse_signature(@source) | ||
@syntax_error = false | ||
@parse_result = declarations | ||
rescue RBS::ParsingError | ||
@syntax_error = true | ||
@parse_result | ||
end | ||
|
||
sig { override.returns(T::Boolean) } | ||
def syntax_error? | ||
@syntax_error | ||
end | ||
|
||
sig { override.returns(LanguageId) } | ||
def language_id | ||
LanguageId::RBS | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class RBSDocumentTest < Minitest::Test | ||
def test_parse_result_is_array_of_declarations | ||
document = RubyLsp::RBSDocument.new(source: <<~RBS, version: 1, uri: URI("file:///foo.rbs")) | ||
class Foo | ||
def bar: () -> void | ||
end | ||
RBS | ||
|
||
refute_predicate(document, :syntax_error?) | ||
assert_equal(:Foo, T.cast(document.parse_result[0], RBS::AST::Declarations::Class).name.name) | ||
end | ||
|
||
def test_parsing_remembers_syntax_errors | ||
# The syntax error is that `-` should be `->` | ||
document = RubyLsp::RBSDocument.new(source: +<<~RBS, version: 1, uri: URI("file:///foo.rbs")) | ||
class Foo | ||
def bar: () - void | ||
end | ||
RBS | ||
|
||
assert_predicate(document, :syntax_error?) | ||
|
||
document.push_edits( | ||
[{ range: { start: { line: 1, character: 15 }, end: { line: 1, character: 15 } }, text: ">" }], | ||
version: 2, | ||
) | ||
document.parse | ||
refute_predicate(document, :syntax_error?) | ||
end | ||
end |