Class: Prism::Merge::NodeBodyLayout

Inherits:
Object
  • Object
show all
Defined in:
lib/prism/merge/node_body_layout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node:, analysis:, merger:) ⇒ NodeBodyLayout

Returns a new instance of NodeBodyLayout.



8
9
10
11
12
# File 'lib/prism/merge/node_body_layout.rb', line 8

def initialize(node:, analysis:, merger:)
  @node = node
  @analysis = analysis
  @merger = merger
end

Instance Attribute Details

#analysisObject (readonly)

Returns the value of attribute analysis.



6
7
8
# File 'lib/prism/merge/node_body_layout.rb', line 6

def analysis
  @analysis
end

#mergerObject (readonly)

Returns the value of attribute merger.



6
7
8
# File 'lib/prism/merge/node_body_layout.rb', line 6

def merger
  @merger
end

#nodeObject (readonly)

Returns the value of attribute node.



6
7
8
# File 'lib/prism/merge/node_body_layout.rb', line 6

def node
  @node
end

Instance Method Details

#body_ends_on_closing_line?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/prism/merge/node_body_layout.rb', line 47

def body_ends_on_closing_line?
  body_present? && body_statements.last.location.end_line == node.location.end_line
end

#body_starts_on_opening_line?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/prism/merge/node_body_layout.rb', line 43

def body_starts_on_opening_line?
  body_present? && body_statements.first.location.start_line == node.location.start_line
end

#body_textObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/prism/merge/node_body_layout.rb', line 14

def body_text
  return '' unless body_present?

  lines = []
  lines << opening_line_body_text if body_starts_on_opening_line?

  middle_body_line_range.each do |line_num|
    lines << analysis.line_at(line_num).to_s.chomp
  end

  lines << closing_line_body_text if body_ends_on_closing_line?

  return '' if lines.empty?

  "#{lines.join("\n")}\n"
end

#closing_line_textObject



37
38
39
40
41
# File 'lib/prism/merge/node_body_layout.rb', line 37

def closing_line_text
  return analysis.line_at(node.location.end_line).to_s.chomp unless body_ends_on_closing_line?

  analysis.source.byteslice(body_end_offset...line_end_offset(node.location.end_line)).to_s.chomp
end

#opening_line_textObject



31
32
33
34
35
# File 'lib/prism/merge/node_body_layout.rb', line 31

def opening_line_text
  return analysis.line_at(node.location.start_line).to_s.chomp unless body_starts_on_opening_line?

  analysis.source.byteslice(line_start_offset(node.location.start_line)...body_start_offset).to_s
end

#source_line_for_body_line(body_line) ⇒ Object



51
52
53
54
55
# File 'lib/prism/merge/node_body_layout.rb', line 51

def source_line_for_body_line(body_line)
  return unless body_line

  body_source_lines[body_line - 1]
end