diff options
| author | Kevin Newton <kddnewton@gmail.com> | 2024-02-18 16:11:18 -0500 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2024-02-18 21:37:13 +0000 |
| commit | ea529dd409a7edcaa2c6154a315ec6766b98459d (patch) | |
| tree | f184f4fe97e040b7867d8170ad208491987dfe1a /lib/prism | |
| parent | 792804e32f7aaa1008acd01068df3c0b85cd8ffe (diff) | |
[ruby/prism] Lazy methods for start and end offset on node
https://github.com/ruby/prism/commit/4510e2746d
Diffstat (limited to 'lib/prism')
| -rw-r--r-- | lib/prism/parse_result/comments.rb | 17 |
1 files changed, 12 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 |
