Class: SyntaxTree::Formatter
- Inherits:
-
PrettierPrint
- Object
- PrettierPrint
- SyntaxTree::Formatter
- Defined in:
- lib/syntax_tree/formatter.rb
Overview
A slightly enhanced PP that knows how to format recursively including comments.
Constant Summary collapse
- OPTIONS =
We want to minimize as much as possible the number of options that are available in syntax tree. For the most part, if users want non-default formatting, they should override the format methods on the specific nodes themselves. However, because of some history with prettier and the fact that folks have become entrenched in their ways, we decided to provide a small amount of configurability.
Note that we’re keeping this in a global-ish hash instead of just overriding methods on classes so that other plugins can reference this if necessary. For example, the RBS plugin references the quote style.
{ quote: "\"", trailing_comma: false }
- 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, *args, quote: OPTIONS[:quote], trailing_comma: OPTIONS[:trailing_comma]) ⇒ Formatter
constructor
A new instance of Formatter.
- #parent ⇒ Object
- #parents ⇒ Object
Constructor Details
#initialize(source, *args, quote: OPTIONS[:quote], trailing_comma: OPTIONS[:trailing_comma]) ⇒ Formatter
Returns a new instance of Formatter.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/syntax_tree/formatter.rb', line 29 def initialize( source, *args, quote: OPTIONS[:quote], trailing_comma: OPTIONS[:trailing_comma] ) super(*args) @source = source @stack = [] # Memoizing these values per formatter to make access faster. @quote = quote @trailing_comma = trailing_comma end |
Instance Attribute Details
#quote ⇒ Object (readonly)
These options are overridden in plugins to we need to make sure they are available here.
26 27 28 |
# File 'lib/syntax_tree/formatter.rb', line 26 def quote @quote end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
22 23 24 |
# File 'lib/syntax_tree/formatter.rb', line 22 def source @source end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
22 23 24 |
# File 'lib/syntax_tree/formatter.rb', line 22 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.
26 27 28 |
# File 'lib/syntax_tree/formatter.rb', line 26 def trailing_comma @trailing_comma end |
Class Method Details
.format(source, node) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/syntax_tree/formatter.rb', line 45 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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/syntax_tree/formatter.rb', line 52 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
92 93 94 |
# File 'lib/syntax_tree/formatter.rb', line 92 def format_each(nodes) nodes.each { |node| format(node) } end |
#parent ⇒ Object
96 97 98 |
# File 'lib/syntax_tree/formatter.rb', line 96 def parent stack[-2] end |
#parents ⇒ Object
100 101 102 |
# File 'lib/syntax_tree/formatter.rb', line 100 def parents stack[0...-1].reverse_each end |