rufo 0.0.33 → 0.0.34
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/README.md +90 -70
- data/lib/rufo/command.rb +2 -2
- data/lib/rufo/formatter.rb +58 -20
- 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: d722fed549e79c6f97a6ec76c94574aa7b644520
|
4
|
+
data.tar.gz: d4f1a92c881618644dd8c8b2aa71b6261b969014
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c18dfd73b6ce027e04402131ec960ec4162b8d0a7b8ba91f70c7793ad5c2e17f1c847435d37ab5226580c358383a9c9eb507b6bbfa0551d4ce73a079e28fe004
|
7
|
+
data.tar.gz: fda9a82998eea248c95efa63523ae67931b582acb12bcd9a3ef1023a8e58b8b36246a8a9235c50e08343ca27b5e455390ed540f7d158db46db910c6c9bc6554b
|
data/.travis.yml
CHANGED
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
|
-
|
608
|
+
private
|
589
609
|
|
590
|
-
|
591
|
-
|
610
|
+
def foo
|
611
|
+
end
|
592
612
|
|
593
|
-
|
594
|
-
|
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
|
-
|
632
|
+
private
|
613
633
|
|
614
|
-
|
615
|
-
|
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
|
-
|
628
|
-
|
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
|
-
|
660
|
+
private
|
641
661
|
|
642
|
-
|
643
|
-
|
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
|
-
|
684
|
+
private
|
665
685
|
|
666
|
-
|
667
|
-
|
686
|
+
def foo
|
687
|
+
end
|
668
688
|
|
669
|
-
|
670
|
-
|
689
|
+
def bar
|
690
|
+
end
|
671
691
|
end
|
672
692
|
|
673
693
|
class Bar
|
674
694
|
private
|
675
695
|
|
676
|
-
|
677
|
-
|
696
|
+
def foo
|
697
|
+
end
|
678
698
|
|
679
|
-
|
680
|
-
|
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
|
-
|
689
|
-
|
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
|
-
|
758
|
-
|
777
|
+
foo: 1,
|
778
|
+
barbaz: 2,
|
759
779
|
}
|
760
780
|
|
761
781
|
{
|
762
|
-
|
763
|
-
|
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
|
-
|
775
|
-
|
794
|
+
foo: 1,
|
795
|
+
barbaz: 2,
|
776
796
|
}
|
777
797
|
|
778
798
|
{
|
779
|
-
|
780
|
-
|
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
|
-
|
860
|
-
|
879
|
+
1,
|
880
|
+
2
|
861
881
|
]
|
862
882
|
|
863
883
|
[
|
864
|
-
|
865
|
-
|
884
|
+
1,
|
885
|
+
2,
|
866
886
|
]
|
867
887
|
|
868
888
|
{
|
869
|
-
|
870
|
-
|
889
|
+
foo: 1,
|
890
|
+
bar: 2
|
871
891
|
}
|
872
892
|
|
873
893
|
{
|
874
|
-
|
875
|
-
|
894
|
+
foo: 1,
|
895
|
+
bar: 2,
|
876
896
|
}
|
877
897
|
|
878
898
|
foo(
|
879
|
-
|
880
|
-
|
899
|
+
x: 1,
|
900
|
+
y: 2
|
881
901
|
)
|
882
902
|
|
883
903
|
foo(
|
884
|
-
|
885
|
-
|
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
|
-
|
894
|
-
|
913
|
+
1,
|
914
|
+
2,
|
895
915
|
]
|
896
916
|
|
897
917
|
[
|
898
|
-
|
899
|
-
|
918
|
+
1,
|
919
|
+
2,
|
900
920
|
]
|
901
921
|
|
902
922
|
{
|
903
|
-
|
904
|
-
|
923
|
+
foo: 1,
|
924
|
+
bar: 2,
|
905
925
|
}
|
906
926
|
|
907
927
|
{
|
908
|
-
|
909
|
-
|
928
|
+
foo: 1,
|
929
|
+
bar: 2,
|
910
930
|
}
|
911
931
|
|
912
932
|
foo(
|
913
|
-
|
914
|
-
|
933
|
+
x: 1,
|
934
|
+
y: 2,
|
915
935
|
)
|
916
936
|
|
917
937
|
foo(
|
918
|
-
|
919
|
-
|
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
|
-
|
927
|
-
|
946
|
+
1,
|
947
|
+
2
|
928
948
|
]
|
929
949
|
|
930
950
|
[
|
931
|
-
|
932
|
-
|
951
|
+
1,
|
952
|
+
2
|
933
953
|
]
|
934
954
|
|
935
955
|
{
|
936
|
-
|
937
|
-
|
956
|
+
foo: 1,
|
957
|
+
bar: 2
|
938
958
|
}
|
939
959
|
|
940
960
|
{
|
941
|
-
|
942
|
-
|
961
|
+
foo: 1,
|
962
|
+
bar: 2
|
943
963
|
}
|
944
964
|
|
945
965
|
foo(
|
946
|
-
|
947
|
-
|
966
|
+
x: 1,
|
967
|
+
y: 2
|
948
968
|
)
|
949
969
|
|
950
970
|
foo(
|
951
|
-
|
952
|
-
|
971
|
+
x: 1,
|
972
|
+
y: 2
|
953
973
|
)
|
954
|
-
|
974
|
+
```
|
955
975
|
|
956
976
|
With `:dynamic` it won't modify it.
|
957
977
|
|
data/lib/rufo/command.rb
CHANGED
@@ -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[
|
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
|
data/lib/rufo/formatter.rb
CHANGED
@@ -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
|
104
|
-
|
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
|
-
|
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
|
-
|
933
|
+
has_slash_newline, _ = skip_space_backslash
|
934
|
+
|
935
|
+
sticky = indentable_value?(value)
|
912
936
|
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
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
|
-
|
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
|
-
|
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 @
|
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 @
|
3905
|
+
next if @unmodifiable_string_lines[line]
|
3868
3906
|
|
3869
3907
|
current_line = lines[line]
|
3870
3908
|
current_line = "#{" " * indent}#{current_line}"
|
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.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-
|
11
|
+
date: 2017-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|