Module: Jade::Formatter::StructDeclaration

Extended by:
Helper, StructDeclaration
Included in:
StructDeclaration
Defined in:
lib/jade/formatter/declarations.rb

Instance Method Summary collapse

Methods included from Helper

and_indent, dispatch_for, format_delimited, format_exposing, format_leading_comments, format_node, format_pattern, format_trailing_comment, format_type, format_type_atom, too_long?

Instance Method Details

#format(node, indent:, source:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jade/formatter/declarations.rb', line 63

def format(node, indent:, source:)
  node => AST::StructDeclaration(name:, type_params:, record_type:)

  params_str = type_params.empty? ?
    "" :
    "(#{type_params.map(&:name).join(', ')})"
  header = "struct #{name}#{params_str} ="

  record_type => AST::TypeRecord(fields:, row_var:)

  if fields.size > 1
    format_multiline(header, fields, row_var, indent)
  else
    "#{header} #{format_type(record_type)}".then(&and_indent(indent))
  end
end

#format_multiline(header, fields, row_var, indent) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/jade/formatter/declarations.rb', line 80

def format_multiline(header, fields, row_var, indent)
  open_brace = row_var ? "{ #{row_var.name} |" : "{"
  fields_str = fields
    .map { |k, v| "#{k}: #{format_type(v)}".then(&and_indent(indent + 1)) }
    .join(",\n")

  and_indent(indent)
    .call("#{header} #{open_brace}")
    .then { "#{it}\n#{fields_str}\n#{INDENT * indent}}" }
end