Class: SyntaxTree::Formatter
- Inherits:
-
PrettierPrint
- Object
- PrettierPrint
- SyntaxTree::Formatter
- Includes:
- SingleQuotes, TrailingComma
- Defined in:
- lib/syntax_tree/formatter.rb,
lib/syntax_tree/formatter/single_quotes.rb,
lib/syntax_tree/formatter/trailing_comma.rb
Overview
A slightly enhanced PP that knows how to format recursively including comments.
Defined Under Namespace
Modules: SingleQuotes, TrailingComma
Constant Summary collapse
- COMMENT_PRIORITY =
1- HEREDOC_PRIORITY =
2
Instance Attribute Summary collapse
-
#quote ⇒ Object
readonly
These options are overridden in plugins to we need to make sure they are available here.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
-
#trailing_comma ⇒ Object
(also: #trailing_comma?)
readonly
These options are overridden in plugins to we need to make sure they are available here.
Class Method Summary collapse
Instance Method Summary collapse
- #format(node, stackable: true) ⇒ Object
- #format_each(nodes) ⇒ Object
-
#initialize(source) ⇒ Formatter
constructor
A new instance of Formatter.
- #parent ⇒ Object
- #parents ⇒ Object
Constructor Details
#initialize(source) ⇒ Formatter
Returns a new instance of Formatter.
17 18 19 20 21 22 23 24 |
# File 'lib/syntax_tree/formatter.rb', line 17 def initialize(source, ...) super(...) @source = source @stack = [] @quote = "\"" @trailing_comma = false end |
Instance Attribute Details
#quote ⇒ Object (readonly)
These options are overridden in plugins to we need to make sure they are available here.
14 15 16 |
# File 'lib/syntax_tree/formatter.rb', line 14 def quote @quote end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
10 11 12 |
# File 'lib/syntax_tree/formatter.rb', line 10 def source @source end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
10 11 12 |
# File 'lib/syntax_tree/formatter.rb', line 10 def stack @stack end |
#trailing_comma ⇒ Object (readonly) Also known as: trailing_comma?
These options are overridden in plugins to we need to make sure they are available here.
14 15 16 |
# File 'lib/syntax_tree/formatter.rb', line 14 def trailing_comma @trailing_comma end |
Class Method Details
.format(source, node) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/syntax_tree/formatter.rb', line 26 def self.format(source, node) formatter = new(source, []) node.format(formatter) formatter.flush formatter.output.join end |
Instance Method Details
#format(node, stackable: true) ⇒ Object
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 67 68 69 70 71 |
# File 'lib/syntax_tree/formatter.rb', line 33 def format(node, stackable: true) stack << node if stackable doc = nil # If there are comments, then we're going to format them around the node # so that they get printed properly. if node.comments.any? leading, trailing = node.comments.partition(&:leading?) # Print all comments that were found before the node. leading.each do |comment| comment.format(self) breakable(force: true) end # If the node has a stree-ignore comment right before it, then we're # going to just print out the node as it was seen in the source. doc = if leading.last&.ignore? text(source[node.location.start_char...node.location.end_char]) else node.format(self) end # Print all comments that were found after the node. trailing.each do |comment| line_suffix(priority: COMMENT_PRIORITY) do comment.inline? ? text(" ") : breakable comment.format(self) break_parent end end else doc = node.format(self) end stack.pop if stackable doc end |
#format_each(nodes) ⇒ Object
73 74 75 |
# File 'lib/syntax_tree/formatter.rb', line 73 def format_each(nodes) nodes.each { |node| format(node) } end |
#parent ⇒ Object
77 78 79 |
# File 'lib/syntax_tree/formatter.rb', line 77 def parent stack[-2] end |
#parents ⇒ Object
81 82 83 |
# File 'lib/syntax_tree/formatter.rb', line 81 def parents stack[0...-1].reverse_each end |