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.
2445 2446 2447 2448 |
# File 'lib/syntax_tree/node.rb', line 2445 def initialize(keyword, node) @keyword = keyword @node = node end |
Instance Attribute Details
#keyword ⇒ Object (readonly)
- String
-
the keyword to print
2440 2441 2442 |
# File 'lib/syntax_tree/node.rb', line 2440 def keyword @keyword end |
#node ⇒ Object (readonly)
- Break | Next | Return
-
the node being formatted
2443 2444 2445 |
# File 'lib/syntax_tree/node.rb', line 2443 def node @node end |
Instance Method Details
#format(q) ⇒ Object
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 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 |
# File 'lib/syntax_tree/node.rb', line 2450 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 |