Module: Jade::Formatter::Helper

Overview

Mixin for per-node formatter modules. Provides:

- INDENT, LINE_LIMIT constants
- `format_node(node, indent:, source:)` — top-level dispatcher
that prepends leading comments and appends a trailing one.
- `and_indent(n)` — prefixes every line of a string with N levels of
indent, leaving blank lines untouched.
- `too_long?(line, indent)` — width check against LINE_LIMIT.
- `format_delimited(...)` — the open/items/close pattern used by
Tuple, List, and a few callers.
- `format_pattern`, `format_type`, `format_exposing` — sibling
walks delegated to their own modules.

Instance Method Summary collapse

Instance Method Details

#and_indent(indent) ⇒ Object



68
69
70
71
72
73
# File 'lib/jade/formatter/helper.rb', line 68

def and_indent(indent)
  ->(str) {
    prefix = INDENT * indent
    str.lines.map { |line| line == "\n" ? line : "#{prefix}#{line}" }.join
  }
end

#dispatch_for(node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jade/formatter/helper.rb', line 24

def dispatch_for(node)
  case node
  in AST::Module                       then ModuleNode
  in AST::Body                         then Body
  in AST::FunctionDeclaration          then FunctionDeclaration
  in AST::FunctionDeclarationParam     then FunctionDeclarationParam
  in AST::TypeDeclaration              then TypeDeclaration
  in AST::VariantDeclaration           then VariantDeclaration
  in AST::StructDeclaration            then StructDeclaration
  in AST::ImportDeclaration            then ImportDeclaration
  in AST::InteropImportDeclaration     then InteropImportDeclaration
  in AST::InteropFunction              then InteropFunction
  in AST::InterfaceDeclaration         then InterfaceDeclaration
  in AST::InterfaceFunctionDecl        then InterfaceFunctionDecl
  in AST::Assign                       then Assign
  in AST::Bind                         then Bind
  in AST::Implementation               then Implementation
  in AST::ImplementationFunction       then ImplementationFunction
  in AST::IfThenElse                   then IfThenElse
  in AST::CaseOf                       then CaseOf
  in AST::CaseOfBranch                 then CaseOfBranch
  in AST::Lambda                       then Lambda
  in AST::InfixApplication             then InfixApplication
  in AST::FunctionCall                 then FunctionCall
  in AST::KeyedCall                    then KeyedCall
  in AST::Placeholder                  then Placeholder
  in AST::MemberAccess                 then MemberAccess
  in AST::QualifiedAccess              then QualifiedAccess
  in AST::RecordAccess                 then RecordAccess
  in AST::RecordAccessSugar            then RecordAccessSugar
  in AST::RecordUpdateSugar            then RecordUpdateSugar
  in AST::Grouping                     then Grouping
  in AST::Tuple                        then Tuple
  in AST::List                         then List
  in AST::RecordLiteral                then RecordLiteral
  in AST::RecordUpdate                 then RecordUpdate
  in AST::VariableReference            then VariableReference
  in AST::ConstructorReference         then ConstructorReference
  in AST::CurriedConstructor           then CurriedConstructor
  in AST::CharLiteral                  then CharLiteral
  in AST::Literal                      then Literal
  end
end

#format_delimited(strs, open, close, trailing_comma, indent) ⇒ Object

Generic open/sep/close formatter shared by Tuple and List. Inline when it fits and no trailing-comma hint; multi-line otherwise.



102
103
104
105
106
107
108
109
110
# File 'lib/jade/formatter/helper.rb', line 102

def format_delimited(strs, open, close, trailing_comma, indent)
  inline = "#{open}#{strs.join(', ')}#{close}"
  if trailing_comma || too_long?(inline, indent)
    inner = strs.map { "#{it.then(&and_indent(indent + 1))}," }.join("\n")
    "#{INDENT * indent}#{open}\n#{inner}\n#{INDENT * indent}#{close}"
  else
    inline.then(&and_indent(indent))
  end
end

#format_exposing(node, indent: 0) ⇒ Object



124
125
126
# File 'lib/jade/formatter/helper.rb', line 124

def format_exposing(node, indent: 0)
  Exposing.format(node, indent:)
end

#format_leading_comments(node, indent, source = nil) ⇒ Object

The blank line between blocks is load-bearing: collapsing it turns a section banner into the next declaration's doc comment.



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

def format_leading_comments(node, indent, source = nil)
  return "" if node.leading_comments.empty?

  leading_comment_blocks(node, source)
    .map { |block| block.map { it.value.then(&and_indent(indent)) }.join("\n") }
    .join("\n\n")
    .then { it + "\n" }
end

#format_node(node, indent: 0, source: nil) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/jade/formatter/helper.rb', line 15

def format_node(node, indent: 0, source: nil)
  leading  = format_leading_comments(node, indent, source)
  trailing = format_trailing_comment(node)

  dispatch_for(node)
    .format(node, indent:, source:)
    .then { leading + it + trailing }
end

#format_pattern(node, source: nil) ⇒ Object



112
113
114
# File 'lib/jade/formatter/helper.rb', line 112

def format_pattern(node, source: nil)
  Pattern.format(node, source:)
end

#format_trailing_comment(node) ⇒ Object



94
95
96
97
98
# File 'lib/jade/formatter/helper.rb', line 94

def format_trailing_comment(node)
  return "" if node.trailing_comments.empty?

  " #{node.trailing_comments.first.value}"
end

#format_type(node) ⇒ Object



116
117
118
# File 'lib/jade/formatter/helper.rb', line 116

def format_type(node)
  Type.format(node)
end

#format_type_atom(node) ⇒ Object



120
121
122
# File 'lib/jade/formatter/helper.rb', line 120

def format_type_atom(node)
  Type.format_atom(node)
end

#leading_comment_blocks(node, source) ⇒ Object



90
91
92
# File 'lib/jade/formatter/helper.rb', line 90

def leading_comment_blocks(node, source)
  source ? node.leading_comment_groups(source) : [node.leading_comments]
end

#too_long?(line, indent) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/jade/formatter/helper.rb', line 75

def too_long?(line, indent)
  (INDENT * indent).length + line.length > LINE_LIMIT
end