Module: Jade::Formatter::Exposing

Extended by:
Exposing, Helper
Included in:
Exposing
Defined in:
lib/jade/formatter/exposing.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: 0) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jade/formatter/exposing.rb', line 7

def format(node, indent: 0)
  case node
  in AST::ExposeAll
    "exposing (..)"

  in AST::ExposeNone | nil
    ""

  in AST::ExposeList(items:, trailing_comma:)
    item_strs = sort(items).map { format_item(it) }
    inline    = "exposing (#{item_strs.join(', ')})"

    if trailing_comma || too_long?(inline, indent)
      inner = item_strs.map { "#{INDENT * (indent + 1)}#{it}," }.join("\n")
      "exposing (\n#{inner}\n#{INDENT * indent})"
    else
      inline
    end
  end
end

#format_item(node) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/jade/formatter/exposing.rb', line 38

def format_item(node)
  case node
  in AST::ExposeValue(name:)      then name
  in AST::ExposeType(name:)       then name
  in AST::ExposeTypeExpand(name:) then "#{name}(..)"
  in AST::ExposeAs(as:)           then as
  end
end

#sort(items) ⇒ Object

Types and constructors first, then values — both alphabetised.



29
30
31
32
33
34
35
36
# File 'lib/jade/formatter/exposing.rb', line 29

def sort(items)
  items.sort_by do |item|
    case item
    in AST::ExposeType | AST::ExposeTypeExpand then [0, item.name]
    in AST::ExposeValue then [1, item.name]
    end
  end
end