Module: Jade::Formatter::Helper
- Included in:
- Jade::Formatter, Assign, Bind, Body, Calls, CaseOf, CaseOfBranch, CharLiteral, ConstructorReference, Exposing, FunctionCall, FunctionDeclaration, FunctionDeclarationParam, Grouping, IfThenElse, Implementation, ImplementationFunction, ImportDeclaration, InfixApplication, InterfaceDeclaration, InterfaceFunctionDecl, InteropFunction, InteropImportDeclaration, KeyedCall, Lambda, List, Literal, MemberAccess, ModuleNode, Pattern, Placeholder, QualifiedAccess, RecordAccess, RecordAccessSugar, RecordLiteral, RecordUpdate, RecordUpdateSugar, StructDeclaration, Tuple, Type, TypeDeclaration, VariableReference, VariantDeclaration
- Defined in:
- lib/jade/formatter/helper.rb
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
- #and_indent(indent) ⇒ Object
- #dispatch_for(node) ⇒ Object
-
#format_delimited(strs, open, close, trailing_comma, indent) ⇒ Object
Generic open/sep/close formatter shared by Tuple and List.
- #format_exposing(node, indent: 0) ⇒ Object
- #format_leading_comments(node, indent) ⇒ Object
- #format_node(node, indent: 0, source: nil) ⇒ Object
- #format_pattern(node, source: nil) ⇒ Object
- #format_trailing_comment(node) ⇒ Object
- #format_type(node) ⇒ Object
- #format_type_atom(node) ⇒ Object
- #too_long?(line, indent) ⇒ Boolean
Instance Method Details
#and_indent(indent) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/jade/formatter/helper.rb', line 67 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 |
# 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::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.
95 96 97 98 99 100 101 102 103 |
# File 'lib/jade/formatter/helper.rb', line 95 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
117 118 119 |
# File 'lib/jade/formatter/helper.rb', line 117 def format_exposing(node, indent: 0) Exposing.format(node, indent:) end |
#format_leading_comments(node, indent) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/jade/formatter/helper.rb', line 78 def format_leading_comments(node, indent) return "" if node.leading_comments.empty? node.leading_comments .map { |tok| tok.value.then(&and_indent(indent)) } .join("\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) trailing = format_trailing_comment(node) dispatch_for(node) .format(node, indent:, source:) .then { leading + it + trailing } end |
#format_pattern(node, source: nil) ⇒ Object
105 106 107 |
# File 'lib/jade/formatter/helper.rb', line 105 def format_pattern(node, source: nil) Pattern.format(node, source:) end |
#format_trailing_comment(node) ⇒ Object
87 88 89 90 91 |
# File 'lib/jade/formatter/helper.rb', line 87 def format_trailing_comment(node) return "" if node.trailing_comments.empty? " #{node.trailing_comments.first.value}" end |
#format_type(node) ⇒ Object
109 110 111 |
# File 'lib/jade/formatter/helper.rb', line 109 def format_type(node) Type.format(node) end |
#format_type_atom(node) ⇒ Object
113 114 115 |
# File 'lib/jade/formatter/helper.rb', line 113 def format_type_atom(node) Type.format_atom(node) end |
#too_long?(line, indent) ⇒ Boolean
74 75 76 |
# File 'lib/jade/formatter/helper.rb', line 74 def too_long?(line, indent) (INDENT * indent).length + line.length > LINE_LIMIT end |