Skip to content

Commit

Permalink
Merge pull request #2761 from ruby/script-lines
Browse files Browse the repository at this point in the history
Node#script_lines and supporting infra
  • Loading branch information
kddnewton authored May 2, 2024
2 parents 1afc484 + cb4a8ab commit 9bd1373
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/prism/parse_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def encoding
source.encoding
end

# Returns the lines of the source code as an array of strings.
def lines
source.lines
end

# Perform a byteslice on the source code using the given byte offset and
# byte length.
def slice(byte_offset, length)
Expand Down Expand Up @@ -177,6 +182,11 @@ def inspect
"#<Prism::Location @start_offset=#{@start_offset} @length=#{@length} start_line=#{start_line}>"
end

# Returns all of the lines of the source code associated with this location.
def source_lines
source.lines
end

# The source code that this location represents.
def slice
source.slice(start_offset, length)
Expand Down
6 changes: 6 additions & 0 deletions rbi/prism/parse_result.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Prism::Source
sig { returns(Encoding) }
def encoding; end

sig { returns(T::Array[Integer]) }
def lines; end

sig { params(byte_offset: Integer, length: Integer).returns(String) }
def slice(byte_offset, length); end

Expand Down Expand Up @@ -78,6 +81,9 @@ class Prism::Location
sig { returns(String) }
def inspect; end

sig { returns(T::Array[String]) }
def source_lines; end

sig { returns(String) }
def slice; end

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 @@ -6,6 +6,7 @@ module Prism

def initialize: (String source, ?Integer start_line, ?Array[Integer] offsets) -> void
def encoding: () -> Encoding
def lines: () -> Array[String]
def slice: (Integer byte_offset, Integer length) -> String
def line: (Integer byte_offset) -> Integer
def line_start: (Integer byte_offset) -> Integer
Expand All @@ -31,6 +32,7 @@ module Prism
def comments: () -> Array[comment]
def copy: (?source: Source, ?start_offset: Integer, ?length: Integer) -> Location
def chop: () -> Location
def source_lines: () -> Array[String]
def slice: () -> String
def slice_lines: () -> String
def start_character_offset: () -> Integer
Expand Down
9 changes: 9 additions & 0 deletions templates/lib/prism/node.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ module Prism
end
end

# Returns all of the lines of the source code associated with this node.
def source_lines
location.source_lines
end

# An alias for source_lines, used to mimic the API from
# RubyVM::AbstractSyntaxTree to make it easier to migrate.
alias script_lines source_lines

# Slice the location of the node from the source.
def slice
location.slice
Expand Down
6 changes: 6 additions & 0 deletions templates/rbi/prism/node.rbi.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class Prism::Node
sig { returns(Integer) }
def end_offset; end

sig { returns(T::Array[String]) }
def source_lines; end

sig { returns(T::Array[String]) }
def script_lines; end

sig { returns(String) }
def slice; end

Expand Down
2 changes: 2 additions & 0 deletions templates/sig/prism/node.rbs.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module Prism

def start_offset: () -> Integer
def end_offset: () -> Integer
def source_lines: () -> Array[String]
alias script_lines source_lines
def slice: () -> String
def slice_lines: () -> String
def pretty_print: (untyped q) -> untyped
Expand Down

0 comments on commit 9bd1373

Please sign in to comment.