Class: Prism::NodeInspector
Overview
This object is responsible for generating the output for the inspect method implementations of child nodes.
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
:nodoc:.
-
#prefix ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#<<(line) ⇒ Object
Appends a line to the output with the current prefix.
-
#child_inspector(append) ⇒ Object
Returns a new inspector that can be used to inspect a child node.
-
#child_node(node, append) ⇒ Object
Generates a string that represents a child node.
-
#header(node) ⇒ Object
This generates a string that is used as the header of the inspect output for any given node.
-
#initialize(prefix = "") ⇒ NodeInspector
constructor
A new instance of NodeInspector.
-
#list(prefix, nodes) ⇒ Object
Generates a string that represents a list of nodes.
-
#location(value) ⇒ Object
Generates a string that represents a location field on a node.
-
#to_str ⇒ Object
Returns the output as a string.
Constructor Details
#initialize(prefix = "") ⇒ NodeInspector
Returns a new instance of NodeInspector.
9 10 11 12 |
# File 'lib/prism/node_inspector.rb', line 9 def initialize(prefix = "") @prefix = prefix @output = +"" end |
Instance Attribute Details
#output ⇒ Object (readonly)
:nodoc:
7 8 9 |
# File 'lib/prism/node_inspector.rb', line 7 def output @output end |
#prefix ⇒ Object (readonly)
:nodoc:
7 8 9 |
# File 'lib/prism/node_inspector.rb', line 7 def prefix @prefix end |
Instance Method Details
#<<(line) ⇒ Object
Appends a line to the output with the current prefix.
15 16 17 |
# File 'lib/prism/node_inspector.rb', line 15 def <<(line) output << "#{prefix}#{line}" end |
#child_inspector(append) ⇒ Object
Returns a new inspector that can be used to inspect a child node.
59 60 61 |
# File 'lib/prism/node_inspector.rb', line 59 def child_inspector(append) NodeInspector.new("#{prefix}#{append}") end |
#child_node(node, append) ⇒ Object
Generates a string that represents a child node.
54 55 56 |
# File 'lib/prism/node_inspector.rb', line 54 def child_node(node, append) node.inspect(child_inspector(append)).delete_prefix(prefix) end |
#header(node) ⇒ Object
This generates a string that is used as the header of the inspect output for any given node.
21 22 23 24 25 26 27 |
# File 'lib/prism/node_inspector.rb', line 21 def header(node) output = +"@ #{node.class.name.split("::").last} (" output << "location: (#{node.location.start_line},#{node.location.start_column})-(#{node.location.end_line},#{node.location.end_column})" output << ", newline: true" if node.newline? output << ")\n" output end |
#list(prefix, nodes) ⇒ Object
Generates a string that represents a list of nodes. It handles properly using the box drawing characters to make the output look nice.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/prism/node_inspector.rb', line 31 def list(prefix, nodes) output = +"(length: #{nodes.length})\n" last_index = nodes.length - 1 nodes.each_with_index do |node, index| pointer, preadd = (index == last_index) ? ["└── ", " "] : ["├── ", "│ "] node_prefix = "#{prefix}#{preadd}" output << node.inspect(NodeInspector.new(node_prefix)).sub(node_prefix, "#{prefix}#{pointer}") end output end |
#location(value) ⇒ Object
Generates a string that represents a location field on a node.
45 46 47 48 49 50 51 |
# File 'lib/prism/node_inspector.rb', line 45 def location(value) if value "(#{value.start_line},#{value.start_column})-(#{value.end_line},#{value.end_column}) = #{value.slice.inspect}" else "∅" end end |
#to_str ⇒ Object
Returns the output as a string.
64 65 66 |
# File 'lib/prism/node_inspector.rb', line 64 def to_str output end |