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

- (PP) initialize(node)

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:



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

def initialize(node)
  @node = node
end

Class Method Details

+ (String) indent(string, indent = 2)

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)


65
66
67
# File 'lib/hexp/node/pp.rb', line 65

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

Instance Method Details

- (String) call

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



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

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

- (String) pp_attributes

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)


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

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

- (String) pp_children

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)


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

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

- (String) pp_tag

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)


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

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