ruby-beautify 0.95.0 → 0.96.0
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/CONTRIB.md +12 -0
- data/Guardfile +1 -0
- data/WHATSNEW.md +5 -0
- data/bin/rbeautify +25 -171
- data/bin/ruby-beautify +25 -171
- data/lib/ruby-beautify.rb +150 -13
- data/lib/ruby-beautify/version.rb +2 -2
- data/ruby-beautify.gemspec +1 -1
- data/spec/example.rb +5 -0
- data/spec/example_beautified.rb +5 -0
- metadata +15 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13168e4681dafb98dab93fbde6ed6df28bf935fe
|
4
|
+
data.tar.gz: 905e6c45a87f5a0c64d6d959fb01095e35196386
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d8c76c28509bf4da9c6e3078039a707a3f4a5c9acba859ac877bb604877dbaeb73292fed65dfc83f89e0d51168570d20d65458e8c03f5a66e91730d2020a91e
|
7
|
+
data.tar.gz: 01ff2b88dcd164966f9d3778653e009a8a08cdc51f6f51612efbdea6be7bf763c0171f1c70212d9a8d97620d7c06e25e79e3096e5e7e4fdda92485525f12920a
|
data/CONTRIB.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
How to contribute to this project.
|
2
|
+
|
3
|
+
## Thin rules (to be made into sections)
|
4
|
+
|
5
|
+
* Work off the dev branch, not master.
|
6
|
+
* Pull requests should be named something related to PR, `hotfix-XXX` or `feature-YYY`.
|
7
|
+
* Do not edit the `version.rb` or the `gemspec`.
|
8
|
+
* All pull requests have to pass tests on `travis-ci` before I'll consider merging them.
|
9
|
+
|
10
|
+
## Formatting and style
|
11
|
+
|
12
|
+
Run all your code through `ruby-beautify` with default settings before committing.
|
data/Guardfile
CHANGED
data/WHATSNEW.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
## 0.96.0
|
2
|
+
* Rewrote the syntax checker so that it pipes a string through stdout, doesn't need a temp file.
|
3
|
+
* Moved everything into the module to clean up the bin.
|
4
|
+
* @Sir-Irk fixed a bug where else and end end up on the same line.
|
5
|
+
|
1
6
|
## 0.95.0
|
2
7
|
* Merged a quick fix from @pkuykendall to catch block assignments with ||=
|
3
8
|
* Added a required version to the gemspec.
|
data/bin/rbeautify
CHANGED
@@ -3,185 +3,39 @@ require 'ruby-beautify'
|
|
3
3
|
require 'ripper'
|
4
4
|
require 'optparse'
|
5
5
|
|
6
|
+
include RubyBeautify
|
7
|
+
|
6
8
|
Options = OptionParser.new do |opts|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
opts.on("-V", "--version", "Print version") { |version| puts RBeautify::VERSION;exit 0}
|
10
|
+
opts.on("-c", "--indent_count [COUNT]", Integer, "Count of characters to use for indenting. (default: 1)") { |count| @indent_count = count}
|
11
|
+
opts.on("-t", "--tabs", "Use tabs for the indent character (default)") { @indent_token = "\t" }
|
12
|
+
opts.on("-s", "--spaces", "Use spaces for the indent character") { @indent_token = " " }
|
13
|
+
opts.banner = "Usage: print ruby into a pretty format, or break trying."
|
12
14
|
end
|
13
15
|
Options.parse!
|
14
16
|
|
15
|
-
@open_block_start = ["module", "class", "begin", "def", 'if', 'while', 'unless', 'case']
|
16
|
-
@both_block = ["else", "elsif", 'rescue', 'when']
|
17
|
-
@open_block_do = ['do', '{']
|
18
|
-
@close_block = ['end', '}']
|
19
|
-
|
20
|
-
@open_brackets = [:on_lparen, :on_lbracket, :on_lbrace, :on_embexpr_beg]
|
21
|
-
@close_brackets = [:on_rparen, :on_rbracket, :on_rbrace, :on_embexpr_end]
|
22
|
-
@new_lines = [:on_nl, :on_ignored_nl, :on_comment, :on_embdoc_end]
|
23
|
-
|
24
17
|
@indent_token = "\t" unless @indent_token
|
25
18
|
@indent_count = 1 unless @indent_count
|
26
19
|
|
27
|
-
def puts_indented_line(level, string)
|
28
|
-
if string =~ /^\n$/
|
29
|
-
puts
|
30
|
-
else
|
31
|
-
indent = (@indent_token * @indent_count) * level
|
32
|
-
puts "#{indent}#{string.lstrip}"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def pretty(content)
|
37
|
-
begin
|
38
|
-
lex = Ripper.lex(content)
|
39
|
-
rescue
|
40
|
-
exit 255
|
41
|
-
end
|
42
|
-
|
43
|
-
indent_level = 0
|
44
|
-
line_lex = []
|
45
|
-
|
46
|
-
# walk through line tokens
|
47
|
-
lex.each do |token|
|
48
|
-
line_lex << token
|
49
|
-
if @new_lines.include? token[1] # if type of this token is a new line
|
50
|
-
|
51
|
-
# did we just close something? if so, lets bring it down a level.
|
52
|
-
if closing_block?(line_lex) || closing_assignment?(line_lex)
|
53
|
-
indent_level -= 1 if indent_level > 0
|
54
|
-
end
|
55
|
-
|
56
|
-
# print our line, in place.
|
57
|
-
line_string = line_lex.map {|l| l.last}.join
|
58
|
-
puts_indented_line(indent_level, line_string)
|
59
|
-
|
60
|
-
# oh, we opened something did we? lets indent for the next run.
|
61
|
-
if opening_block?(line_lex) || opening_assignment?(line_lex)
|
62
|
-
indent_level += 1
|
63
|
-
end
|
64
|
-
|
65
|
-
line_lex.clear
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
return nil
|
70
|
-
end
|
71
|
-
|
72
|
-
# how many times do we open in this line?
|
73
|
-
def opening_block_count(line_lex)
|
74
|
-
line_lex.select {|l| l[1] == :on_kw && @open_block_do.include?(l[2])}.count
|
75
|
-
end
|
76
|
-
|
77
|
-
# how many times do we close?
|
78
|
-
def closing_block_count(line_lex)
|
79
|
-
line_lex.select {|l| l[1] == :on_kw && @close_block.include?(l[2])}.count
|
80
|
-
end
|
81
|
-
|
82
|
-
# is the first word a key word?
|
83
|
-
def starts_block?(line_lex)
|
84
|
-
return true if contains_block_assignment? line_lex
|
85
|
-
line_lex.each do |x|
|
86
|
-
# search for a first non-space token
|
87
|
-
if not x[1] == :on_sp
|
88
|
-
return x[1] == :on_kw && @open_block_start.include?(x[2])
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
# is the first word one of our 'both' keywords?
|
94
|
-
def both_block?(line_lex)
|
95
|
-
line_lex.each do |x|
|
96
|
-
# search for a first non-space token
|
97
|
-
if not x[1] == :on_sp
|
98
|
-
return x[1] == :on_kw && @both_block.include?(x[2])
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
# kinda complex, we count open/close to determine if we ultimately have a
|
104
|
-
# hanging line. Always true if it's a both_block.
|
105
|
-
def opening_block?(line_lex)
|
106
|
-
return true if both_block? line_lex
|
107
|
-
opens = starts_block?(line_lex) ? 1 : 0
|
108
|
-
opens += opening_block_count line_lex
|
109
|
-
closes = closing_block_count line_lex
|
110
|
-
return false if opens == closes
|
111
|
-
return true if opens > closes
|
112
|
-
end
|
113
|
-
|
114
|
-
# kinda complex, we count open/close to determine if we ultimately have close a
|
115
|
-
# hanging line. Always true if it's a both_block.
|
116
|
-
def closing_block?(line_lex)
|
117
|
-
return true if both_block? line_lex
|
118
|
-
opens = starts_block?(line_lex) ? 1 : 0
|
119
|
-
opens += opening_block_count line_lex
|
120
|
-
closes = closing_block_count line_lex
|
121
|
-
return false if opens == closes
|
122
|
-
return true if opens < closes
|
123
|
-
end
|
124
|
-
|
125
|
-
# count the amount of opening assignments.
|
126
|
-
def opening_assignment_count(line_lex)
|
127
|
-
line_lex.select {|l| @open_brackets.include? l[1]}.count
|
128
|
-
end
|
129
|
-
|
130
|
-
# count the amount of closing assignments.
|
131
|
-
def closing_assignment_count(line_lex)
|
132
|
-
line_lex.select {|l| @close_brackets.include? l[1]}.count
|
133
|
-
end
|
134
|
-
|
135
|
-
# same trick as opening_block
|
136
|
-
def opening_assignment?(line_lex)
|
137
|
-
opens = opening_assignment_count line_lex
|
138
|
-
closes = closing_assignment_count line_lex
|
139
|
-
return false if opens == closes
|
140
|
-
return true if opens > closes
|
141
|
-
end
|
142
|
-
|
143
|
-
# ...
|
144
|
-
def closing_assignment?(line_lex)
|
145
|
-
opens = opening_assignment_count line_lex
|
146
|
-
closes = closing_assignment_count line_lex
|
147
|
-
return false if opens == closes
|
148
|
-
return true if closes > opens
|
149
|
-
end
|
150
|
-
|
151
|
-
# test for assignment from a block
|
152
|
-
def contains_block_assignment?(line_lex)
|
153
|
-
compacted_line = line_lex.reject{|x| x[1] == :on_sp} #remove spaces
|
154
|
-
idx = compacted_line.rindex{|x| ['=', '||='].include? x[2]} #find last equal
|
155
|
-
if idx
|
156
|
-
return @open_block_start.include?(compacted_line[idx+1][2]) #check for if/begin block
|
157
|
-
end
|
158
|
-
return false
|
159
|
-
end
|
160
|
-
|
161
|
-
# is this ruby file valid syntax?
|
162
|
-
def valid_syntax?(file)
|
163
|
-
#ugly but fast, ruby returns stdout if it's ok, and stderr if not.
|
164
|
-
#if we get anything back, we were ok, else we were not.
|
165
|
-
r = `ruby -c #{file} 2> /dev/null`
|
166
|
-
return false if r == ""
|
167
|
-
return true
|
168
|
-
end
|
169
|
-
|
170
20
|
# no argument, assume we want to open STDIN.
|
171
21
|
if ARGV.empty?
|
172
|
-
|
173
|
-
puts pretty content
|
22
|
+
content = $stdin.read
|
174
23
|
else
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
24
|
+
file = ARGV[0]
|
25
|
+
|
26
|
+
if File.exist? file
|
27
|
+
content = open(file).read
|
28
|
+
else
|
29
|
+
puts "No such file: #{file}"
|
30
|
+
exit
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
if content
|
35
|
+
if syntax_ok? content
|
36
|
+
pretty_string content, indent_token: @indent_token, indent_count: @indent_count
|
37
|
+
else
|
38
|
+
puts content
|
39
|
+
exit 127
|
40
|
+
end
|
187
41
|
end
|
data/bin/ruby-beautify
CHANGED
@@ -3,185 +3,39 @@ require 'ruby-beautify'
|
|
3
3
|
require 'ripper'
|
4
4
|
require 'optparse'
|
5
5
|
|
6
|
+
include RubyBeautify
|
7
|
+
|
6
8
|
Options = OptionParser.new do |opts|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
opts.on("-V", "--version", "Print version") { |version| puts RBeautify::VERSION;exit 0}
|
10
|
+
opts.on("-c", "--indent_count [COUNT]", Integer, "Count of characters to use for indenting. (default: 1)") { |count| @indent_count = count}
|
11
|
+
opts.on("-t", "--tabs", "Use tabs for the indent character (default)") { @indent_token = "\t" }
|
12
|
+
opts.on("-s", "--spaces", "Use spaces for the indent character") { @indent_token = " " }
|
13
|
+
opts.banner = "Usage: print ruby into a pretty format, or break trying."
|
12
14
|
end
|
13
15
|
Options.parse!
|
14
16
|
|
15
|
-
@open_block_start = ["module", "class", "begin", "def", 'if', 'while', 'unless', 'case']
|
16
|
-
@both_block = ["else", "elsif", 'rescue', 'when']
|
17
|
-
@open_block_do = ['do', '{']
|
18
|
-
@close_block = ['end', '}']
|
19
|
-
|
20
|
-
@open_brackets = [:on_lparen, :on_lbracket, :on_lbrace, :on_embexpr_beg]
|
21
|
-
@close_brackets = [:on_rparen, :on_rbracket, :on_rbrace, :on_embexpr_end]
|
22
|
-
@new_lines = [:on_nl, :on_ignored_nl, :on_comment, :on_embdoc_end]
|
23
|
-
|
24
17
|
@indent_token = "\t" unless @indent_token
|
25
18
|
@indent_count = 1 unless @indent_count
|
26
19
|
|
27
|
-
def puts_indented_line(level, string)
|
28
|
-
if string =~ /^\n$/
|
29
|
-
puts
|
30
|
-
else
|
31
|
-
indent = (@indent_token * @indent_count) * level
|
32
|
-
puts "#{indent}#{string.lstrip}"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def pretty(content)
|
37
|
-
begin
|
38
|
-
lex = Ripper.lex(content)
|
39
|
-
rescue
|
40
|
-
exit 255
|
41
|
-
end
|
42
|
-
|
43
|
-
indent_level = 0
|
44
|
-
line_lex = []
|
45
|
-
|
46
|
-
# walk through line tokens
|
47
|
-
lex.each do |token|
|
48
|
-
line_lex << token
|
49
|
-
if @new_lines.include? token[1] # if type of this token is a new line
|
50
|
-
|
51
|
-
# did we just close something? if so, lets bring it down a level.
|
52
|
-
if closing_block?(line_lex) || closing_assignment?(line_lex)
|
53
|
-
indent_level -= 1 if indent_level > 0
|
54
|
-
end
|
55
|
-
|
56
|
-
# print our line, in place.
|
57
|
-
line_string = line_lex.map {|l| l.last}.join
|
58
|
-
puts_indented_line(indent_level, line_string)
|
59
|
-
|
60
|
-
# oh, we opened something did we? lets indent for the next run.
|
61
|
-
if opening_block?(line_lex) || opening_assignment?(line_lex)
|
62
|
-
indent_level += 1
|
63
|
-
end
|
64
|
-
|
65
|
-
line_lex.clear
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
return nil
|
70
|
-
end
|
71
|
-
|
72
|
-
# how many times do we open in this line?
|
73
|
-
def opening_block_count(line_lex)
|
74
|
-
line_lex.select {|l| l[1] == :on_kw && @open_block_do.include?(l[2])}.count
|
75
|
-
end
|
76
|
-
|
77
|
-
# how many times do we close?
|
78
|
-
def closing_block_count(line_lex)
|
79
|
-
line_lex.select {|l| l[1] == :on_kw && @close_block.include?(l[2])}.count
|
80
|
-
end
|
81
|
-
|
82
|
-
# is the first word a key word?
|
83
|
-
def starts_block?(line_lex)
|
84
|
-
return true if contains_block_assignment? line_lex
|
85
|
-
line_lex.each do |x|
|
86
|
-
# search for a first non-space token
|
87
|
-
if not x[1] == :on_sp
|
88
|
-
return x[1] == :on_kw && @open_block_start.include?(x[2])
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
# is the first word one of our 'both' keywords?
|
94
|
-
def both_block?(line_lex)
|
95
|
-
line_lex.each do |x|
|
96
|
-
# search for a first non-space token
|
97
|
-
if not x[1] == :on_sp
|
98
|
-
return x[1] == :on_kw && @both_block.include?(x[2])
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
# kinda complex, we count open/close to determine if we ultimately have a
|
104
|
-
# hanging line. Always true if it's a both_block.
|
105
|
-
def opening_block?(line_lex)
|
106
|
-
return true if both_block? line_lex
|
107
|
-
opens = starts_block?(line_lex) ? 1 : 0
|
108
|
-
opens += opening_block_count line_lex
|
109
|
-
closes = closing_block_count line_lex
|
110
|
-
return false if opens == closes
|
111
|
-
return true if opens > closes
|
112
|
-
end
|
113
|
-
|
114
|
-
# kinda complex, we count open/close to determine if we ultimately have close a
|
115
|
-
# hanging line. Always true if it's a both_block.
|
116
|
-
def closing_block?(line_lex)
|
117
|
-
return true if both_block? line_lex
|
118
|
-
opens = starts_block?(line_lex) ? 1 : 0
|
119
|
-
opens += opening_block_count line_lex
|
120
|
-
closes = closing_block_count line_lex
|
121
|
-
return false if opens == closes
|
122
|
-
return true if opens < closes
|
123
|
-
end
|
124
|
-
|
125
|
-
# count the amount of opening assignments.
|
126
|
-
def opening_assignment_count(line_lex)
|
127
|
-
line_lex.select {|l| @open_brackets.include? l[1]}.count
|
128
|
-
end
|
129
|
-
|
130
|
-
# count the amount of closing assignments.
|
131
|
-
def closing_assignment_count(line_lex)
|
132
|
-
line_lex.select {|l| @close_brackets.include? l[1]}.count
|
133
|
-
end
|
134
|
-
|
135
|
-
# same trick as opening_block
|
136
|
-
def opening_assignment?(line_lex)
|
137
|
-
opens = opening_assignment_count line_lex
|
138
|
-
closes = closing_assignment_count line_lex
|
139
|
-
return false if opens == closes
|
140
|
-
return true if opens > closes
|
141
|
-
end
|
142
|
-
|
143
|
-
# ...
|
144
|
-
def closing_assignment?(line_lex)
|
145
|
-
opens = opening_assignment_count line_lex
|
146
|
-
closes = closing_assignment_count line_lex
|
147
|
-
return false if opens == closes
|
148
|
-
return true if closes > opens
|
149
|
-
end
|
150
|
-
|
151
|
-
# test for assignment from a block
|
152
|
-
def contains_block_assignment?(line_lex)
|
153
|
-
compacted_line = line_lex.reject{|x| x[1] == :on_sp} #remove spaces
|
154
|
-
idx = compacted_line.rindex{|x| ['=', '||='].include? x[2]} #find last equal
|
155
|
-
if idx
|
156
|
-
return @open_block_start.include?(compacted_line[idx+1][2]) #check for if/begin block
|
157
|
-
end
|
158
|
-
return false
|
159
|
-
end
|
160
|
-
|
161
|
-
# is this ruby file valid syntax?
|
162
|
-
def valid_syntax?(file)
|
163
|
-
#ugly but fast, ruby returns stdout if it's ok, and stderr if not.
|
164
|
-
#if we get anything back, we were ok, else we were not.
|
165
|
-
r = `ruby -c #{file} 2> /dev/null`
|
166
|
-
return false if r == ""
|
167
|
-
return true
|
168
|
-
end
|
169
|
-
|
170
20
|
# no argument, assume we want to open STDIN.
|
171
21
|
if ARGV.empty?
|
172
|
-
|
173
|
-
puts pretty content
|
22
|
+
content = $stdin.read
|
174
23
|
else
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
24
|
+
file = ARGV[0]
|
25
|
+
|
26
|
+
if File.exist? file
|
27
|
+
content = open(file).read
|
28
|
+
else
|
29
|
+
puts "No such file: #{file}"
|
30
|
+
exit
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
if content
|
35
|
+
if syntax_ok? content
|
36
|
+
pretty_string content, indent_token: @indent_token, indent_count: @indent_count
|
37
|
+
else
|
38
|
+
puts content
|
39
|
+
exit 127
|
40
|
+
end
|
187
41
|
end
|
data/lib/ruby-beautify.rb
CHANGED
@@ -1,20 +1,157 @@
|
|
1
|
-
require
|
1
|
+
require 'open3'
|
2
|
+
require 'ruby-beautify/version'
|
2
3
|
|
3
|
-
module
|
4
|
-
|
5
|
-
dest = ""
|
6
|
-
block = nil
|
4
|
+
module RubyBeautify
|
5
|
+
extend self
|
7
6
|
|
8
|
-
|
9
|
-
|
7
|
+
OPEN_BLOCK_START = ["module", "class", "begin", "def", 'if', 'while', 'unless', 'case']
|
8
|
+
BOTH_BLOCK = ["else", "elsif", 'rescue', 'when']
|
9
|
+
OPEN_BLOCK_DO = ['do', '{']
|
10
|
+
CLOSE_BLOCK = ['end', '}']
|
11
|
+
|
12
|
+
OPEN_BRACKETS = [:on_lparen, :on_lbracket, :on_lbrace, :on_embexpr_beg]
|
13
|
+
CLOSE_BRACKETS = [:on_rparen, :on_rbracket, :on_rbrace, :on_embexpr_end]
|
14
|
+
NEW_LINES = [:on_nl, :on_ignored_nl, :on_comment, :on_embdoc_end]
|
15
|
+
|
16
|
+
# print an indented line. Requires the leve, token, count, and string.
|
17
|
+
def puts_indented_line(level, token = "\t", count = 1, string)
|
18
|
+
if string =~ /^\n$/
|
19
|
+
puts
|
20
|
+
else
|
21
|
+
indent = (token * count) * level
|
22
|
+
puts "#{indent}#{string.lstrip}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def pretty_string(content, indent_token: "\t", indent_count: 1)
|
27
|
+
begin
|
28
|
+
lex = Ripper.lex(content)
|
29
|
+
rescue
|
30
|
+
exit 255
|
31
|
+
end
|
32
|
+
|
33
|
+
indent_level = 0
|
34
|
+
line_lex = []
|
35
|
+
|
36
|
+
# walk through line tokens
|
37
|
+
lex.each do |token|
|
38
|
+
line_lex << token
|
39
|
+
if NEW_LINES.include? token[1] # if type of this token is a new line
|
40
|
+
|
41
|
+
# did we just close something? if so, lets bring it down a level.
|
42
|
+
if closing_block?(line_lex) || closing_assignment?(line_lex)
|
43
|
+
indent_level -= 1 if indent_level > 0
|
44
|
+
end
|
45
|
+
|
46
|
+
# print our line, in place.
|
47
|
+
line_string = line_lex.map {|l| l.last}.join
|
48
|
+
puts_indented_line(indent_level, indent_token, indent_count, line_string)
|
49
|
+
|
50
|
+
# oh, we opened something did we? lets indent for the next run.
|
51
|
+
if opening_block?(line_lex) || opening_assignment?(line_lex)
|
52
|
+
indent_level += 1
|
53
|
+
end
|
54
|
+
|
55
|
+
line_lex.clear
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
return nil
|
60
|
+
end
|
61
|
+
|
62
|
+
# how many times do we open in this line?
|
63
|
+
def opening_block_count(line_lex)
|
64
|
+
line_lex.select {|l| l[1] == :on_kw && OPEN_BLOCK_DO.include?(l[2])}.count
|
65
|
+
end
|
66
|
+
|
67
|
+
# how many times do we close?
|
68
|
+
def closing_block_count(line_lex)
|
69
|
+
line_lex.select {|l| l[1] == :on_kw && CLOSE_BLOCK.include?(l[2])}.count
|
70
|
+
end
|
71
|
+
|
72
|
+
# count the amount of opening assignments.
|
73
|
+
def opening_assignment_count(line_lex)
|
74
|
+
line_lex.select {|l| OPEN_BRACKETS.include? l[1]}.count
|
75
|
+
end
|
76
|
+
|
77
|
+
# count the amount of closing assignments.
|
78
|
+
def closing_assignment_count(line_lex)
|
79
|
+
line_lex.select {|l| CLOSE_BRACKETS.include? l[1]}.count
|
80
|
+
end
|
81
|
+
|
82
|
+
# check the syntax of a string, will pipe it through the ruby bin to see if
|
83
|
+
# it has a valid syntax.
|
84
|
+
def syntax_ok?(string)
|
85
|
+
out, err, status = Open3.capture3("ruby -c -", stdin_data:string )
|
86
|
+
return false unless err.empty?
|
87
|
+
return true
|
88
|
+
end
|
89
|
+
|
90
|
+
# same trick as opening_block
|
91
|
+
def opening_assignment?(line_lex)
|
92
|
+
opens = opening_assignment_count line_lex
|
93
|
+
closes = closing_assignment_count line_lex
|
94
|
+
return false if opens == closes
|
95
|
+
return true if opens > closes
|
96
|
+
end
|
97
|
+
|
98
|
+
# ...
|
99
|
+
def closing_assignment?(line_lex)
|
100
|
+
opens = opening_assignment_count line_lex
|
101
|
+
closes = closing_assignment_count line_lex
|
102
|
+
return false if opens == closes
|
103
|
+
return true if closes > opens
|
104
|
+
end
|
105
|
+
|
106
|
+
# test for assignment from a block
|
107
|
+
def contains_block_assignment?(line_lex)
|
108
|
+
compacted_line = line_lex.reject{|x| x[1] == :on_sp} #remove spaces
|
109
|
+
idx = compacted_line.rindex{|x| ['=', '||='].include? x[2]} #find last equal
|
110
|
+
if idx
|
111
|
+
return OPEN_BLOCK_START.include?(compacted_line[idx+1][2]) #check for if/begin block
|
10
112
|
end
|
113
|
+
return false
|
114
|
+
end
|
11
115
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
116
|
+
# is the first word a key word?
|
117
|
+
def starts_block?(line_lex)
|
118
|
+
return true if contains_block_assignment? line_lex
|
119
|
+
line_lex.each do |x|
|
120
|
+
# search for a first non-space token
|
121
|
+
if not x[1] == :on_sp
|
122
|
+
return x[1] == :on_kw && OPEN_BLOCK_START.include?(x[2])
|
123
|
+
end
|
16
124
|
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# is the first word one of our 'both' keywords?
|
128
|
+
def both_block?(line_lex)
|
129
|
+
line_lex.each do |x|
|
130
|
+
# search for a first non-space token
|
131
|
+
if not x[1] == :on_sp
|
132
|
+
return x[1] == :on_kw && BOTH_BLOCK.include?(x[2])
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# kinda complex, we count open/close to determine if we ultimately have a
|
138
|
+
# hanging line. Always true if it's a both_block.
|
139
|
+
def opening_block?(line_lex)
|
140
|
+
opens = (starts_block?(line_lex) || both_block?(line_lex)) ? 1 : 0
|
141
|
+
opens += opening_block_count line_lex
|
142
|
+
closes = closing_block_count line_lex
|
143
|
+
return false if opens == closes
|
144
|
+
return true if opens > closes
|
145
|
+
end
|
17
146
|
|
18
|
-
|
147
|
+
# kinda complex, we count open/close to determine if we ultimately have close a
|
148
|
+
# hanging line. Always true if it's a both_block.
|
149
|
+
def closing_block?(line_lex)
|
150
|
+
return true if both_block? line_lex
|
151
|
+
opens = starts_block?(line_lex) ? 1 : 0
|
152
|
+
opens += opening_block_count line_lex
|
153
|
+
closes = closing_block_count line_lex
|
154
|
+
return false if opens == closes
|
155
|
+
return true if opens < closes
|
19
156
|
end
|
20
|
-
end
|
157
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = "0.
|
1
|
+
module RubyBeautify
|
2
|
+
VERSION = "0.96.0"
|
3
3
|
end
|
data/ruby-beautify.gemspec
CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
|
|
13
13
|
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
14
|
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
15
15
|
gem.require_paths = ["lib"]
|
16
|
-
gem.version =
|
16
|
+
gem.version = RubyBeautify::VERSION
|
17
17
|
gem.add_development_dependency 'rake'
|
18
18
|
gem.add_development_dependency 'bundler'
|
19
19
|
gem.add_development_dependency 'rspec'
|
data/spec/example.rb
CHANGED
@@ -69,6 +69,11 @@ else
|
|
69
69
|
puts 'not likely.'
|
70
70
|
end
|
71
71
|
|
72
|
+
#Test for different if-statement formatting
|
73
|
+
if 1 > 0 then puts 'something'
|
74
|
+
else puts 'nothing' end
|
75
|
+
puts "This line should stay the same"
|
76
|
+
|
72
77
|
# Test for already indented blocks
|
73
78
|
class There2 < There
|
74
79
|
def m1()
|
data/spec/example_beautified.rb
CHANGED
@@ -69,6 +69,11 @@ else
|
|
69
69
|
puts 'not likely.'
|
70
70
|
end
|
71
71
|
|
72
|
+
#Test for different if-statement formatting
|
73
|
+
if 1 > 0 then puts 'something'
|
74
|
+
else puts 'nothing' end
|
75
|
+
puts "This line should stay the same"
|
76
|
+
|
72
77
|
# Test for already indented blocks
|
73
78
|
class There2 < There
|
74
79
|
def m1()
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-beautify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.96.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ernie Brodeur
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: a cli tool (and module) to beautify ruby code.
|
@@ -60,9 +60,10 @@ executables:
|
|
60
60
|
extensions: []
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
|
-
-
|
64
|
-
-
|
65
|
-
-
|
63
|
+
- .gitignore
|
64
|
+
- .rspec
|
65
|
+
- .travis.yml
|
66
|
+
- CONTRIB.md
|
66
67
|
- Gemfile
|
67
68
|
- Guardfile
|
68
69
|
- LICENSE
|
@@ -87,17 +88,17 @@ require_paths:
|
|
87
88
|
- lib
|
88
89
|
required_ruby_version: !ruby/object:Gem::Requirement
|
89
90
|
requirements:
|
90
|
-
- -
|
91
|
+
- - '>='
|
91
92
|
- !ruby/object:Gem::Version
|
92
93
|
version: '2.0'
|
93
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
95
|
requirements:
|
95
|
-
- -
|
96
|
+
- - '>='
|
96
97
|
- !ruby/object:Gem::Version
|
97
98
|
version: '0'
|
98
99
|
requirements: []
|
99
100
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.
|
101
|
+
rubygems_version: 2.4.5
|
101
102
|
signing_key:
|
102
103
|
specification_version: 4
|
103
104
|
summary: a cli tool (and module) to beautify ruby code.
|