rufo 0.0.22 → 0.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +26 -1
- data/lib/rufo/command.rb +2 -2
- data/lib/rufo/formatter.rb +75 -14
- data/lib/rufo/version.rb +1 -1
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: bd6cf27bd7b2fb2451482dff10c6fe5a3930311e
         | 
| 4 | 
            +
              data.tar.gz: 7a4eebf1d5df0dc1ae9e05f9a04030cef866cdf2
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: bd58dc896ab51bc57c5901fd5d1b46f29784d8f934de72176e21723bf542889520dc1a03f741ca14d2eddf618bbf66522ef758bd4612c1276937d533855d7ced
         | 
| 7 | 
            +
              data.tar.gz: a0e7d72efa0b67fc29bb893eac4be54dfdd3dfb9b90700aaef306e34de7303c947f86c3116cad7b0a3c06357a8eaf4ee786954cb6862e83d2998e429f0c3187c
         | 
    
        data/README.md
    CHANGED
    
    | @@ -49,7 +49,7 @@ according to **rufo**, and will exit with exit code 1. | |
| 49 49 |  | 
| 50 50 | 
             
            ## Configuration
         | 
| 51 51 |  | 
| 52 | 
            -
            Rufo follows most  | 
| 52 | 
            +
            Rufo follows most of the conventions found in this [Ruby style guide](https://github.com/bbatsov/ruby-style-guide). It does a bit more than that, and it can also be configured a bit.
         | 
| 53 53 |  | 
| 54 54 | 
             
            To configure it, place a `.rufo` file in your project. When formatting a file or a directory
         | 
| 55 55 | 
             
            via the `rufo` program, a `.rufo` file will try to be found in that directory or parent directories.
         | 
| @@ -94,6 +94,31 @@ indent_size 2 | |
| 94 94 | 
             
            As time passes there might be more configurations available. Please open an
         | 
| 95 95 | 
             
            issue if you need something else to be configurable.
         | 
| 96 96 |  | 
| 97 | 
            +
            ## Formatting rules
         | 
| 98 | 
            +
             | 
| 99 | 
            +
            Rufo follows most of the conventions found in this [Ruby style guide](https://github.com/bbatsov/ruby-style-guide).
         | 
| 100 | 
            +
             | 
| 101 | 
            +
            However, there are some differences. Some of them are:
         | 
| 102 | 
            +
             | 
| 103 | 
            +
            ### `*`, `/` and `**` don't require spaces around them
         | 
| 104 | 
            +
             | 
| 105 | 
            +
            All of these are good:
         | 
| 106 | 
            +
             | 
| 107 | 
            +
            ```ruby
         | 
| 108 | 
            +
            # First option
         | 
| 109 | 
            +
            2*x + 3*y + z
         | 
| 110 | 
            +
             | 
| 111 | 
            +
            # Second option
         | 
| 112 | 
            +
            2 * x + 3 * y + z
         | 
| 113 | 
            +
             | 
| 114 | 
            +
            # Another option
         | 
| 115 | 
            +
            2 * x + 3*y + z
         | 
| 116 | 
            +
            ```
         | 
| 117 | 
            +
             | 
| 118 | 
            +
            Rufo will leave them as they are. The reason is that the first format looks
         | 
| 119 | 
            +
            good mathematically. If you do insert a space before the `*` operator,
         | 
| 120 | 
            +
            a space will be inserted afterwards.
         | 
| 121 | 
            +
             | 
| 97 122 | 
             
            ## Status
         | 
| 98 123 |  | 
| 99 124 | 
             
            The formatter is able to format `rails` and other projects, so at this point
         | 
    
        data/lib/rufo/command.rb
    CHANGED
    
    | @@ -55,8 +55,7 @@ class Rufo::Command | |
| 55 55 | 
             
                changed = false
         | 
| 56 56 |  | 
| 57 57 | 
             
                files.each do |file|
         | 
| 58 | 
            -
                   | 
| 59 | 
            -
                  changed ||= success
         | 
| 58 | 
            +
                  changed |= format_file file
         | 
| 60 59 | 
             
                end
         | 
| 61 60 |  | 
| 62 61 | 
             
                exit 1 if changed
         | 
| @@ -119,6 +118,7 @@ class Rufo::Command | |
| 119 118 | 
             
                filename_for_dot_rufo = nil
         | 
| 120 119 |  | 
| 121 120 | 
             
                OptionParser.new do |opts|
         | 
| 121 | 
            +
                  opts.version = Rufo::VERSION
         | 
| 122 122 | 
             
                  opts.banner = "Usage: rufo files or dirs [options]"
         | 
| 123 123 |  | 
| 124 124 | 
             
                  opts.on("-c", "--check", "Only check formating changes") do
         | 
    
        data/lib/rufo/formatter.rb
    CHANGED
    
    | @@ -41,14 +41,31 @@ class Rufo::Formatter | |
| 41 41 | 
             
                # Position of comments that occur at the end of a line
         | 
| 42 42 | 
             
                @comments_positions = []
         | 
| 43 43 |  | 
| 44 | 
            +
                # Associate lines to alignments
         | 
| 44 45 | 
             
                # Associate a line to an index inside @comments_position
         | 
| 45 46 | 
             
                # becuase when aligning something to the left of a comment
         | 
| 46 47 | 
             
                # we need to adjust the relative comment
         | 
| 47 | 
            -
                @ | 
| 48 | 
            +
                @line_to_alignments_positions = Hash.new { |h, k| h[k] = [] }
         | 
| 48 49 |  | 
| 49 50 | 
             
                # Position of assignments
         | 
| 50 51 | 
             
                @assignments_positions = []
         | 
| 51 52 |  | 
| 53 | 
            +
                # Range of assignment (line => end_line)
         | 
| 54 | 
            +
                #
         | 
| 55 | 
            +
                # We need this because when we have to format:
         | 
| 56 | 
            +
                #
         | 
| 57 | 
            +
                # ```
         | 
| 58 | 
            +
                # abc = 1
         | 
| 59 | 
            +
                # a = foo bar: 2
         | 
| 60 | 
            +
                #         baz: #
         | 
| 61 | 
            +
                # ```
         | 
| 62 | 
            +
                #
         | 
| 63 | 
            +
                # Because we'll insert two spaces after `a`, this will
         | 
| 64 | 
            +
                # result in a mis-alignment for baz (and possibly other lines
         | 
| 65 | 
            +
                # below it). So, we remember the line ranges of an assignment,
         | 
| 66 | 
            +
                # and once we align the first one we fix the other ones.
         | 
| 67 | 
            +
                @assignments_ranges = {}
         | 
| 68 | 
            +
             | 
| 52 69 | 
             
                # Hash keys positions
         | 
| 53 70 | 
             
                @hash_keys_positions = []
         | 
| 54 71 |  | 
| @@ -613,11 +630,15 @@ class Rufo::Formatter | |
| 613 630 | 
             
                # [:assign, target, value]
         | 
| 614 631 | 
             
                _, target, value = node
         | 
| 615 632 |  | 
| 633 | 
            +
                line = @line
         | 
| 634 | 
            +
             | 
| 616 635 | 
             
                visit target
         | 
| 617 636 | 
             
                consume_space(!@align_assignments)
         | 
| 618 637 | 
             
                track_assignment
         | 
| 619 638 | 
             
                consume_op "="
         | 
| 620 639 | 
             
                visit_assign_value value
         | 
| 640 | 
            +
             | 
| 641 | 
            +
                @assignments_ranges[line] = @line if @line != line
         | 
| 621 642 | 
             
              end
         | 
| 622 643 |  | 
| 623 644 | 
             
              def visit_op_assign(node)
         | 
| @@ -625,6 +646,9 @@ class Rufo::Formatter | |
| 625 646 | 
             
                #
         | 
| 626 647 | 
             
                # [:opassign, target, op, value]
         | 
| 627 648 | 
             
                _, target, op, value = node
         | 
| 649 | 
            +
             | 
| 650 | 
            +
                line = @line
         | 
| 651 | 
            +
             | 
| 628 652 | 
             
                visit target
         | 
| 629 653 | 
             
                consume_space(!@align_assignments)
         | 
| 630 654 |  | 
| @@ -640,6 +664,8 @@ class Rufo::Formatter | |
| 640 664 | 
             
                next_token
         | 
| 641 665 |  | 
| 642 666 | 
             
                visit_assign_value value
         | 
| 667 | 
            +
             | 
| 668 | 
            +
                @assignments_ranges[line] = @line if @line != line
         | 
| 643 669 | 
             
              end
         | 
| 644 670 |  | 
| 645 671 | 
             
              def visit_multiple_assign(node)
         | 
| @@ -686,31 +712,32 @@ class Rufo::Formatter | |
| 686 712 | 
             
              end
         | 
| 687 713 |  | 
| 688 714 | 
             
              def track_comment
         | 
| 689 | 
            -
                @ | 
| 715 | 
            +
                @line_to_alignments_positions[@line] << [:comment, @column, @comments_positions, @comments_positions.size]
         | 
| 690 716 | 
             
                @comments_positions << [@line, @column, 0, nil, 0]
         | 
| 691 717 | 
             
              end
         | 
| 692 718 |  | 
| 693 719 | 
             
              def track_assignment(offset = 0)
         | 
| 694 | 
            -
                track_alignment @assignments_positions, offset
         | 
| 720 | 
            +
                track_alignment :assign, @assignments_positions, offset
         | 
| 695 721 | 
             
              end
         | 
| 696 722 |  | 
| 697 723 | 
             
              def track_hash_key
         | 
| 698 724 | 
             
                return unless @current_hash
         | 
| 699 725 |  | 
| 700 | 
            -
                track_alignment @hash_keys_positions, 0, @current_hash.object_id
         | 
| 726 | 
            +
                track_alignment :hash_key, @hash_keys_positions, 0, @current_hash.object_id
         | 
| 701 727 | 
             
              end
         | 
| 702 728 |  | 
| 703 729 | 
             
              def track_case_when
         | 
| 704 | 
            -
                track_alignment @case_when_positions
         | 
| 730 | 
            +
                track_alignment :case_whem, @case_when_positions
         | 
| 705 731 | 
             
              end
         | 
| 706 732 |  | 
| 707 | 
            -
              def track_alignment(target, offset = 0, id = nil)
         | 
| 733 | 
            +
              def track_alignment(key, target, offset = 0, id = nil)
         | 
| 708 734 | 
             
                last = target.last
         | 
| 709 735 | 
             
                if last && last[0] == @line
         | 
| 710 736 | 
             
                  # Track only the first alignment in a line
         | 
| 711 737 | 
             
                  return
         | 
| 712 738 | 
             
                end
         | 
| 713 739 |  | 
| 740 | 
            +
                @line_to_alignments_positions[@line] << [key, @column, target, target.size]
         | 
| 714 741 | 
             
                target << [@line, @column, @indent, id, offset]
         | 
| 715 742 | 
             
              end
         | 
| 716 743 |  | 
| @@ -885,6 +912,8 @@ class Rufo::Formatter | |
| 885 912 |  | 
| 886 913 | 
             
                  if newline? || comment?
         | 
| 887 914 | 
             
                    if needs_trailing_newline
         | 
| 915 | 
            +
                      write "," unless found_comma
         | 
| 916 | 
            +
             | 
| 888 917 | 
             
                      indent(next_indent) do
         | 
| 889 918 | 
             
                        consume_end_of_line
         | 
| 890 919 | 
             
                      end
         | 
| @@ -894,6 +923,7 @@ class Rufo::Formatter | |
| 894 923 | 
             
                    end
         | 
| 895 924 | 
             
                  else
         | 
| 896 925 | 
             
                    if needs_trailing_newline && !found_comma
         | 
| 926 | 
            +
                      write ","
         | 
| 897 927 | 
             
                      consume_end_of_line
         | 
| 898 928 | 
             
                      write_indent
         | 
| 899 929 | 
             
                    end
         | 
| @@ -2792,6 +2822,14 @@ class Rufo::Formatter | |
| 2792 2822 | 
             
                  consume_end_of_line(false, false, false)
         | 
| 2793 2823 | 
             
                end
         | 
| 2794 2824 |  | 
| 2825 | 
            +
                # A then keyword can appear after a newline after an `if`, `unless`, etc.
         | 
| 2826 | 
            +
                # Since that's a super weird formatting for if, probably way too obsolete
         | 
| 2827 | 
            +
                # by now, we just remove it.
         | 
| 2828 | 
            +
                if keyword?("then")
         | 
| 2829 | 
            +
                  next_token
         | 
| 2830 | 
            +
                  skip_space_or_newline
         | 
| 2831 | 
            +
                end
         | 
| 2832 | 
            +
             | 
| 2795 2833 | 
             
                # If the body is [[:void_stmt]] it's an empty body
         | 
| 2796 2834 | 
             
                # so there's nothing to write
         | 
| 2797 2835 | 
             
                if exps.size == 1 && exps[0][0] == :void_stmt
         | 
| @@ -2998,22 +3036,22 @@ class Rufo::Formatter | |
| 2998 3036 | 
             
              end
         | 
| 2999 3037 |  | 
| 3000 3038 | 
             
              def do_align_comments
         | 
| 3001 | 
            -
                do_align @comments_positions,  | 
| 3039 | 
            +
                do_align @comments_positions, :comment
         | 
| 3002 3040 | 
             
              end
         | 
| 3003 3041 |  | 
| 3004 3042 | 
             
              def do_align_assignments
         | 
| 3005 | 
            -
                do_align @assignments_positions
         | 
| 3043 | 
            +
                do_align @assignments_positions, :assign
         | 
| 3006 3044 | 
             
              end
         | 
| 3007 3045 |  | 
| 3008 3046 | 
             
              def do_align_hash_keys
         | 
| 3009 | 
            -
                do_align @hash_keys_positions,  | 
| 3047 | 
            +
                do_align @hash_keys_positions, :hash_key
         | 
| 3010 3048 | 
             
              end
         | 
| 3011 3049 |  | 
| 3012 3050 | 
             
              def do_align_case_when
         | 
| 3013 | 
            -
                do_align @case_when_positions
         | 
| 3051 | 
            +
                do_align @case_when_positions, :case
         | 
| 3014 3052 | 
             
              end
         | 
| 3015 3053 |  | 
| 3016 | 
            -
              def do_align(elements,  | 
| 3054 | 
            +
              def do_align(elements, scope)
         | 
| 3017 3055 | 
             
                lines = @output.lines
         | 
| 3018 3056 |  | 
| 3019 3057 | 
             
                # Chunk comments that are in consecutive lines
         | 
| @@ -3024,7 +3062,7 @@ class Rufo::Formatter | |
| 3024 3062 | 
             
                chunks.each do |comments|
         | 
| 3025 3063 | 
             
                  next if comments.size == 1
         | 
| 3026 3064 |  | 
| 3027 | 
            -
                  if  | 
| 3065 | 
            +
                  if scope == :hash_key
         | 
| 3028 3066 | 
             
                    # Don't indent successive hash keys if any of them is in turn a hash
         | 
| 3029 3067 | 
             
                    # or array literal that is formatted in separate lines.
         | 
| 3030 3068 | 
             
                    has_brace_newline = comments.any? do |(l, c)|
         | 
| @@ -3050,8 +3088,19 @@ class Rufo::Formatter | |
| 3050 3088 | 
             
                    filler_size = max_column - column
         | 
| 3051 3089 | 
             
                    filler = " " * filler_size
         | 
| 3052 3090 |  | 
| 3053 | 
            -
                     | 
| 3054 | 
            -
             | 
| 3091 | 
            +
                    # Move all lines affected by the assignment shift
         | 
| 3092 | 
            +
                    if scope == :assign && (range = @assignments_ranges[line])
         | 
| 3093 | 
            +
                      (line + 1..range).each do |line_number|
         | 
| 3094 | 
            +
                        lines[line_number] = "#{filler}#{lines[line_number]}"
         | 
| 3095 | 
            +
             | 
| 3096 | 
            +
                        # And move other elements too if applicable
         | 
| 3097 | 
            +
                        adjust_other_alignments scope, line_number, column, filler_size
         | 
| 3098 | 
            +
                      end
         | 
| 3099 | 
            +
                    end
         | 
| 3100 | 
            +
             | 
| 3101 | 
            +
                    # Move comments to the right if a change happened
         | 
| 3102 | 
            +
                    if scope != :comment
         | 
| 3103 | 
            +
                      adjust_other_alignments scope, line, column, filler_size
         | 
| 3055 3104 | 
             
                    end
         | 
| 3056 3105 |  | 
| 3057 3106 | 
             
                    lines[line] = "#{before}#{filler}#{after}"
         | 
| @@ -3061,6 +3110,18 @@ class Rufo::Formatter | |
| 3061 3110 | 
             
                @output = lines.join
         | 
| 3062 3111 | 
             
              end
         | 
| 3063 3112 |  | 
| 3113 | 
            +
              def adjust_other_alignments(scope, line, column, offset)
         | 
| 3114 | 
            +
                adjustments = @line_to_alignments_positions[line]
         | 
| 3115 | 
            +
                return unless adjustments
         | 
| 3116 | 
            +
             | 
| 3117 | 
            +
                adjustments.each do |key, adjustment_column, target, index|
         | 
| 3118 | 
            +
                  next if adjustment_column <= column
         | 
| 3119 | 
            +
                  next if scope == key
         | 
| 3120 | 
            +
             | 
| 3121 | 
            +
                  target[index][1] += offset
         | 
| 3122 | 
            +
                end
         | 
| 3123 | 
            +
              end
         | 
| 3124 | 
            +
             | 
| 3064 3125 | 
             
              def chunk_while(array, &block)
         | 
| 3065 3126 | 
             
                if array.respond_to?(:chunk_while)
         | 
| 3066 3127 | 
             
                  array.chunk_while(&block)
         | 
    
        data/lib/rufo/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: rufo
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.24
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Ary Borenszweig
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: exe
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2017-06- | 
| 11 | 
            +
            date: 2017-06-26 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bundler
         |