Class: Hexp::Node::PP

Inherits:
Object
  • Object
show all
Defined in:
lib/hexp/node/pp.rb

Overview

Pretty-print a node and its contents

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ PP

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new pretty-printer

Parameters:



11
12
13
# File 'lib/hexp/node/pp.rb', line 11

def initialize(node)
  @node = node
end

Class Method Details

.indent(string, indent = 2) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Indent a multiline string with a number of spaces

Parameters:

  • string (String)

    The string to indent

  • indent (Integer) (defaults to: 2)

    The number of spaces to use for indentation

Returns:

  • (String)


69
70
71
# File 'lib/hexp/node/pp.rb', line 69

def self.indent(string, indent = 2)
  string.lines.map {|line|  " "*indent + line}.join
end

Instance Method Details

#callString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Perform the pretty-printing

Returns:

  • (String)

    The pp output



20
21
22
23
24
25
26
# File 'lib/hexp/node/pp.rb', line 20

def call
  [
    @node.class.inspect_name,
    pp_tag,
    PP.indent(pp_attributes + pp_children).strip
  ].join
end

#pp_attributesString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Format the node attributes

Returns:

  • (String)


42
43
44
45
46
# File 'lib/hexp/node/pp.rb', line 42

def pp_attributes
  attrs = @node.attributes
  return '' if attrs.empty?
  ', ' + attrs.inspect
end

#pp_childrenString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Format the node children

Returns:

  • (String)


53
54
55
56
57
# File 'lib/hexp/node/pp.rb', line 53

def pp_children
  children = @node.children
  return ']' if children.empty?
  ", [\n#{ children.map(&:pp).join(",\n") }]]"
end

#pp_tagString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Format the node tag

Returns:

  • (String)


33
34
35
# File 'lib/hexp/node/pp.rb', line 33

def pp_tag
  "[#{@node.tag.inspect}"
end