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

Fix source display #1832

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/rbs/location_aux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ class Location
def inspect
rks = each_required_key.to_a
ops = each_optional_key.to_a.map {|x| "?#{x}" }
"#<#{self.class}:#{self.__id__} buffer=#{buffer.name}, start=#{start_line}:#{start_column}, pos=#{start_pos}...#{end_pos}, children=#{(rks + ops).join(",")} source='#{source.lines.first&.chomp}'>"
src = if source.length <= 1
source.inspect
else
source.each_line.first&.chomp&.inspect
end
"#<#{self.class}:#{self.__id__} buffer=#{buffer.name}, start=#{start_line}:#{start_column}, pos=#{start_pos}...#{end_pos}, children=#{(rks + ops).join(",")} source=#{src}>"
end

def self.new(buffer_ = nil, start_pos_ = nil, end_pos_ = nil, buffer: nil, start_pos: nil, end_pos: nil)
Expand Down
14 changes: 14 additions & 0 deletions test/rbs/location_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ def test_location_to_s
assert_equal "foo.rbs:1:0...2:3", loc.to_s
end

def test_location_inspect
content = ''
loc = Location.new(Buffer.new(name: "trivia.rbs", content: content), 0, content.length)
assert_include loc.inspect, "source=\"\""

content = "\n"
loc = Location.new(Buffer.new(name: "trivia.rbs", content: content), 0, content.length)
assert_include loc.inspect, "source=\"\\n\""

content = "class Foo\n def foo: () -> void\nend\n"
loc = Location.new(Buffer.new(name: "foo.rbs", content: content), 0, content.length)
assert_include loc.inspect, "source=\"class Foo\""
end

private

def buffer(content: nil)
Expand Down