Class: SyntaxTree::FlowControlFormatter
- Inherits:
-
Object
- Object
- SyntaxTree::FlowControlFormatter
- Defined in:
- lib/syntax_tree/node.rb
Overview
Formats either a Break, Next, or Return node.
Instance Attribute Summary collapse
-
#keyword ⇒ Object
readonly
- String
-
the keyword to print.
-
#node ⇒ Object
readonly
- Break | Next | Return
-
the node being formatted.
Instance Method Summary collapse
- #format(q) ⇒ Object
-
#initialize(keyword, node) ⇒ FlowControlFormatter
constructor
A new instance of FlowControlFormatter.
Constructor Details
#initialize(keyword, node) ⇒ FlowControlFormatter
Returns a new instance of FlowControlFormatter.
2185 2186 2187 2188 |
# File 'lib/syntax_tree/node.rb', line 2185 def initialize(keyword, node) @keyword = keyword @node = node end |
Instance Attribute Details
#keyword ⇒ Object (readonly)
- String
-
the keyword to print
2180 2181 2182 |
# File 'lib/syntax_tree/node.rb', line 2180 def keyword @keyword end |
#node ⇒ Object (readonly)
- Break | Next | Return
-
the node being formatted
2183 2184 2185 |
# File 'lib/syntax_tree/node.rb', line 2183 def node @node end |
Instance Method Details
#format(q) ⇒ Object
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 |
# File 'lib/syntax_tree/node.rb', line 2190 def format(q) q.group do q.text(keyword) parts = node.arguments.parts length = parts.length if length == 0 # Here there are no arguments at all, so we're not going to print # anything. This would be like if we had: # # break # elsif length >= 2 # If there are multiple arguments, format them all. If the line is # going to break into multiple, then use brackets to start and end the # expression. format_arguments(q, " [", "]") else # If we get here, then we're formatting a single argument to the flow # control keyword. part = parts.first case part when Paren statements = part.contents.body if statements.length == 1 statement = statements.first if statement.is_a?(ArrayLiteral) contents = statement.contents if contents && contents.parts.length >= 2 # Here we have a single argument that is a set of parentheses # wrapping an array literal that has at least 2 elements. # We're going to print the contents of the array directly. # This would be like if we had: # # break([1, 2, 3]) # # which we will print as: # # break 1, 2, 3 # q.text(" ") format_array_contents(q, statement) else # Here we have a single argument that is a set of parentheses # wrapping an array literal that has 0 or 1 elements. We're # going to skip the parentheses but print the array itself. # This would be like if we had: # # break([1]) # # which we will print as: # # break [1] # q.text(" ") q.format(statement) end elsif skip_parens?(statement) # Here we have a single argument that is a set of parentheses # that themselves contain a single statement. That statement is # a simple value that we can skip the parentheses for. This # would be like if we had: # # break(1) # # which we will print as: # # break 1 # q.text(" ") q.format(statement) else # Here we have a single argument that is a set of parentheses. # We're going to print the parentheses themselves as if they # were the set of arguments. This would be like if we had: # # break(foo.bar) # q.format(part) end else q.format(part) end when ArrayLiteral contents = part.contents if contents && contents.parts.length >= 2 # Here there is a single argument that is an array literal with at # least two elements. We skip directly into the array literal's # elements in order to print the contents. This would be like if # we had: # # break [1, 2, 3] # # which we will print as: # # break 1, 2, 3 # q.text(" ") format_array_contents(q, part) else # Here there is a single argument that is an array literal with 0 # or 1 elements. In this case we're going to print the array as it # is because skipping the brackets would change the remaining. # This would be like if we had: # # break [] # break [1] # q.text(" ") q.format(part) end else # Here there is a single argument that hasn't matched one of our # previous cases. We're going to print the argument as it is. This # would be like if we had: # # break foo # format_arguments(q, "(", ")") end end end end |