mirror of
https://github.com/ruby/ruby.git
synced 2025-09-17 01:23:57 +02:00
parent
0599184a18
commit
9688093124
3 changed files with 29 additions and 0 deletions
|
@ -45,6 +45,12 @@ module Prism
|
||||||
offsets[find_line(byte_offset)]
|
offsets[find_line(byte_offset)]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns the byte offset of the end of the line corresponding to the given
|
||||||
|
# byte offset.
|
||||||
|
def line_end(byte_offset)
|
||||||
|
offsets[find_line(byte_offset) + 1] || source.bytesize
|
||||||
|
end
|
||||||
|
|
||||||
# Return the column number for the given byte offset.
|
# Return the column number for the given byte offset.
|
||||||
def column(byte_offset)
|
def column(byte_offset)
|
||||||
byte_offset - line_start(byte_offset)
|
byte_offset - line_start(byte_offset)
|
||||||
|
@ -176,6 +182,15 @@ module Prism
|
||||||
source.slice(start_offset, length)
|
source.slice(start_offset, length)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The source code that this location represents starting from the beginning
|
||||||
|
# of the line that this location starts on to the end of the line that this
|
||||||
|
# location ends on.
|
||||||
|
def slice_lines
|
||||||
|
line_start = source.line_start(start_offset)
|
||||||
|
line_end = source.line_end(end_offset)
|
||||||
|
source.slice(line_start, line_end - line_start)
|
||||||
|
end
|
||||||
|
|
||||||
# The character offset from the beginning of the source where this location
|
# The character offset from the beginning of the source where this location
|
||||||
# starts.
|
# starts.
|
||||||
def start_character_offset
|
def start_character_offset
|
||||||
|
|
|
@ -45,6 +45,13 @@ module Prism
|
||||||
location.slice
|
location.slice
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Slice the location of the node from the source, starting at the beginning
|
||||||
|
# of the line that the location starts on, ending at the end of the line
|
||||||
|
# that the location ends on.
|
||||||
|
def slice_lines
|
||||||
|
location.slice_lines
|
||||||
|
end
|
||||||
|
|
||||||
# Similar to inspect, but respects the current level of indentation given by
|
# Similar to inspect, but respects the current level of indentation given by
|
||||||
# the pretty print object.
|
# the pretty print object.
|
||||||
def pretty_print(q)
|
def pretty_print(q)
|
||||||
|
|
|
@ -209,6 +209,13 @@ module Prism
|
||||||
assert_equal "", location.slice
|
assert_equal "", location.slice
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_location_slice_lines
|
||||||
|
result = Prism.parse("\nprivate def foo\nend\n")
|
||||||
|
method = result.value.statements.body.first.arguments.arguments.first
|
||||||
|
|
||||||
|
assert_equal "private def foo\nend\n", method.slice_lines
|
||||||
|
end
|
||||||
|
|
||||||
def test_heredoc?
|
def test_heredoc?
|
||||||
refute parse_expression("\"foo\"").heredoc?
|
refute parse_expression("\"foo\"").heredoc?
|
||||||
refute parse_expression("\"foo \#{1}\"").heredoc?
|
refute parse_expression("\"foo \#{1}\"").heredoc?
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue