rufo 0.0.33 → 0.0.34

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3fd395a366e561d4a5196bd66a89e84488ca699
4
- data.tar.gz: b1e5f7872f193254a8287a0778a9c6a747c4532a
3
+ metadata.gz: d722fed549e79c6f97a6ec76c94574aa7b644520
4
+ data.tar.gz: d4f1a92c881618644dd8c8b2aa71b6261b969014
5
5
  SHA512:
6
- metadata.gz: abef0e942b850cbfbd21a032f63dd1f7a9d487ae4d1dd2fbc071232f7ff434f5dddb855faba972773e6d0a1747e15d59ab11e85e3920c838d5c2afb4b7135498
7
- data.tar.gz: 701c57b9d9bdca0a1e5bd121f6abd33b1ecff4cdeb6c1449905bd77d3064fc4facdb2381d472043b25a09e898be12972ab89ea016e7010bb35ff398f6dca1db9
6
+ metadata.gz: c18dfd73b6ce027e04402131ec960ec4162b8d0a7b8ba91f70c7793ad5c2e17f1c847435d37ab5226580c358383a9c9eb507b6bbfa0551d4ce73a079e28fe004
7
+ data.tar.gz: fda9a82998eea248c95efa63523ae67931b582acb12bcd9a3ef1023a8e58b8b36246a8a9235c50e08343ca27b5e455390ed540f7d158db46db910c6c9bc6554b
@@ -3,6 +3,8 @@ language: ruby
3
3
  cache: bundler
4
4
  rvm:
5
5
  - 2.4.1
6
+ - 2.3.4
7
+ - 2.2.7
6
8
  before_install: gem install bundler -v 1.15.1
7
9
  script:
8
10
  - bundle exec rake ci
data/README.md CHANGED
@@ -98,6 +98,8 @@ If Rufo does not change these things by default, what does it do? Well, it makes
98
98
  - there are no more than one consecutive empty lines
99
99
  - methods are separated by an empty line
100
100
  - no trailing semicolons remain
101
+ - no trailing whitespace remains
102
+ - a trailing newline at the end of the file remains
101
103
 
102
104
  And of course it can be configured to do more. Check the settings section below.
103
105
 
@@ -143,13 +145,31 @@ according to **rufo**, and will exit with exit code 1.
143
145
  ## Editor support
144
146
 
