| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
module Prism |
| 5 |
class ParseResult < Result |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class Comments |
| 21 |
|
| 22 |
|
| 23 |
class NodeTarget |
| 24 |
attr_reader :node |
| 25 |
|
| 26 |
def initialize(node) |
| 27 |
@node = node |
| 28 |
end |
| 29 |
|
| 30 |
def start_offset |
| 31 |
node.start_offset |
| 32 |
end |
| 33 |
|
| 34 |
def end_offset |
| 35 |
node.end_offset |
| 36 |
end |
| 37 |
|
| 38 |
def encloses?(comment) |
| 39 |
start_offset <= comment.location.start_offset && |
| 40 |
comment.location.end_offset <= end_offset |
| 41 |
end |
| 42 |
|
| 43 |
def leading_comment(comment) |
| 44 |
node.location.leading_comment(comment) |
| 45 |
end |
| 46 |
|
| 47 |
def trailing_comment(comment) |
| 48 |
node.location.trailing_comment(comment) |
| 49 |
end |
| 50 |
end |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
class LocationTarget |
| 55 |
attr_reader :location |
| 56 |
|
| 57 |
def initialize(location) |
| 58 |
@location = location |
| 59 |
end |
| 60 |
|
| 61 |
def start_offset |
| 62 |
location.start_offset |
| 63 |
end |
| 64 |
|
| 65 |
def end_offset |
| 66 |
location.end_offset |
| 67 |
end |
| 68 |
|
| 69 |
def encloses?(comment) |
| 70 |
false |
| 71 |
end |
| 72 |
|
| 73 |
def leading_comment(comment) |
| 74 |
location.leading_comment(comment) |
| 75 |
end |
| 76 |
|
| 77 |
def trailing_comment(comment) |
| 78 |
location.trailing_comment(comment) |
| 79 |
end |
| 80 |
end |
| 81 |
|
| 82 |
|
| 83 |
attr_reader :parse_result |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
def initialize(parse_result) |
| 88 |
@parse_result = parse_result |
| 89 |
end |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
def attach! |
| 94 |
parse_result.comments.each do |comment| |
| 95 |
preceding, enclosing, following = nearest_targets(parse_result.value, comment) |
| 96 |
|
| 97 |
if comment.trailing? |
| 98 |
if preceding |
| 99 |
preceding.trailing_comment(comment) |
| 100 |
else |
| 101 |
(following || enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment) |
| 102 |
end |
| 103 |
else |
| 104 |
|
| 105 |
if following |
| 106 |
following.leading_comment(comment) |
| 107 |
elsif preceding |
| 108 |
preceding.trailing_comment(comment) |
| 109 |
else |
| 110 |
(enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment) |
| 111 |
end |
| 112 |
end |
| 113 |
end |
| 114 |
end |
| 115 |
|
| 116 |
private |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
def nearest_targets(node, comment) |
| 121 |
comment_start = comment.location.start_offset |
| 122 |
comment_end = comment.location.end_offset |
| 123 |
|
| 124 |
targets = [] |
| 125 |
node.comment_targets.map do |value| |
| 126 |
case value |
| 127 |
when StatementsNode |
| 128 |
targets.concat(value.body.map { |node| NodeTarget.new(node) }) |
| 129 |
when Node |
| 130 |
targets << NodeTarget.new(value) |
| 131 |
when Location |
| 132 |
targets << LocationTarget.new(value) |
| 133 |
end |
| 134 |
end |
| 135 |
|
| 136 |
targets.sort_by!(&:start_offset) |
| 137 |
preceding = nil |
| 138 |
following = nil |
| 139 |
|
| 140 |
left = 0 |
| 141 |
right = targets.length |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
while left < right |
| 147 |
middle = (left + right) / 2 |
| 148 |
target = targets[middle] |
| 149 |
|
| 150 |
target_start = target.start_offset |
| 151 |
target_end = target.end_offset |
| 152 |
|
| 153 |
if target.encloses?(comment) |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
return nearest_targets(target.node, comment) |
| 158 |
end |
| 159 |
|
| 160 |
if target_end <= comment_start |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
preceding = target |
| 166 |
left = middle + 1 |
| 167 |
next |
| 168 |
end |
| 169 |
|
| 170 |
if comment_end <= target_start |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
following = target |
| 176 |
right = middle |
| 177 |
next |
| 178 |
end |
| 179 |
|
| 180 |
|
| 181 |
raise "Comment location overlaps with a target location" |
| 182 |
end |
| 183 |
|
| 184 |
[preceding, NodeTarget.new(node), following] |
| 185 |
end |
| 186 |
end |
| 187 |
end |
| 188 |
end |