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
2372 2373 2374 2375 |
# File 'lib/syntax_tree/node.rb', line 2372 def initialize(keyword, node) @keyword = keyword @node = node end |
Instance Attribute Details
#keyword ⇒ Object (readonly)
- String
-
the keyword to print
2367 2368 2369 |
# File 'lib/syntax_tree/node.rb', line 2367 def keyword @keyword end |
#node ⇒ Object (readonly)
- Break | Next | Return
-
the node being formatted
2370 2371 2372 |
# File 'lib/syntax_tree/node.rb', line 2370 def node @node end |
Instance Method Details
#format(q) ⇒ Object
2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 |
# File 'lib/syntax_tree/node.rb', line 2377 def format(q) # If there are no arguments associated with this flow control, then we can # safely just print the keyword and return. if node.arguments.nil? q.text(keyword) return end 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 |