145
147
  - Sublime Text: [sublime-rufo](https://github.com/asterite/sublime-rufo)
148
+ - Visual Studio Code: [rufo-vscode](https://marketplace.visualstudio.com/items?itemName=siliconsenthil.rufo-vscode)
146
149
  - Vim: [rufo-vim](https://github.com/splattael/rufo-vim) :construction:
147
150
  - Atom: [rufo-atom](https://github.com/bmulvihill/rufo-atom) :construction:
148
- - Emacs [emacs-rufo](https://github.com/aleandros/emacs-rufo) :construction:
151
+ - Emacs [emacs-rufo](https://github.com/aleandros/emacs-rufo) :construction: or [rufo-mode.el](https://github.com/danielma/rufo-mode.el) :construction:
149
152
 
150
153
  Did you write a plugin for your favorite editor? That's great! Let me know about it and
151
154
  I will list it here.
152
155
 
156
+ ### Tips for editor plugins implementors
157
+
158
+ Rufo works best, and it's incredibly convenient, when code is automatically
159
+ formatted on save. In this way you can for example surround a piece of code with
160
+ `if ... end` and it gets automatically indented, or unindented when you remove
161
+ such code.
162
+
163
+ For this to work best, the cursor position must be preserved, otherwise it becomes
164
+ pretty annoying if the cursor is reset at the top of the editor.
165
+
166
+ You should compute a diff between the old content and new content
167
+ and apply the necessary editions. You can check out how this is done in the
168
+ [Sublime Text plugin for Rufo](https://github.com/asterite/sublime-rufo):
169
+
170
+ - [diff_match_patch.py](https://github.com/asterite/sublime-rufo/blob/master/diff_match_patch.py) contains the diff algorithm (you can port it to other languages or try to search for a similar algorithm online)
171
+ - [rufo_format.py](https://github.com/asterite/sublime-rufo/blob/master/rufo_format.py#L46-L53) consumes the diff result and applies it chunk by chunk in the editor's view
172
+
153
173
  ## Configuration
154
174
 
155
175
  To configure Rufo, place a `.rufo` file in your project. When formatting a file or a directory
@@ -585,13 +605,13 @@ Given this code:
585
605
 
586
606
  ```ruby
587
607
  class Foo
588
- private
608
+ private
589
609
 
590
- def foo
591
- end
610
+ def foo
611
+ end
592
612
 
593
- def bar
594
- end
613
+ def bar
614
+ end
595
615
  end
596
616
 
597
617
  class Bar
@@ -609,10 +629,10 @@ With `:dynamic`, the formatter will change it to:
609
629
 
610
630
  ```ruby
611
631
  class Foo
612
- private
632
+ private
613
633
 
614
- def foo
615
- end
634
+ def foo
635
+ end
616
636
 
617
637
  def bar
618
638
  end
@@ -624,8 +644,8 @@ class Bar
624
644
  def foo
625
645
  end
626
646
 
627
- def bar
628
- end
647
+ def bar
648
+ end
629
649
  end
630
650
  ```
631
651
 
@@ -637,10 +657,10 @@ With `:align`, the formatter will change it to:
637
657
 
638
658
  ```ruby
639
659
  class Foo
640
- private
660
+ private
641
661
 
642
- def foo
643
- end
662
+ def foo
663
+ end
644
664
 
645
665
  def bar
646
666
  end
@@ -661,23 +681,23 @@ With `:indent`, the formatter will change it to:
661
681
 
662
682
  ```ruby
663
683
  class Foo
664
- private
684
+ private
665
685
 
666
- def foo
667
- end
686
+ def foo
687
+ end
668
688
 
669
- def bar
670
- end
689
+ def bar
690
+ end
671
691
  end
672
692
 
673
693
  class Bar
674
694
  private
675
695
 
676
- def foo
677
- end
696
+ def foo
697
+ end
678
698
 
679
- def bar
680
- end
699
+ def bar
700
+ end
681
701
  end
682
702
  ```
683
703
 
@@ -685,8 +705,8 @@ end
685
705
 
686
706
  ```ruby
687
707
  class Foo
688
- def foo
689
- end
708
+ def foo
709
+ end
690
710
 
691
711
  private
692
712
 
@@ -754,13 +774,13 @@ Given this code:
754
774
 
755
775
  ```ruby
756
776
  {
757
- foo: 1,
758
- barbaz: 2,
777
+ foo: 1,
778
+ barbaz: 2,
759
779
  }
760
780
 
761
781
  {
762
- :foo => 1,
763
- :barbaz => 2,
782
+ :foo => 1,
783
+ :barbaz => 2,
764
784
  }
765
785
 
766
786
  method foo: 1,
@@ -771,13 +791,13 @@ With `true`, the formatter will change it to:
771
791
 
772
792
  ```ruby
773
793
  {
774
- foo: 1,
775
- barbaz: 2,
794
+ foo: 1,
795
+ barbaz: 2,
776
796
  }
777
797
 
778
798
  {
779
- :foo => 1,
780
- :barbaz => 2,
799
+ :foo => 1,
800
+ :barbaz => 2,
781
801
  }
782
802
 
783
803
  method foo: 1,
@@ -856,33 +876,33 @@ Given this code:
856
876
 
857
877
  ```ruby
858
878
  [
859
- 1,
860
- 2
879
+ 1,
880
+ 2
861
881
  ]
862
882
 
863
883
  [
864
- 1,
865
- 2,
884
+ 1,
885
+ 2,
866
886
  ]
867
887
 
868
888
  {
869
- foo: 1,
870
- bar: 2
889
+ foo: 1,
890
+ bar: 2
871
891
  }
872
892
 
873
893
  {
874
- foo: 1,
875
- bar: 2,
894
+ foo: 1,
895
+ bar: 2,
876
896
  }
877
897
 
878
898
  foo(
879
- x: 1,
880
- y: 2
899
+ x: 1,
900
+ y: 2
881
901
  )
882
902
 
883
903
  foo(
884
- x: 1,
885
- y: 2,
904
+ x: 1,
905
+ y: 2,
886
906
  )
887
907
  ```
888
908
 
@@ -890,68 +910,68 @@ With `:always`, the formatter will change it to:
890
910
 
891
911
  ```ruby
892
912
  [
893
- 1,
894
- 2,
913
+ 1,
914
+ 2,
895
915
  ]
896
916
 
897
917
  [
898
- 1,
899
- 2,
918
+ 1,
919
+ 2,
900
920
  ]
901
921
 
902
922
  {
903
- foo: 1,
904
- bar: 2,
923
+ foo: 1,
924
+ bar: 2,
905
925
  }
906
926
 
907
927
  {
908
- foo: 1,
909
- bar: 2,
928
+ foo: 1,
929
+ bar: 2,
910
930
  }
911
931
 
912
932
  foo(
913
- x: 1,
914
- y: 2,
933
+ x: 1,
934
+ y: 2,
915
935
  )
916
936
 
917
937
  foo(
918
- x: 1,
919
- y: 2,
938
+ x: 1,
939
+ y: 2,
920
940
  )
921
941
  ```
922
942
  With `:never`, the formatter will change it to:
923
943
 
924
944
  ```ruby
925
945
  [
926
- 1,
927
- 2
946
+ 1,
947
+ 2
928
948
  ]
929
949
 
930
950
  [
931
- 1,
932
- 2
951
+ 1,
952
+ 2
933
953
  ]
934
954
 
935
955
  {
936
- foo: 1,
937
- bar: 2
956
+ foo: 1,
957
+ bar: 2
938
958
  }
939
959
 
940
960
  {
941
- foo: 1,
942
- bar: 2
961
+ foo: 1,
962
+ bar: 2
943
963
  }
944
964
 
945
965
  foo(
946
- x: 1,
947
- y: 2
966
+ x: 1,
967
+ y: 2
948
968
  )
949
969
 
950
970
  foo(
951
- x: 1,
952
- y: 2
971
+ x: 1,
972
+ y: 2
953
973
  )
954
- ``
974
+ ```
955
975
 
956
976
  With `:dynamic` it won't modify it.
957
977
 
@@ -44,7 +44,7 @@ class Rufo::Command
44
44
 
45
45
  args.each do |arg|
46
46
  if Dir.exist?(arg)
47
- files.concat Dir["#{arg}/**/*.rb"].select(&File.method(:file?))
47
+ files.concat Dir[File.join(arg, '**', '*.rb')].select(&File.method(:file?))
48
48
  elsif File.exist?(arg)
49
49
  files << arg
50
50
  else
@@ -73,7 +73,7 @@ class Rufo::Command
73
73
  return true
74
74
  end
75
75
 
76
- if code != result
76
+ if code.force_encoding(result.encoding) != result
77
77
  if @want_check
78
78
  STDERR.puts "Error: formatting #{filename} produced changes"
79
79
  else
@@ -100,8 +100,9 @@ class Rufo::Formatter
100
100
  # Do we want to compute the above?
101
101
  @want_first_token_in_line = false
102
102
 
103
- # Each line that belongs to a heredoc content is put here
104
- @heredoc_lines = {}
103
+ # Each line that belongs to a string literal besides the first
104
+ # go here, so we don't break them when indenting/dedenting stuff
105
+ @unmodifiable_string_lines = {}
105
106
 
106
107
  # Position of comments that occur at the end of a line
107
108
  @comments_positions = []
@@ -721,12 +722,18 @@ class Rufo::Formatter
721
722
  inner = inner[1..-1] unless node[0] == :xstring_literal
722
723
  visit_exps(inner, with_lines: false)
723
724
 
725
+ # Every line between the first line and end line of this
726
+ # string (excluding the first line) must remain like it is
727
+ # now (we don't want to mess with that when indenting/dedenting)
728
+ #
729
+ # This can happen with heredocs, but also with string literals
730
+ # spanning multiple lines.
731
+ (line + 1..@line).each do |i|
732
+ @unmodifiable_string_lines[i] = true
733
+ end
734
+
724
735
  case current_token_kind
725
736
  when :on_heredoc_end
726
- (line + 1..@line).each do |i|
727
- @heredoc_lines[i] = true
728
- end
729
-
730
737
  heredoc, tilde = @current_heredoc
731
738
  if heredoc && tilde
732
739
  write_indent
@@ -751,13 +758,27 @@ class Rufo::Formatter
751
758
  # [:string_concat, string1, string2]
752
759
  _, string1, string2 = node
753
760
 
761
+ token_column = current_token_column
762
+ base_column = @column
763
+
754
764
  visit string1
755
765
 
756
766
  has_backslash, first_space = skip_space_backslash
757
767
  if has_backslash
758
768
  write " \\"
759
769
  write_line
760
- write_indent
770
+
771
+ # If the strings are aligned, like in:
772
+ #
773
+ # foo bar, "hello" \
774
+ # "world"
775
+ #
776
+ # then keep it aligned.
777
+ if token_column == current_token_column
778
+ write_indent(base_column)
779
+ else
780
+ write_indent
781
+ end
761
782
  else
762
783
  consume_space
763
784
  end
@@ -900,21 +921,34 @@ class Rufo::Formatter
900
921
  write_space first_space[2]
901
922
  end
902
923
 
903
- # consume_space(want_preserve_whitespace: !@align_assignments)
904
924
  track_assignment
905
925
  consume_op "="
906
926
  visit_assign_value right
907
927
  end
908
928
 
909
929
  def visit_assign_value(value)
930
+ base_column = @column
931
+
910
932
  first_space = current_token if space?
911
- skip_space
933
+ has_slash_newline, _ = skip_space_backslash
934
+
935
+ sticky = indentable_value?(value)
912
936
 
913
- want_space = first_space || @spaces_around_equal == :one
914
- indent_after_space value, sticky: indentable_value?(value),
915
- want_space: want_space,
916
- first_space: first_space,
917
- preserve_whitespace: @spaces_around_equal == :dynamic && !@align_assignments
937
+ # Remove backslash after equal + newline (it's useless)
938
+ if has_slash_newline
939
+ skip_space_or_newline
940
+ write_line
941
+ indent(next_indent) do
942
+ write_indent
943
+ visit(value)
944
+ end
945
+ else
946
+ want_space = first_space || @spaces_around_equal == :one
947
+ indent_after_space value, sticky: sticky,
948
+ want_space: want_space,
949
+ first_space: first_space,
950
+ preserve_whitespace: @spaces_around_equal == :dynamic && !@align_assignments
951
+ end
918
952
  end
919
953
 
920
954
  def indentable_value?(value)
@@ -1855,9 +1889,11 @@ class Rufo::Formatter
1855
1889
  end
1856
1890
 
1857
1891
  consume_op "*"
1858
- skip_space_or_newline
1859
1892
 
1860
- visit star if star
1893
+ if star
1894
+ skip_space_or_newline
1895
+ visit star
1896
+ end
1861
1897
 
1862
1898
  if after && !after.empty?
1863
1899
  write_params_comma
@@ -3396,7 +3432,9 @@ class Rufo::Formatter
3396
3432
  write current_token_value.rstrip
3397
3433
  next_token
3398
3434
  when :on_embdoc_beg
3399
- write_line if multilple_lines
3435
+ if multilple_lines || last == :comment
3436
+ write_line
3437
+ end
3400
3438
 
3401
3439
  consume_embedded_comment
3402
3440
  last = :comment
@@ -3435,7 +3473,7 @@ class Rufo::Formatter
3435
3473
 
3436
3474
  line = current_token_line
3437
3475
 
3438
- write_line
3476
+ write_line unless @output.empty?
3439
3477
  consume_token :on___end__
3440
3478
 
3441
3479
  lines = @code.lines[line..-1]
@@ -3840,7 +3878,7 @@ class Rufo::Formatter
3840
3878
  (first_line + 1..last_line).each do |line|
3841
3879
  @line_to_call_info.delete(line)
3842
3880
 
3843
- next if @heredoc_lines[line]
3881
+ next if @unmodifiable_string_lines[line]
3844
3882
 
3845
3883
  current_line = lines[line]
3846
3884
  current_line = current_line[diff..-1]
@@ -3864,7 +3902,7 @@ class Rufo::Formatter
3864
3902
 
3865
3903
  @literal_indents.each do |first_line, last_line, indent|
3866
3904
  (first_line + 1..last_line).each do |line|
3867
- next if @heredoc_lines[line]
3905
+ next if @unmodifiable_string_lines[line]
3868
3906
 
3869
3907
  current_line = lines[line]
3870
3908
  current_line = "#{" " * indent}#{current_line}"
@@ -1,3 +1,3 @@
1
1
  module Rufo
2
- VERSION = "0.0.33"
2
+ VERSION = "0.0.34"
3
3
  end
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.33
4
+ version: 0.0.34
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-07-02 00:00:00.000000000 Z
11
+ date: 2017-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler