Class: Prettyrb::Visitor
- Inherits:
-
Object
- Object
- Prettyrb::Visitor
- Includes:
- Document::DSL
- Defined in:
- lib/prettyrb/visitor.rb
Constant Summary collapse
- MAX_LENGTH =
100
- SINGLE_LINE =
"single_line"
- MULTI_LINE =
"multi_line"
- FNAMES =
[ "..", "|", "ˆ", "&", "<=>", "==", "===", "=˜", ">", ">=", "<", "<=", "+", "-", "*", "/", "%", "**", "<<", ">>", "~", "+@", "-@", "[]", "[]=" ]
- VALID_SYMBOLS =
FNAMES + ["!", "!="]
Instance Method Summary collapse
-
#initialize(root_node) ⇒ Visitor
constructor
A new instance of Visitor.
- #output ⇒ Object
- #visit(node) ⇒ Object
Methods included from Document::DSL
#concat, #dedent, #group, #hardline, #if_break, #indent, #join, #softline
Constructor Details
#initialize(root_node) ⇒ Visitor
Returns a new instance of Visitor.
38 39 40 41 |
# File 'lib/prettyrb/visitor.rb', line 38 def initialize(root_node) @output = "" @builder = visit(root_node) end |
Instance Method Details
#output ⇒ Object
43 44 45 |
# File 'lib/prettyrb/visitor.rb', line 43 def output Writer.new(@builder).to_s end |
#visit(node) ⇒ Object
47 48 49 50 51 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 91 92 93 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 |
# File 'lib/prettyrb/visitor.rb', line 47 def visit(node) case node.type when :module body = if node.children[1] concat( indent( hardline, visit(node.children[1]), ), hardline ) end concat( "module ", visit(node.children[0]), body, "end" ) when :sclass body = if node.children[1] concat( indent( hardline, visit(node.children[1]), ), hardline, ) end concat( "class << ", visit(node.children[0]), body, "end" ) when :class inheritance = if node.children[1] concat( " < ", visit(node.children[1]), ) end content = if node.children[2] indent( hardline, visit(node.children[2]), ) end concat( "class ", visit(node.children[0]), inheritance, content, hardline, "end" ) when :if body = if node.body_node concat( indent( hardline, visit(node.body_node), ), ) end elsifs = if node.has_elsif? [hardline] + node.elsif_branches.map do |elsif_branch| concat( "elsif ", visit(elsif_branch.conditions), indent( hardline, visit(elsif_branch.body_node) ), hardline, ) end end else_content = if node.else_branch starting_newline = if !node.has_elsif? hardline end concat( starting_newline, "else", indent( hardline, visit(node.else_branch) ), hardline, ) else hardline end concat( node.unless_node? ? "unless" : "if", " ", visit(node.conditions), body, *elsifs, else_content, "end" ) when :case arguments = if node.children[0] concat( " ", visit(node.children[0]), ) end cases = node.children[1..-1].map do |child| if child && child.type != :when concat( hardline, "else", indent( hardline, visit(child) ), ) elsif child visit child end end concat( "case", arguments, concat(*cases), hardline, "end" ) when :when arguments = node.children[0..-2].compact body = if node.children.last indent( hardline, visit(node.children.last) ) end arguments = if arguments.size > 0 join( separator: ",", parts: arguments.map do |arg| concat(softline, visit(arg)) end ) end concat( hardline, group( "when", if_break(with_break: "", without_break: " "), indent( arguments, ), ), body ) when :const output = [] child = node.children[0] while child&.type == :const || child&.type == :cbase output << child.children[1] child = child.children[0] end (output.reverse + [node.children[1]]).join("::") when :or builder = concat( visit(node.children[0]), " ||", if_break(with_break: "", without_break: " "), softline, visit(node.children[1]), ) if node.parent&.type == :and || node.parent&.type == :or builder else group( indent( builder ) ) end when :and builder = concat( visit(node.children[0]), " &&", if_break(with_break: "", without_break: " "), softline, visit(node.children[1]), ) if node.parent&.type == :and || node.parent&.type == :or builder else group( indent( builder ) ) end when :int, :float node.children[0].to_s when :return if node.children[0] group( "return", if_break(without_break: " ", with_break: ""), indent( softline, visit(node.children[0]), only_when_break: true ) ) else "return" end when :block args = if node.children[1]&.children&.length > 0 concat( " |", visit(node.children[1]), "|", ) end concat( visit(node.children[0]), " do", args, indent( hardline, visit(node.children[2]), ), hardline, "end", ) when :begin needs_parens = (node.parent&.type == :if && node.parent.children[0] == node) || node.parent&.type == :or || node.parent&.type == :and || node.parent&.type == :send if needs_parens concat( "(", *visit_each(node.children), # TODO Split or softline? ")" ) else children = [] node.children.each_with_index do |child, index| children << visit(child) next_child = node.children[index + 1] excluded_types = [:class, :module, :sclass, :def, :defs] if (excluded_types.include?(child.type) && node.children.last != child) || (next_child&.type != child.type && node.children.last != child) children << hardline(count: 2) elsif node.children.last != child children << hardline end end concat(*children) end when :defs args_blocks = visit node.children[2] if node.children[2] body = if node.children[3] concat( indent( hardline, visit(node.children[3]), ), hardline, ) else hardline end concat( "def ", visit(node.children[0]), ".", node.children[1], args_blocks, body, "end" ) when :def args_blocks = visit node.args if node.args body_blocks = visit node.body if node.body body = if node.body concat( indent( hardline, body_blocks, ), hardline, ) else hardline end concat( "def ", # TODO possible break node.name, args_blocks, body, "end", ) when :args if node&.parent&.type == :block group( join( separator: ",", parts: node.children.map(&method(:visit)), ), ) elsif node.children.length > 0 group( "(", softline, join( separator: ",", parts: node.children.map(&method(:visit)), ), softline, ")" ) else nil end when :arg node.children[0] when :masgn concat( visit(node.children[0]), " = ", visit(node.children[-1]) ) when :mlhs if node.parent&.type == :mlhs concat( "(", join(separator: ",", parts: visit_each(node.children)), ")" ) else join(separator: ",", parts: visit_each(node.children)) end when :casgn if !node.children[0].nil? puts "FATAL: FIX CASGN FIRST ARGUMENT" exit 1 end # TODO test softline grouping on right side of `=` group( node.children[1], " = ", # if_break(with_break: "", without_break: " "), # softline, visit(node.children[2]), ) when :lvasgn, :cvasgn, :ivasgn right_blocks = visit node.children[1] if node.children[1] if right_blocks concat( node.children[0].to_s, " = ", # TODO line break for long lines right_blocks, ) else concat( node.children[0].to_s, ) end when :send if node.called_on_heredoc? visit node.target elsif node.array_assignment? equals = if !node.left_hand_mass_assignment? " = " end body = if node.children[3] visit(node.children[3]) end concat( visit(node.target), "[", visit(node.children[2]), "]", # TODO line split equals, body, ) elsif node.array_access? concat( visit(node.target), "[", visit(node.children[2]), "]" ) elsif node.negate? concat( "!", visit(node.target), ) elsif node.negative? concat( "-", visit(node.target), ) elsif node.self_target? body = visit(node.target) if node.target concat( node.method.to_s[0..-2], body, ) elsif node.infix? body = visit(node.children[2]) if node.children[2] group( concat( visit(node.target), " ", node.method, " ", body, ) ) else arguments = if node.arguments.length > 0 concat( "(", indent( join(separator: ",", parts: node.arguments.map { |child| concat(softline, visit(child)) }), only_when_break: true, ), softline, ")", ) end method = if node.left_hand_mass_assignment? node.method.to_s[0...-1] else node.method end if node.target concat( visit(node.target), ".", method, group( arguments, ) ) else concat( method, group( arguments, ) ) end end when :hash if node.children.length > 0 group( "{", if_break(without_break: " ", with_break: ""), indent( join(separator: ",", parts: node.children.map { |child| concat(softline, visit(child)) }), if_break(without_break: " ", with_break: ""), ), softline, "}" ) else "{}" end when :pair if node.children[0].type == :sym concat( visit(node.children[0]), visit(node.children[1]) ) else concat( visit(node.children[0]), " => ", visit(node.children[1]) ) end when :defined? concat( "defined?(", visit(node.children[0]), ")" ) when :and_asgn, :or_asgn operator = if node.type == :and_asgn "&&=" elsif node.type == :or_asgn "||=" end group( visit(node.children[0]), " ", operator, if_break(with_break: "", without_break: " "), softline, visit(node.children[1]) ) when :array if node.parent&.type == :resbody join(separator: ",", parts: visit_each(node.children)) elsif node.children[0]&.type == :splat visit node.children[0] else array_nodes = node.children.each_with_index.map do |child, index| concat(softline, visit(child)) end group( "[", indent( join(separator: ",", parts: array_nodes), ), softline, "]" ) end when :regopt node.children.map(&:to_s).join("") when :regexp content = node.children[0...-1].map do |child| if child.type == :str child.children[0].to_s else visit child end end = if node.children[-1] visit node.children[-1] end if node.percent? concat( "%", node.percent_type, node.start_delimiter, *content, node.end_delimiter, , ) else concat( "/", *content, "/", , ) end when :str, :dstr if node.heredoc? method_calls = if node.parent&.type == :send method_calls = [] parent = node.parent while parent && parent.type == :send && parent.called_on_heredoc? arguments = if parent.arguments.length > 0 concat( "(", join(separator: ",", parts: parent.arguments.map { |child| concat(softline, visit(child)) }), ")", ) end method_calls.push( concat( ".", parent.children[1].to_s, arguments, ) ) parent = parent.parent end concat(*method_calls) end concat( "<<", node.heredoc_type, node.heredoc_identifier, method_calls, hardline(skip_indent: true), node.heredoc_body, node.heredoc_identifier ) elsif node.percent_string? body = node.children.map do |child| if child.is_a?(String) child elsif child.type == :str child.children[0] else visit child end end concat( "%", node.percent_character, node.start_delimiter, *body, node.closing_delimiter, ) else concat( "\"", node.format, "\"", ) end when :alias concat( "alias ", visit(node.children[0]), " ", visit(node.children[1]), ) when :dsym body = node.children.map do |child| if child.string? child.children[0] else concat( "\#{", visit(child), "}", ) end end concat( ":\"", *body, "\"", ) when :sym content = node.children[0].to_s # TODO handle already quoted symbols if !VALID_SYMBOLS.include?(content) && !content.match?(/\A[a-zA-Z_]{1}[a-zA-Z0-9_!?=]*\z/) concat( ":", "'", content, "'", ) else if node.parent&.type == :pair && node.parent.children[0] == node concat( content, ": ", ) else concat( ":", content, ) end end when :kwsplat concat( "**", visit(node.children[0]) ) when :splat concat( "*", visit(node.children[0]) ) when :undef concat( "undef", " ", join(separator: ",", parts: visit_each(node.children)) ) when :forward_args "(...)" when :forwarded_args "..." when :optarg concat( node.children[0], " = ", visit(node.children[1]), ) when :restarg concat( "*", node.children[0], ) when :kwarg concat( node.children[0], ":", ) when :kwoptarg concat( node.children[0], ": ", visit(node.children[1]), ) when :kwrestarg if node.children[0] "**" + node.children[0].to_s else "**" end when :kwnilarg "**nil" when :lvar, :cvar, :ivar, :gvar node.children[0].to_s when :true, :false, :nil, :self, :break node.type.to_s when :cbase "::" when :kwbegin concat( "begin", indent( hardline, visit(node.children[0]) ), hardline, "end" ) when :ensure concat( indent( visit(node.children[0]), ), dedent( hardline, "ensure", ), hardline, visit(node.children[1]), ) when :rescue concat( visit(node.children[0]), dedent( hardline, visit(node.children[1]) ) ) when :resbody args = node.children[0] assignment = node.children[1] body = node.children[2] arguments = if args concat( " ", visit(args), ) end argument_assignment = if assignment concat( " => ", visit(assignment) ) end body = if body visit(body) end concat( "rescue", arguments, argument_assignment, indent( hardline, body, ) ) when :while args = if node.children[0] concat( " ", visit(node.children[0]) ) end body = if node.children[1] indent( hardline, visit(node.children[1]), ) end concat( "while", args, body, hardline, "end", ) when :csend concat( visit(node.children[0]), "&.", # TODO softbreak node.children[1] ) when :erange right_side = visit(node.children[1]) if node.children[1] concat( visit(node.children[0]), "...", right_side ) when :irange right_side = visit(node.children[1]) if node.children[1] concat( visit(node.children[0]), "..", right_side ) when :nth_ref concat( "$", node.children[0].to_s, ) when :super concat( "super(", *visit_each(node.children), ")" ) when :next body = if node.children[0] concat(" ", visit(node.children[0])) end concat( "next", body ) when :zsuper "super" when :block_pass concat("&", visit(node.children[0])) else raise "Unexpected node type: #{node.type}" end end |