Class: SyntaxTree::Formatter
- Inherits:
-
PrettierPrint
- Object
- PrettierPrint
- SyntaxTree::Formatter
- Defined in:
- lib/syntax_tree/formatter.rb,
lib/syntax_tree/plugin/single_quotes.rb,
lib/syntax_tree/plugin/trailing_comma.rb
Overview
A slightly enhanced PP that knows how to format recursively including comments.
Defined Under Namespace
Classes: Options, SemanticVersion
Constant Summary collapse
- COMMENT_PRIORITY =
1- HEREDOC_PRIORITY =
2- SINGLE_QUOTES =
true- TRAILING_COMMA =
true
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.
-
#target_ruby_version ⇒ Object
readonly
These options are overridden in plugins to we need to make sure they are available here.
-
#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
- #grandparent ⇒ Object
-
#group ⇒ Object
This is a simplified version of prettyprint’s group.
-
#initialize(source, *args, options: Options.new) ⇒ Formatter
constructor
A new instance of Formatter.
- #parent ⇒ Object
- #parents ⇒ Object
-
#seplist(list, sep = nil, iter_method = :each) ⇒ Object
A similar version to the super, except that it calls back into the separator proc with the instance of ‘self`.
-
#text(string) ⇒ Object
This is a much simplified version of prettyprint’s text.
Constructor Details
#initialize(source, *args, options: Options.new) ⇒ Formatter
Returns a new instance of Formatter.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/syntax_tree/formatter.rb', line 75 def initialize(source, *args, options: Options.new) super(*args) @source = source @stack = [] # Memoizing these values to make access faster. @quote = .quote @trailing_comma = .trailing_comma @target_ruby_version = .target_ruby_version end |
Instance Attribute Details
#quote ⇒ Object (readonly)
These options are overridden in plugins to we need to make sure they are available here.
72 73 74 |
# File 'lib/syntax_tree/formatter.rb', line 72 def quote @quote end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
68 69 70 |
# File 'lib/syntax_tree/formatter.rb', line 68 def source @source end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
68 69 70 |
# File 'lib/syntax_tree/formatter.rb', line 68 def stack @stack end |
#target_ruby_version ⇒ Object (readonly)
These options are overridden in plugins to we need to make sure they are available here.
72 73 74 |
# File 'lib/syntax_tree/formatter.rb', line 72 def target_ruby_version @target_ruby_version 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.
72 73 74 |
# File 'lib/syntax_tree/formatter.rb', line 72 def trailing_comma @trailing_comma end |
Class Method Details
.format(source, node) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/syntax_tree/formatter.rb', line 87 def self.format(source, node) q = new(source, []) q.format(node) q.flush q.output.join end |
Instance Method Details
#format(node, stackable: true) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/syntax_tree/formatter.rb', line 94 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? trailing = [] last_leading = nil # First, we're going to print all of the comments that were found before # the node. We'll also gather up any trailing comments that we find. node.comments.each do |comment| if comment.leading? comment.format(self) breakable(force: true) last_leading = comment else trailing << comment end 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 last_leading&.ignore? range = source[node.location.start_char...node.location.end_char] first = true range.each_line(chomp: true) do |line| if first first = false else breakable_return end text(line) end breakable_return if range.end_with?("\n") 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
154 155 156 |
# File 'lib/syntax_tree/formatter.rb', line 154 def format_each(nodes) nodes.each { |node| format(node) } end |
#grandparent ⇒ Object
158 159 160 |
# File 'lib/syntax_tree/formatter.rb', line 158 def grandparent stack[-3] end |
#group ⇒ Object
This is a simplified version of prettyprint’s group. It doesn’t provide any of the more advanced options because we don’t need them and they take up expensive computation time.
173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/syntax_tree/formatter.rb', line 173 def group contents = [] doc = Group.new(0, contents: contents) groups << doc target << doc with_target(contents) { yield } groups.pop doc end |
#parent ⇒ Object
162 163 164 |
# File 'lib/syntax_tree/formatter.rb', line 162 def parent stack[-2] end |
#parents ⇒ Object
166 167 168 |
# File 'lib/syntax_tree/formatter.rb', line 166 def parents stack[0...-1].reverse_each end |
#seplist(list, sep = nil, iter_method = :each) ⇒ Object
A similar version to the super, except that it calls back into the separator proc with the instance of ‘self`.
187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/syntax_tree/formatter.rb', line 187 def seplist(list, sep = nil, iter_method = :each) first = true list.__send__(iter_method) do |*v| if first first = false elsif sep sep.call(self) else comma_breakable end yield(*v) end end |
#text(string) ⇒ Object
This is a much simplified version of prettyprint’s text. It avoids calculating width by pushing the string directly onto the target.
203 204 205 |
# File 'lib/syntax_tree/formatter.rb', line 203 def text(string) target << string end |