summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/prism/parse_result/comments.rb17
-rw-r--r--prism/templates/lib/prism/node.rb.erb14
2 files changed, 26 insertions, 5 deletions
diff --git a/lib/prism/parse_result/comments.rb b/lib/prism/parse_result/comments.rb
index 26775b7f76..dd8fa8e1c7 100644
--- a/lib/prism/parse_result/comments.rb
+++ b/lib/prism/parse_result/comments.rb
@@ -27,11 +27,11 @@ module Prism
end
def start_offset
- node.location.start_offset
+ node.start_offset
end
def end_offset
- node.location.end_offset
+ node.end_offset
end
def encloses?(comment)
@@ -94,13 +94,20 @@ module Prism
preceding, enclosing, following = nearest_targets(parse_result.value, comment)
if comment.trailing?
- preceding&.trailing_comment(comment) ||
+ if preceding
+ preceding.trailing_comment(comment)
+ else
(following || enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment)
+ end
else
# If a comment exists on its own line, prefer a leading comment.
- following&.leading_comment(comment) ||
- preceding&.trailing_comment(comment) ||
+ if following
+ following.leading_comment(comment)
+ elsif preceding
+ preceding.trailing_comment(comment)
+ else
(enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment)
+ end
end
end
end
diff --git a/prism/templates/lib/prism/node.rb.erb b/prism/templates/lib/prism/node.rb.erb
index a8a02cf72e..2eb385c09a 100644
--- a/prism/templates/lib/prism/node.rb.erb
+++ b/prism/templates/lib/prism/node.rb.erb
@@ -14,6 +14,20 @@ module Prism
@location = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end
+ # The start offset of the node in the source. This method is effectively a
+ # delegate method to the location object.
+ def start_offset
+ location = @location
+ location.is_a?(Location) ? location.start_offset : location >> 32
+ end
+
+ # The end offset of the node in the source. This method is effectively a
+ # delegate method to the location object.
+ def end_offset
+ location = @location
+ location.is_a?(Location) ? location.end_offset : ((location >> 32) + (location & 0xFFFFFFFF))
+ end
+
def newline? # :nodoc:
@newline ? true : false
end