Class: Prism::Merge::WrapperCommentSupport
- Inherits:
-
Object
- Object
- Prism::Merge::WrapperCommentSupport
- Includes:
- SourceLineLookup
- Defined in:
- lib/prism/merge/wrapper_comment_support.rb
Instance Attribute Summary collapse
-
#merger ⇒ Object
readonly
Returns the value of attribute merger.
Instance Method Summary collapse
- #append_inline_comment_entries(line, entries) ⇒ Object
- #comment_attachment_for(node, source:, analysis: nil) ⇒ Object
- #emit_blank_lines_between(result, last_comment_line:, next_content_line:, analysis:, source:, decision:) ⇒ Object
- #emit_comment_region(result, region, analysis:, source:, decision:, previous_line: nil) ⇒ Object
- #emit_external_trailing_comments(result, comments, source_node:, analysis:, source:, decision:) ⇒ Object
- #emit_leading_comments(result, comments, analysis:, source:, decision:, prev_comment_line: nil) ⇒ Object
- #emit_orphan_regions(result, regions, analysis:, source:, decision:, previous_line: nil) ⇒ Object
-
#external_trailing_comments_for(node, analysis: nil, claimed_lines: Set.new) ⇒ Object
Returns trailing comments that fall outside the node's own line range.
- #filtered_leading_comments_for(node, source) ⇒ Object
-
#initialize(merger:) ⇒ WrapperCommentSupport
constructor
A new instance of WrapperCommentSupport.
- #inline_comment_entries_by_line(entries) ⇒ Object
- #inline_comment_separator_for(line_text, raw_comment) ⇒ Object
- #line_inline_comment_entries(analysis, line_num) ⇒ Object
- #orphan_line_numbers_for(source) ⇒ Object
- #orphan_regions_for(node, source:, analysis: nil) ⇒ Object
- #wrapper_inline_comment_entries_by_line(analysis, node) ⇒ Object
Constructor Details
#initialize(merger:) ⇒ WrapperCommentSupport
Returns a new instance of WrapperCommentSupport.
10 11 12 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 10 def initialize(merger:) @merger = merger end |
Instance Attribute Details
#merger ⇒ Object (readonly)
Returns the value of attribute merger.
8 9 10 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 8 def merger @merger end |
Instance Method Details
#append_inline_comment_entries(line, entries) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 260 def append_inline_comment_entries(line, entries) normalized_entries = Array(entries).filter_map do |entry| raw = entry[:raw].to_s.lstrip next if raw.empty? entry.merge(raw: raw) end return line if normalized_entries.empty? normalized_entries.each_with_index.reduce(line) do |memo, (entry, index)| separator = if memo.empty? '' elsif index.zero? entry[:separator].to_s.empty? ? ' ' : entry[:separator] else ' ' end memo + separator + entry[:raw] end end |
#comment_attachment_for(node, source:, analysis: nil) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 72 def (node, source:, analysis: nil) = cached_comment_augmenter_for(source)&.(node) ||= analysis.(node) if analysis.respond_to?(:comment_attachment_for) = (node, source: source, analysis: analysis, base_attachment: ) return if end |
#emit_blank_lines_between(result, last_comment_line:, next_content_line:, analysis:, source:, decision:) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 134 def emit_blank_lines_between(result, last_comment_line:, next_content_line:, analysis:, source:, decision:) return if next_content_line <= last_comment_line + 1 dest_prefix_comment_lines = merger.instance_variable_get(:@dest_prefix_comment_lines) last_emitted_line = nil ((last_comment_line + 1)...next_content_line).each do |line_num| next if dest_prefix_comment_lines&.include?(line_num) line = required_source_line( analysis, line_num, context: 'emitting blank line between comment region and content' ) next unless line.strip.empty? if source == :template result.add_line(line, decision: decision, template_line: line_num) else result.add_line(line, decision: decision, dest_line: line_num) end last_emitted_line = line_num end last_emitted_line end |
#emit_comment_region(result, region, analysis:, source:, decision:, previous_line: nil) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 162 def emit_comment_region(result, region, analysis:, source:, decision:, previous_line: nil) return unless region.respond_to?(:nodes) return if region.respond_to?(:empty?) && region.empty? last_emitted_line = nil region.nodes.each do |comment_node| line_num = comment_node_line(comment_node) next unless line_num if previous_line gap_line = emit_blank_lines_between( result, last_comment_line: previous_line, next_content_line: line_num, analysis: analysis, source: source, decision: decision ) end last_emitted_line = gap_line || last_emitted_line line = required_source_line( analysis, line_num, context: 'emitting comment-region node' ) if source == :template result.add_line(line, decision: decision, template_line: line_num) else result.add_line(line, decision: decision, dest_line: line_num) end previous_line = line_num last_emitted_line = line_num end last_emitted_line end |
#emit_external_trailing_comments(result, comments, source_node:, analysis:, source:, decision:) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 226 def emit_external_trailing_comments(result, comments, source_node:, analysis:, source:, decision:) previous_line = source_node.location.end_line last_emitted_line = nil comments.each do |comment| line_num = comment.location.start_line gap_line = emit_blank_lines_between( result, last_comment_line: previous_line, next_content_line: line_num, analysis: analysis, source: source, decision: decision ) last_emitted_line = gap_line || last_emitted_line line = required_comment_line( analysis, comment, context: 'emitting external trailing comment' ) if source == :template result.add_line(line, decision: decision, template_line: line_num) else result.add_line(line, decision: decision, dest_line: line_num) end previous_line = line_num last_emitted_line = line_num end last_emitted_line end |
#emit_leading_comments(result, comments, analysis:, source:, decision:, prev_comment_line: nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 95 def emit_leading_comments(result, comments, analysis:, source:, decision:, prev_comment_line: nil) dest_prefix_comment_lines = merger.instance_variable_get(:@dest_prefix_comment_lines) comments.each do |comment| line_num = comment.location.start_line if prev_comment_line && line_num > prev_comment_line + 1 ((prev_comment_line + 1)...line_num).each do |blank_line_num| next if dest_prefix_comment_lines&.include?(blank_line_num) line = required_source_line( analysis, blank_line_num, context: 'emitting blank line between leading comments' ) if source == :template result.add_line(line, decision: decision, template_line: blank_line_num) else result.add_line(line, decision: decision, dest_line: blank_line_num) end end end line = required_comment_line( analysis, comment, context: 'emitting leading comment' ) line = runtime_comment_line(comment, fallback: line) if source == :template result.add_line(line, decision: decision, template_line: line_num) else result.add_line(line, decision: decision, dest_line: line_num) end prev_comment_line = line_num end end |
#emit_orphan_regions(result, regions, analysis:, source:, decision:, previous_line: nil) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 202 def emit_orphan_regions(result, regions, analysis:, source:, decision:, previous_line: nil) last_emitted_line = nil current_previous_line = previous_line Array(regions).sort_by do |region| region.respond_to?(:start_line) ? region.start_line.to_i : 0 end.each do |region| emitted_line = emit_comment_region( result, region, analysis: analysis, source: source, decision: decision, previous_line: current_previous_line ) next unless emitted_line current_previous_line = region.respond_to?(:end_line) ? region.end_line : emitted_line last_emitted_line = emitted_line end last_emitted_line end |
#external_trailing_comments_for(node, analysis: nil, claimed_lines: Set.new) ⇒ Object
Returns trailing comments that fall outside the node's own line range.
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 318 def external_trailing_comments_for(node, analysis: nil, claimed_lines: Set.new) trailing_comments = node.location.respond_to?(:trailing_comments) ? node.location.trailing_comments : [] node_line_range = node.location.start_line..node.location.end_line prism_trailing_comments = trailing_comments.reject do |comment| ln = comment.location.start_line node_line_range.cover?(ln) || claimed_lines.include?(ln) end inferred_trailing_comments = if analysis inferred_external_trailing_comments_for(node, analysis, claimed_lines) else [] end (prism_trailing_comments + inferred_trailing_comments).uniq do |comment| [comment.location.start_line, comment.slice] end end |
#filtered_leading_comments_for(node, source) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 14 def filtered_leading_comments_for(node, source) all_leading_comments = node.location.respond_to?(:leading_comments) ? node.location.leading_comments : [] last_skipped_line = nil dest_prefix_comment_lines = merger.instance_variable_get(:@dest_prefix_comment_lines) prefix_line_numbers = Prism::Merge::MagicCommentSupport.prefix_comment_line_numbers_for_comments(all_leading_comments) # Lines claimed by promoted BlockDirective nodes (e.g. freeze/nocov markers # that Prism hoisted onto this node as leading_comments). Skip them so they # are not re-emitted after already appearing inside the BlockDirective's lines. dest_claimed = merger.dest_analysis.respond_to?(:claimed_lines) ? merger.dest_analysis.claimed_lines : Set.new template_claimed = merger.template_analysis.respond_to?(:claimed_lines) ? merger.template_analysis.claimed_lines : Set.new source_claimed = source == :destination ? dest_claimed : template_claimed comments = if source == :destination all_leading_comments.reject do |comment| ln = comment.location.start_line if dest_prefix_comment_lines&.include?(ln) last_skipped_line = ln true elsif source_claimed.include?(ln) last_skipped_line = ln true end end elsif dest_prefix_comment_lines&.any? all_leading_comments.reject do |comment| ln = comment.location.start_line if prefix_line_numbers.include?(ln) last_skipped_line = ln true elsif source_claimed.include?(ln) last_skipped_line = ln true end end else all_leading_comments.reject do |comment| ln = comment.location.start_line if source_claimed.include?(ln) last_skipped_line = ln true end end end analysis = source == :destination ? merger.dest_analysis : merger.template_analysis = (node, source: source, analysis: analysis) if &.&.fetch(:runtime_reintegrated, false) && .leading_region runtime_line_numbers = .leading_region.nodes.map { |comment| comment_node_line(comment) } comments = (comments.reject do |comment| runtime_line_numbers.include?(comment.location.start_line) end + .leading_region.nodes) .sort_by { |comment| comment_node_line(comment).to_i } end { comments: comments, last_skipped_line: last_skipped_line } end |
#inline_comment_entries_by_line(entries) ⇒ Object
282 283 284 285 286 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 282 def inline_comment_entries_by_line(entries) entries.each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |entry, by_line| by_line[entry[:line]] << entry end end |
#inline_comment_separator_for(line_text, raw_comment) ⇒ Object
336 337 338 339 340 341 342 343 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 336 def inline_comment_separator_for(line_text, raw_comment) return if line_text.to_s.empty? || raw_comment.to_s.empty? prefix, separator, = line_text.sub(/\r?\n\z/, '').rpartition(raw_comment) return unless separator == raw_comment prefix[/[ \t]+\z/] end |
#line_inline_comment_entries(analysis, line_num) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 288 def line_inline_comment_entries(analysis, line_num) line = analysis.line_at(line_num).to_s return [] if line.strip.empty? || line.lstrip.start_with?('#') return [] unless analysis.respond_to?(:parse_result) && analysis.parse_result.respond_to?(:comments) Array(analysis.parse_result.comments).filter_map do |comment| next unless comment.location.start_line == line_num raw = comment.slice.chomp { line: line_num, raw: raw, separator: inline_comment_separator_for(line, raw) } end end |
#orphan_line_numbers_for(source) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 88 def orphan_line_numbers_for(source) Array(cached_comment_augmenter_for(source)&.orphan_regions) .flat_map { |region| Array(region.nodes) } .filter_map { |comment_node| comment_node_line(comment_node) } .uniq end |
#orphan_regions_for(node, source:, analysis: nil) ⇒ Object
83 84 85 86 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 83 def orphan_regions_for(node, source:, analysis: nil) = (node, source: source, analysis: analysis) Array(&.orphan_regions) end |
#wrapper_inline_comment_entries_by_line(analysis, node) ⇒ Object
305 306 307 308 309 310 |
# File 'lib/prism/merge/wrapper_comment_support.rb', line 305 def wrapper_inline_comment_entries_by_line(analysis, node) owner_entries = analysis.send(:owner_inline_comment_entries, node) wrapper_lines = merger.send(:begin_node_boundary_lines, node) raw_entries = wrapper_lines.flat_map { |line_num| line_inline_comment_entries(analysis, line_num) } inline_comment_entries_by_line((owner_entries + raw_entries).uniq { |entry| [entry[:line], entry[:raw]] }) end |