Skip to content

Commit

Permalink
Merge pull request #2740 from ruby/slice-lines
Browse files Browse the repository at this point in the history
Location#slice_lines, Node#slice_lines
  • Loading branch information
kddnewton committed Apr 26, 2024
2 parents 754d9be + 9b61f6f commit 6a2c499
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lib/prism/parse_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def line_start(byte_offset)
offsets[find_line(byte_offset)]
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.
def column(byte_offset)
byte_offset - line_start(byte_offset)
Expand Down Expand Up @@ -176,6 +182,15 @@ def slice
source.slice(start_offset, length)
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
# starts.
def start_character_offset
Expand Down
4 changes: 2 additions & 2 deletions rbi/prism/inspect_visitor.rbi
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# typed: strict

class Prism::InspectVisitor < Prism::Visitor
sig { params(String).void }
sig { params(indent: String).void }
def initialize(indent = ""); end

sig { params(Prism::Node).returns(String) }
sig { params(node: Prism::Node).returns(String) }
def self.compose(node); end

sig { returns(String) }
Expand Down
2 changes: 2 additions & 0 deletions sig/prism/parse_result.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Prism
def slice: (Integer byte_offset, Integer length) -> String
def line: (Integer byte_offset) -> Integer
def line_start: (Integer byte_offset) -> Integer
def line_end: (Integer byte_offset) -> Integer
def line_offset: (Integer byte_offset) -> Integer
def column: (Integer byte_offset) -> Integer
def character_offset: (Integer byte_offset) -> Integer
Expand All @@ -31,6 +32,7 @@ module Prism
def copy: (?source: Source, ?start_offset: Integer, ?length: Integer) -> Location
def chop: () -> Location
def slice: () -> String
def slice_lines: () -> String
def start_character_offset: () -> Integer
def end_offset: () -> Integer
def end_character_offset: () -> Integer
Expand Down
7 changes: 7 additions & 0 deletions templates/lib/prism/node.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ module Prism
location.slice
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
# the pretty print object.
def pretty_print(q)
Expand Down
3 changes: 3 additions & 0 deletions templates/rbi/prism/node.rbi.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Prism::Node
sig { returns(String) }
def slice; end

sig { returns(String) }
def slice_lines; end

sig { params(q: T.untyped).void }
def pretty_print(q); end

Expand Down
1 change: 1 addition & 0 deletions templates/sig/prism/node.rbs.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Prism
def start_offset: () -> Integer
def end_offset: () -> Integer
def slice: () -> String
def slice_lines: () -> String
def pretty_print: (untyped q) -> untyped
def to_dot: () -> String
end
Expand Down
7 changes: 7 additions & 0 deletions test/prism/ruby_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ def test_location_chop
assert_equal "", location.slice
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?
refute parse_expression("\"foo\"").heredoc?
refute parse_expression("\"foo \#{1}\"").heredoc?
Expand Down

0 comments on commit 6a2c499

Please sign in to comment.