rufo 0.0.37 → 0.0.38
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -19
- data/lib/rufo/command.rb +32 -19
- data/lib/rufo/formatter.rb +20 -60
- 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: eb5d79057ba5a76559fc6ff7f243a8d490794700
|
4
|
+
data.tar.gz: 1d35a7183d666b212916b860e55f922ef265c6e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0f992361ccd2b36c155d7fa4c157f48be8d877774e0b6b69d2b110412caeae47ee8984c680ae2f8850c3866c311b56a70925cff5dfded4eee22f4f789ddcc59
|
7
|
+
data.tar.gz: 8f6ed9ee0a58328c73fefa468d8832e80b49d656480a582f3fbc7551c8c158f0fe5c1233d683e143cbfbdbc09c2e7d558392b4f48620782fbcd98d89698ec426
|
data/README.md
CHANGED
@@ -53,17 +53,17 @@ this choice.
|
|
53
53
|
Another example is aligning call parameters:
|
54
54
|
|
55
55
|
```ruby
|
56
|
-
# Align with respect to the first parameter
|
56
|
+
# Align with respect to the first parameter:
|
57
57
|
foo 1, 2,
|
58
58
|
3, 4,
|
59
59
|
5
|
60
60
|
|
61
|
-
# Align by regular indent (2 spaces)
|
61
|
+
# Align by regular indent (2 spaces):
|
62
62
|
foo 1, 2,
|
63
63
|
3, 4,
|
64
64
|
5
|
65
65
|
|
66
|
-
# Align arrays
|
66
|
+
# Align arrays:
|
67
67
|
foo 1, [
|
68
68
|
2,
|
69
69
|
3,
|
@@ -75,12 +75,12 @@ foo 1, [
|
|
75
75
|
3,
|
76
76
|
]
|
77
77
|
|
78
|
-
#
|
78
|
+
# Align trailing calls:
|
79
79
|
assert foo(
|
80
80
|
1
|
81
81
|
)
|
82
82
|
|
83
|
-
# Don't extra align trailing calls
|
83
|
+
# Don't extra align trailing calls:
|
84
84
|
assert foo(
|
85
85
|
1
|
86
86
|
)
|
@@ -101,7 +101,8 @@ If Rufo does not change these things by default, what does it do? Well, it makes
|
|
101
101
|
- no trailing whitespace remains
|
102
102
|
- a trailing newline at the end of the file remains
|
103
103
|
|
104
|
-
And of course it can be configured to do more.
|
104
|
+
And of course it can be configured to do much more.
|
105
|
+
Check the [Settings](https://github.com/asterite/rufo/wiki/Settings) section in the [Wiki](https://github.com/asterite/rufo/wiki) for more details.
|
105
106
|
|
106
107
|
## Installation
|
107
108
|
|
@@ -140,7 +141,15 @@ $ rufo --check file names or dir names
|
|
140
141
|
```
|
141
142
|
|
142
143
|
This will print one line for each file that isn't correctly formatted
|
143
|
-
according to **
|
144
|
+
according to **Rufo**, and will exit with exit code 3.
|
145
|
+
|
146
|
+
### Exit codes
|
147
|
+
|
148
|
+
| Code | Result |
|
149
|
+
| ---- | ---- |
|
150
|
+
| `0` | No errors, but also no formatting changes |
|
151
|
+
| `1` | Error. Either `Ripper` could not parse syntax or input file is missing |
|
152
|
+
| `3` | Input changed. Formatted code differs from input |
|
144
153
|
|
145
154
|
## Editor support
|
146
155
|
|
@@ -148,24 +157,23 @@ according to **rufo**, and will exit with exit code 1.
|
|
148
157
|
- Emacs [emacs-rufo](https://github.com/aleandros/emacs-rufo) :construction: or [rufo.el](https://github.com/danielma/rufo.el)
|
149
158
|
- Sublime Text: [sublime-rufo](https://github.com/asterite/sublime-rufo)
|
150
159
|
- Vim: [rufo-vim](https://github.com/splattael/rufo-vim)
|
151
|
-
- Visual Studio Code: [rufo-vscode](https://marketplace.visualstudio.com/items?itemName=siliconsenthil.rufo-vscode)
|
160
|
+
- Visual Studio Code: [rufo-vscode](https://marketplace.visualstudio.com/items?itemName=siliconsenthil.rufo-vscode) or [vscode-rufo](https://marketplace.visualstudio.com/items?itemName=mbessey.vscode-rufo)
|
152
161
|
|
153
162
|
|
154
163
|
Did you write a plugin for your favorite editor? That's great! Let me know about it and
|
155
164
|
I will list it here.
|
156
165
|
|
157
|
-
### Tips for editor
|
166
|
+
### Tips for editor plugin implementors
|
158
167
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
such code.
|
168
|
+
It is especially convenient if your code is automatically _formatted on save_.
|
169
|
+
For example, surrounding a piece of code with `if ... end` will automatically indent
|
170
|
+
the code when you save. Likewise, it will be automatically unindented should you remove it.
|
163
171
|
|
164
172
|
For this to work best, the cursor position must be preserved, otherwise it becomes
|
165
|
-
pretty annoying if the cursor is reset
|
173
|
+
pretty annoying if the cursor is reset to the top of the editor.
|
166
174
|
|
167
175
|
You should compute a diff between the old content and new content
|
168
|
-
and apply the necessary
|
176
|
+
and apply the necessary changes. You can check out how this is done in the
|
169
177
|
[Sublime Text plugin for Rufo](https://github.com/asterite/sublime-rufo):
|
170
178
|
|
171
179
|
- [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)
|
@@ -173,8 +181,8 @@ and apply the necessary editions. You can check out how this is done in the
|
|
173
181
|
|
174
182
|
## Configuration
|
175
183
|
|
176
|
-
To configure Rufo, place a `.rufo` file in your project.
|
177
|
-
|
184
|
+
To configure Rufo, place a `.rufo` file in your project. Then when you format a file or a directory,
|
185
|
+
Rufo will look for a `.rufo` file in that directory or parent directories and apply the configuration.
|
178
186
|
|
179
187
|
The `.rufo` file is a Ruby file that is evaluated in the context of the formatter.
|
180
188
|
The available settings are listed [here](https://github.com/asterite/rufo/wiki/Settings).
|
@@ -191,8 +199,8 @@ To parse and lex, [Ripper](https://ruby-doc.org/stdlib-2.4.0/libdoc/ripper/rdoc/
|
|
191
199
|
|
192
200
|
As a reference, this was implemented in a similar fashion to [Crystal](https://github.com/crystal-lang/crystal)'s formatter.
|
193
201
|
|
194
|
-
And as a side note,
|
195
|
-
|
202
|
+
And as a side note, Rufo has **no dependencies**. To run Rufo's specs you will require [rspec](https://github.com/rspec/rspec), but that's it.
|
203
|
+
This means Rufo loads very fast (no need to read many Ruby files), and since [Ripper](https://ruby-doc.org/stdlib-2.4.0/libdoc/ripper/rdoc/Ripper.html) is mostly written
|
196
204
|
in C (uses Ruby's lexer and parser) it formats files pretty fast too.
|
197
205
|
|
198
206
|
## Development
|
data/lib/rufo/command.rb
CHANGED
@@ -3,6 +3,10 @@
|
|
3
3
|
require "optparse"
|
4
4
|
|
5
5
|
class Rufo::Command
|
6
|
+
CODE_OK = 0
|
7
|
+
CODE_ERROR = 1
|
8
|
+
CODE_CHANGE = 3
|
9
|
+
|
6
10
|
def self.run(argv)
|
7
11
|
want_check, filename_for_dot_rufo = parse_options(argv)
|
8
12
|
new(want_check, filename_for_dot_rufo).run(argv)
|
@@ -15,25 +19,26 @@ class Rufo::Command
|
|
15
19
|
end
|
16
20
|
|
17
21
|
def run(argv)
|
18
|
-
if argv.empty?
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
status_code = if argv.empty?
|
23
|
+
format_stdin
|
24
|
+
else
|
25
|
+
format_args argv
|
26
|
+
end
|
27
|
+
|
28
|
+
exit status_code
|
23
29
|
end
|
24
30
|
|
25
31
|
def format_stdin
|
26
32
|
code = STDIN.read
|
33
|
+
|
27
34
|
result = format(code, @filename_for_dot_rufo || Dir.getwd)
|
28
35
|
|
29
|
-
if
|
30
|
-
|
31
|
-
|
32
|
-
print result
|
33
|
-
end
|
36
|
+
print(result) if !@want_check
|
37
|
+
|
38
|
+
code == result ? CODE_OK : CODE_CHANGE
|
34
39
|
rescue Rufo::SyntaxError
|
35
40
|
STDERR.puts "Error: the given text is not a valid ruby program (it has syntax errors)"
|
36
|
-
|
41
|
+
CODE_ERROR
|
37
42
|
rescue => ex
|
38
43
|
STDERR.puts "You've found a bug!"
|
39
44
|
STDERR.puts "Please report it to https://github.com/asterite/rufo/issues with code that triggers it"
|
@@ -54,13 +59,23 @@ class Rufo::Command
|
|
54
59
|
end
|
55
60
|
end
|
56
61
|
|
62
|
+
return CODE_ERROR if files.empty?
|
63
|
+
|
57
64
|
changed = false
|
65
|
+
syntax_error = false
|
58
66
|
|
59
67
|
files.each do |file|
|
60
|
-
|
68
|
+
result = format_file(file)
|
69
|
+
|
70
|
+
changed |= result == CODE_CHANGE
|
71
|
+
syntax_error |= result == CODE_ERROR
|
61
72
|
end
|
62
73
|
|
63
|
-
|
74
|
+
case
|
75
|
+
when syntax_error then CODE_ERROR
|
76
|
+
when changed then CODE_CHANGE
|
77
|
+
else CODE_OK
|
78
|
+
end
|
64
79
|
end
|
65
80
|
|
66
81
|
def format_file(filename)
|
@@ -72,24 +87,22 @@ class Rufo::Command
|
|
72
87
|
# We ignore syntax errors as these might be template files
|
73
88
|
# with .rb extension
|
74
89
|
STDERR.puts "Error: #{filename} has syntax errors"
|
75
|
-
return
|
90
|
+
return CODE_ERROR
|
76
91
|
end
|
77
92
|
|
78
93
|
if code.force_encoding(result.encoding) != result
|
79
94
|
if @want_check
|
80
|
-
STDERR.puts "
|
95
|
+
STDERR.puts "Formatting #{filename} produced changes"
|
81
96
|
else
|
82
97
|
File.write(filename, result)
|
83
98
|
puts "Format: #{filename}"
|
84
99
|
end
|
85
100
|
|
86
|
-
return
|
101
|
+
return CODE_CHANGE
|
87
102
|
end
|
88
|
-
|
89
|
-
false
|
90
103
|
rescue Rufo::SyntaxError
|
91
104
|
STDERR.puts "Error: the given text in #{filename} is not a valid ruby program (it has syntax errors)"
|
92
|
-
|
105
|
+
CODE_ERROR
|
93
106
|
rescue => ex
|
94
107
|
STDERR.puts "You've found a bug!"
|
95
108
|
STDERR.puts "It happened while trying to format the file #{filename}"
|
data/lib/rufo/formatter.rb
CHANGED
@@ -939,14 +939,7 @@ class Rufo::Formatter
|
|
939
939
|
|
940
940
|
consume_call_dot
|
941
941
|
|
942
|
-
|
943
|
-
|
944
|
-
if newline? || comment?
|
945
|
-
consume_end_of_line
|
946
|
-
write_indent(next_indent)
|
947
|
-
else
|
948
|
-
write_space_using_setting(first_space, @spaces_around_dot)
|
949
|
-
end
|
942
|
+
skip_space_or_newline_using_setting(@spaces_around_dot, next_indent)
|
950
943
|
|
951
944
|
if name == :call
|
952
945
|
# :call means it's .()
|
@@ -1695,17 +1688,7 @@ class Rufo::Formatter
|
|
1695
1688
|
check :on_comma
|
1696
1689
|
write ","
|
1697
1690
|
next_token
|
1698
|
-
|
1699
|
-
first_space = skip_space
|
1700
|
-
|
1701
|
-
if newline? || comment?
|
1702
|
-
indent(base_column || @indent) do
|
1703
|
-
consume_end_of_line(want_multiline: false, first_space: first_space)
|
1704
|
-
write_indent
|
1705
|
-
end
|
1706
|
-
else
|
1707
|
-
write_space_using_setting(first_space, @spaces_after_comma)
|
1708
|
-
end
|
1691
|
+
skip_space_or_newline_using_setting(@spaces_after_comma, base_column || @indent)
|
1709
1692
|
end
|
1710
1693
|
end
|
1711
1694
|
|
@@ -2108,15 +2091,7 @@ class Rufo::Formatter
|
|
2108
2091
|
check :on_comma
|
2109
2092
|
write ","
|
2110
2093
|
next_token
|
2111
|
-
|
2112
|
-
first_space = skip_space
|
2113
|
-
|
2114
|
-
if newline? || comment?
|
2115
|
-
consume_end_of_line
|
2116
|
-
write_indent
|
2117
|
-
else
|
2118
|
-
write_space_using_setting(first_space, @spaces_after_comma)
|
2119
|
-
end
|
2094
|
+
skip_space_or_newline_using_setting(@spaces_after_comma)
|
2120
2095
|
end
|
2121
2096
|
|
2122
2097
|
def visit_array(node)
|
@@ -2365,12 +2340,7 @@ class Rufo::Formatter
|
|
2365
2340
|
end
|
2366
2341
|
end
|
2367
2342
|
|
2368
|
-
|
2369
|
-
if newline? || comment?
|
2370
|
-
skip_space_or_newline
|
2371
|
-
else
|
2372
|
-
write_space_using_setting(first_space, @spaces_inside_array_bracket)
|
2373
|
-
end
|
2343
|
+
skip_space_or_newline_using_setting(@spaces_inside_array_bracket)
|
2374
2344
|
|
2375
2345
|
check :on_rbracket
|
2376
2346
|
write "]"
|
@@ -2407,15 +2377,7 @@ class Rufo::Formatter
|
|
2407
2377
|
|
2408
2378
|
visit receiver
|
2409
2379
|
|
2410
|
-
|
2411
|
-
|
2412
|
-
if newline? || comment?
|
2413
|
-
consume_end_of_line
|
2414
|
-
|
2415
|
-
write_indent(@dot_column || next_indent)
|
2416
|
-
else
|
2417
|
-
write_space_using_setting(first_space, @spaces_around_dot)
|
2418
|
-
end
|
2380
|
+
skip_space_or_newline_using_setting(@spaces_around_dot, @dot_column || next_indent)
|
2419
2381
|
|
2420
2382
|
# Remember dot column
|
2421
2383
|
dot_column = @column
|
@@ -2423,15 +2385,7 @@ class Rufo::Formatter
|
|
2423
2385
|
|
2424
2386
|
consume_call_dot
|
2425
2387
|
|
2426
|
-
|
2427
|
-
|
2428
|
-
if newline? || comment?
|
2429
|
-
consume_end_of_line
|
2430
|
-
write_indent(next_indent)
|
2431
|
-
else
|
2432
|
-
skip_space_or_newline
|
2433
|
-
write_space_using_setting(first_space, @spaces_around_dot)
|
2434
|
-
end
|
2388
|
+
skip_space_or_newline_using_setting(@spaces_around_dot, next_indent)
|
2435
2389
|
|
2436
2390
|
visit name
|
2437
2391
|
|
@@ -2483,14 +2437,8 @@ class Rufo::Formatter
|
|
2483
2437
|
write "->"
|
2484
2438
|
next_token
|
2485
2439
|
|
2486
|
-
|
2487
|
-
|
2488
|
-
elsif @spaces_after_lambda_arrow == :one
|
2489
|
-
skip_space
|
2490
|
-
write_space
|
2491
|
-
else
|
2492
|
-
skip_space_or_newline
|
2493
|
-
end
|
2440
|
+
first_space = skip_space
|
2441
|
+
write_space_using_setting(first_space, @spaces_after_lambda_arrow)
|
2494
2442
|
|
2495
2443
|
if empty_params?(params)
|
2496
2444
|
if current_token_kind == :on_lparen
|
@@ -3481,6 +3429,18 @@ class Rufo::Formatter
|
|
3481
3429
|
end
|
3482
3430
|
end
|
3483
3431
|
|
3432
|
+
def skip_space_or_newline_using_setting(setting, indent_size = @indent)
|
3433
|
+
indent(indent_size) do
|
3434
|
+
first_space = skip_space
|
3435
|
+
if newline? || comment?
|
3436
|
+
consume_end_of_line(want_multiline: false, first_space: first_space)
|
3437
|
+
write_indent
|
3438
|
+
else
|
3439
|
+
write_space_using_setting(first_space, setting)
|
3440
|
+
end
|
3441
|
+
end
|
3442
|
+
end
|
3443
|
+
|
3484
3444
|
def write_line
|
3485
3445
|
@output << "\n"
|
3486
3446
|
@last_was_newline = true
|
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.38
|
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-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|