ruby-beautify 0.92.2 → 0.93.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 771591fa2eb43b8a8244394dff9afc39082923bc
4
+ data.tar.gz: 276d9e48285d905dabec7943f926432c23f573d7
5
+ SHA512:
6
+ metadata.gz: a29b4dcd745558242d2e14eb22989c02e5731c95aeb28e857a3aaa64fdcc2fca80e94bcfbcfedacc993fd90104c85bc2b19a0151782ef1849c663da104e38db7
7
+ data.tar.gz: 89cdc67493a723ab777bba6306e07750531b7c8e199461a929ff3ef15135d269dea40789b8725b7d94b9d00b2b9894ecd43ac31a6676dc7c07505eed2e9278ac
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  Gemfile.lock
2
+ pkg/*
data/README.md CHANGED
@@ -2,38 +2,69 @@
2
2
 
3
3
  This gem provides a cli binary named 'rbeautify' that will pretty up ruby code.
4
4
 
5
+
5
6
  ## Installation
6
7
 
7
- $ gem install ruby-beautify
8
+ % gem install ruby-beautify
8
9
 
9
10
  ## Usage
10
11
 
11
12
  To Pretty up a file:
12
13
 
13
- $ rbeautify filename
14
-
15
- It can take mulitple filenames:
16
-
17
- $ rbeautify a b c
14
+ % rbeautify filename
18
15
 
19
16
  Without a filename it reads from STDIN, suitable for piping:
20
17
 
21
- $ curl 'http://example.org/ugly-file.rb' | rbeautify
18
+ % curl 'http://example.org/ugly-file.rb' | rbeautify
22
19
 
23
20
  It has help:
24
21
 
25
- $ rbeautify -h
22
+ % rbeautify --help
23
+
24
+ You can pick your indent character:
25
+
26
+ % rbeautify --(t)abs
27
+
28
+ % rbeautify --(s)paces
29
+
30
+ You can also pick the count of characters:
31
+
32
+ % rbeautify --indent_(c)ount 1
33
+
34
+ Examples:
35
+
36
+ % rbeautify -c 2 -s filename
37
+
38
+ % rbeautify filename
39
+
40
+ % rbeautify -t -c 2 filename
41
+
42
+ ## Bugs
43
+
44
+ I don't have every ruby style tested against, so I expect some gaps. Feel free to submit issues (please include an example) and I'll figure out how to 'beautify' it.
45
+
46
+ Please feel free to open issues, I am actively working on this project again, thanks entirely to the ripper gem.
47
+
48
+ ## Todo
49
+
50
+ * Add vim style comment hinting.
51
+ * Add overwrite in place to files.
52
+ * Add 'best guest' for files that fail syntax checking.
53
+ * Add syntax checking to files rendered via STDIN.
54
+
55
+ Longer term I'd like to do some more to assignment, line wrapping, and spacing in/around keywords.
26
56
 
27
57
  ## Contributing
28
58
 
29
- 1. Fork it
30
- 2. Create your feature branch (`git checkout -b my-new-feature`)
31
- 3. Commit your changes (`git commit -am 'Added some feature'`)
32
- 4. Push to the branch (`git push origin my-new-feature`)
33
- 5. Create new Pull Request
59
+ * fork it.
60
+ * create it.
61
+ * push it.
62
+ * pull it.
34
63
 
35
64
  # History
36
65
 
37
66
  The original analyzer is available at: http://www.arachnoid.com/ruby/rubyBeautifier.html.
38
67
 
39
68
  My work is based off of this sublime-text2 plugin: https://github.com/CraigWilliams/BeautifyRuby but cleaned up and made suitable for use directly in a shell.
69
+
70
+ I've recently re-written this to use the stdlib `ripper` gem to do the lexical analyzing. Consequently I've dropped all of the old legacy code that did this.
@@ -1,28 +1,180 @@
1
- #!/usr/bin/ruby
1
+ #!/usr/bin/env ruby
2
2
  require 'ruby-beautify'
3
+ require 'ripper'
3
4
  require 'optparse'
4
5
 
5
- include RBeautify
6
-
7
6
  Options = OptionParser.new do |opts|
8
7
  opts.on("-V", "--version", "Print version") { |version| puts RBeautify::VERSION;exit 0}
8
+ opts.on("-c", "--indent_count [COUNT]", Integer, "Count of characters to use for indenting. (default: 1)") { |count| @indent_count = count}
9
+ opts.on("-t", "--tabs", "Use tabs for the indent character (default)") { @indent_token = "\t" }
10
+ opts.on("-s", "--spaces", "Use tabs for the indent character") { @indent_token = " " }
9
11
  opts.banner = "Usage: print ruby into a pretty format, or break trying."
10
12
  end
11
13
  Options.parse!
12
14
 
13
- if ARGV.empty?
15
+ @open_block_start = ["module", "class", "begin", "def", 'if', 'while', 'unless']
16
+ @both_block = ["else", "elsif", 'rescue']
17
+ @open_block_do = ['do', '{']
18
+ @close_block = ['end', '}']
19
+
20
+ @open_brackets = [:on_lparen, :on_lbracket, :on_lbrace]
21
+ @close_brackets = [:on_rparen, :on_rbracket, :on_rbrace]
22
+ @indent_token = "\t" unless @indent_token
23
+ @indent_count = 1 unless @indent_count
24
+
25
+ def flatten_content(content)
26
+ # I realize this is slow, todo, make it not slow.
27
+ flat_content = []
28
+ content.split("\n").each do |l|
29
+ w = l.split(/\s*(?<line>.*)/).last
30
+ if w
31
+ flat_content.push w
32
+ else
33
+ flat_content.push l
34
+ end
35
+ end
36
+
37
+ return flat_content.join("\n")
38
+ end
39
+
40
+ def puts_indented_line(level, string)
41
+ indent = (@indent_token * @indent_count) * level
42
+ puts indent + string
43
+ end
44
+
45
+ def pretty(content)
46
+ # it's far easier if the content is 'flat', we don't have to worry about
47
+ # pre-existing indent levels. So lets lex that.
14
48
  begin
15
- puts RBeautify.beautify_string :ruby, STDIN
16
- rescue Exception => e
17
- puts e.message
18
- exit
49
+ lex = Ripper.lex(flatten_content(content))
50
+ rescue
51
+ exit 255
19
52
  end
53
+
54
+ # quick way to get our total lines.
55
+ total_lines = lex.last[0][0]
56
+ line_index = 1
57
+
58
+ indent_level = 0
59
+
60
+ # walk through our lines.
61
+ while (line_index <= total_lines)
62
+ # a 'lex' copy of our line.
63
+ line_lex = lex.select {|l| l[0][0] == line_index}
64
+ # a string version.
65
+ line_string = line_lex.map {|l| l.last}.join("")
66
+
67
+ # did we just close something? if so, lets bring it down a level.
68
+ if closing_block?(line_lex) || closing_assignment?(line_lex)
69
+ indent_level -=1 if indent_level > 0
70
+ end
71
+
72
+ # print our line, in place.
73
+ puts_indented_line(indent_level, line_string)
74
+
75
+ # oh, we opened something did we? lets indent for the next run.
76
+ if opening_block?(line_lex) || opening_assignment?(line_lex)
77
+ indent_level +=1
78
+ end
79
+
80
+ line_index += 1
81
+ end
82
+
83
+ return nil
84
+ end
85
+
86
+ # how many times do we open in this line?
87
+ def opening_block_count(line_lex)
88
+ line_lex.select {|l| l[1] == :on_kw && @open_block_do.include?(l[2])}.count
89
+ end
90
+
91
+ # how many times do we close?
92
+ def closing_block_count(line_lex)
93
+ line_lex.select {|l| l[1] == :on_kw && @close_block.include?(l[2])}.count
94
+ end
95
+
96
+ # is the first word a key word?
97
+ def starts_block?(line_lex)
98
+ line_lex.first[1] == :on_kw && @open_block_start.include?(line_lex.first[2])
99
+ end
100
+
101
+ # is the first word one of our 'both' keywords?
102
+ def both_block?(line_lex)
103
+ return line_lex.first[1] == :on_kw && @both_block.include?(line_lex.first[2])
104
+ end
105
+
106
+ # kinda complex, we count open/close to determine if we ultimately have a
107
+ # hanging line. Always true if it's a both_block.
108
+ def opening_block?(line_lex)
109
+ return true if both_block? line_lex
110
+ opens = starts_block?(line_lex) ? 1 : 0
111
+ opens += opening_block_count line_lex
112
+ closes = closing_block_count line_lex
113
+ return false if opens == closes
114
+ return true if opens > closes
115
+ end
116
+
117
+ # kinda complex, we count open/close to determine if we ultimately have close a
118
+ # hanging line. Always true if it's a both_block.
119
+ def closing_block?(line_lex)
120
+ return true if both_block? line_lex
121
+ opens = starts_block?(line_lex) ? 1 : 0
122
+ opens += opening_block_count line_lex
123
+ closes = closing_block_count line_lex
124
+ return false if opens == closes
125
+ return true if opens < closes
126
+ end
127
+
128
+ # count the amount of opening assignments.
129
+ def opening_assignment_count(line_lex)
130
+ line_lex.select {|l| @open_brackets.include? l[1]}.count
131
+ end
132
+
133
+ # count the amount of closing assignments.
134
+ def closing_assignment_count(line_lex)
135
+ line_lex.select {|l| @close_brackets.include? l[1]}.count
136
+ end
137
+
138
+ # same trick as opening_block
139
+ def opening_assignment?(line_lex)
140
+ opens = opening_assignment_count line_lex
141
+ closes = closing_assignment_count line_lex
142
+ return false if opens == closes
143
+ return true if opens > closes
144
+ end
145
+
146
+ # ...
147
+ def closing_assignment?(line_lex)
148
+ opens = opening_assignment_count line_lex
149
+ closes = closing_assignment_count line_lex
150
+ return false if opens == closes
151
+ return true if closes > opens
152
+ end
153
+
154
+ # is this ruby file valid syntax?
155
+ def valid_syntax?(file)
156
+ #ugly but fast, ruby returns stdout if it's ok, and stderr if not.
157
+ #if we get anything back, we were ok, else we were not.
158
+ r = `ruby -c #{file} 2> /dev/null`
159
+ return false if r == ""
160
+ return true
161
+ end
162
+
163
+ # no argument, assume we want to open STDIN.
164
+ if ARGV.empty?
165
+ content = gets
166
+ puts pretty content
20
167
  else
21
- ARGV.each do |f|
22
- if File.exist? f
23
- puts RBeautify.beautify_string :ruby, open(f).read
168
+ file = ARGV[0]
169
+ if File.exist? file
170
+ if valid_syntax? file
171
+ content = open(file).read
172
+ pretty content
24
173
  else
25
- puts "No such file: #{f}"
174
+ puts content
175
+ exit 127
26
176
  end
177
+ else
178
+ puts "No such file: #{file}"
27
179
  end
28
180
  end
@@ -1,10 +1,4 @@
1
1
  require "ruby-beautify/version"
2
- require 'ruby-beautify/block_start'
3
- require 'ruby-beautify/block_end'
4
- require 'ruby-beautify/block_matcher'
5
- require 'ruby-beautify/language'
6
- require 'ruby-beautify/line'
7
- require 'ruby-beautify/config/ruby'
8
2
 
9
3
  module RBeautify
10
4
  def self.beautify_string(language, source, use_tabs=false)
@@ -1,3 +1,3 @@
1
1
  module RBeautify
2
- VERSION = "0.92.2"
2
+ VERSION = "0.93.0"
3
3
  end
@@ -4,7 +4,7 @@ Gem::Specification.new do |gem|
4
4
  gem.name = 'ruby-beautify'
5
5
  gem.summary = "a cli tool (and module) to beautify ruby code."
6
6
  gem.description = gem.summary
7
- gem.authors = ["Ernie Brodeur", "Craig Williams", "Joel Chippindale", "Paul Lutus"]
7
+ gem.authors = ["Ernie Brodeur"]
8
8
  gem.email = 'ebrodeur@ujami.net'
9
9
  gem.homepage = "https://github.com/erniebrodeur/ruby-beautify"
10
10
 
metadata CHANGED
@@ -1,33 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-beautify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.92.2
5
- prerelease:
4
+ version: 0.93.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Ernie Brodeur
9
- - Craig Williams
10
- - Joel Chippindale
11
- - Paul Lutus
12
8
  autorequire:
13
9
  bindir: bin
14
10
  cert_chain: []
15
- date: 2012-07-20 00:00:00.000000000 Z
11
+ date: 2014-11-16 00:00:00.000000000 Z
16
12
  dependencies:
17
13
  - !ruby/object:Gem::Dependency
18
14
  name: rspec
19
15
  requirement: !ruby/object:Gem::Requirement
20
- none: false
21
16
  requirements:
22
- - - ! '>='
17
+ - - ">="
23
18
  - !ruby/object:Gem::Version
24
19
  version: '0'
25
20
  type: :development
26
21
  prerelease: false
27
22
  version_requirements: !ruby/object:Gem::Requirement
28
- none: false
29
23
  requirements:
30
- - - ! '>='
24
+ - - ">="
31
25
  - !ruby/object:Gem::Version
32
26
  version: '0'
33
27
  description: a cli tool (and module) to beautify ruby code.
@@ -37,60 +31,37 @@ executables:
37
31
  extensions: []
38
32
  extra_rdoc_files: []
39
33
  files:
40
- - .gitignore
34
+ - ".gitignore"
41
35
  - Gemfile
42
36
  - LICENSE
43
37
  - README.md
44
38
  - RELEASE.md
45
39
  - Rakefile
46
40
  - bin/rbeautify
47
- - lib/beautifier.rb
48
41
  - lib/ruby-beautify.rb
49
- - lib/ruby-beautify/block_end.rb
50
- - lib/ruby-beautify/block_matcher.rb
51
- - lib/ruby-beautify/block_start.rb
52
- - lib/ruby-beautify/config/ruby.rb
53
- - lib/ruby-beautify/language.rb
54
- - lib/ruby-beautify/line.rb
55
42
  - lib/ruby-beautify/version.rb
56
43
  - ruby-beautify.gemspec
57
- - spec/fixtures/ruby.yml
58
- - spec/rbeautify/block_matcher_spec.rb
59
- - spec/rbeautify/block_start_spec.rb
60
- - spec/rbeautify/config/ruby_spec.rb
61
- - spec/rbeautify/line_spec.rb
62
- - spec/rbeautify_spec.rb
63
- - spec/spec_helper.rb
64
44
  homepage: https://github.com/erniebrodeur/ruby-beautify
65
45
  licenses: []
46
+ metadata: {}
66
47
  post_install_message:
67
48
  rdoc_options: []
68
49
  require_paths:
69
50
  - lib
70
51
  required_ruby_version: !ruby/object:Gem::Requirement
71
- none: false
72
52
  requirements:
73
- - - ! '>='
53
+ - - ">="
74
54
  - !ruby/object:Gem::Version
75
55
  version: '0'
76
56
  required_rubygems_version: !ruby/object:Gem::Requirement
77
- none: false
78
57
  requirements:
79
- - - ! '>='
58
+ - - ">="
80
59
  - !ruby/object:Gem::Version
81
60
  version: '0'
82
61
  requirements: []
83
62
  rubyforge_project:
84
- rubygems_version: 1.8.21
63
+ rubygems_version: 2.2.2
85
64
  signing_key:
86
- specification_version: 3
65
+ specification_version: 4
87
66
  summary: a cli tool (and module) to beautify ruby code.
88
- test_files:
89
- - spec/fixtures/ruby.yml
90
- - spec/rbeautify/block_matcher_spec.rb
91
- - spec/rbeautify/block_start_spec.rb
92
- - spec/rbeautify/config/ruby_spec.rb
93
- - spec/rbeautify/line_spec.rb
94
- - spec/rbeautify_spec.rb
95
- - spec/spec_helper.rb
96
- has_rdoc:
67
+ test_files: []
@@ -1,168 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # Ruby beautifier, version 2.1, 09/11/2006
4
- # Copyright (c) 2006, P. Lutus
5
- # Released under the GPL
6
-
7
- # Changes
8
- # Craig Williams
9
- # 12/26/2011
10
- # Modified (very minor) to work with a Sublime Text 2 plugin
11
-
12
- # indent regexp tests
13
-
14
- @indentExp = [
15
- /^module\b/,
16
- /^if\b/,
17
- /^\@{0,2}[\w\.]*[\s\t]*\=[\s\t]*if\b/,
18
- /(=\s*|^)until\b/,
19
- /(=\s*|^)for\b/,
20
- /^unless\b/,
21
- /^\@{0,2}[\w\.]*[\s\t]*\=[\s\t]*unless\b/,
22
- /(=\s*|^)while\b/,
23
- /(=\s*|^)begin\b/,
24
- /(^| )case\b/,
25
- /\bthen\b/,
26
- /^class\b/,
27
- /^rescue\b/,
28
- /^def\b/,
29
- /\bdo\b/,
30
- /^else\b/,
31
- /^elsif\b/,
32
- /^ensure\b/,
33
- /\bwhen\b/,
34
- /\{[^\}]*$/,
35
- /\[[^\]]*$/,
36
- /\([^\)]*$/
37
- ]
38
-
39
- # outdent regexp tests
40
-
41
- @outdentExp = [
42
- /^rescue\b/,
43
- /^ensure\b/,
44
- /^elsif\b/,
45
- /^end\b/,
46
- /^else\b/,
47
- /\bwhen\b/,
48
- /^[^\{]*\}/,
49
- /^[^\[]*\]/,
50
- /^[^\(]*\)/
51
- ]
52
-
53
- def makeTab(tab)
54
- (tab < 0) ? "" : @tabStr * @tabSize * tab
55
- end
56
-
57
- def addLine(line,tab)
58
- line.strip!
59
- line = makeTab(tab)+line if line.length > 0
60
- line + "\n"
61
- end
62
-
63
- def beautifyRuby(contents)
64
- commentBlock = false
65
- programEnd = false
66
- multiLineArray = Array.new
67
- multiLineStr = ""
68
- tab = 0
69
- source = contents
70
- dest = ""
71
- source.split("\n").each do |line|
72
- if(!programEnd)
73
- # detect program end mark
74
- if(line =~ /^__END__$/)
75
- programEnd = true
76
- else
77
- # combine continuing lines
78
- if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
79
- multiLineArray.push line
80
- multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
81
- next
82
- end
83
-
84
- # add final line
85
- if(multiLineStr.length > 0)
86
- multiLineArray.push line
87
- multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
88
- end
89
-
90
- tline = ((multiLineStr.length > 0)?multiLineStr:line).strip
91
- if(tline =~ /^=begin/)
92
- commentBlock = true
93
- end
94
- end
95
- end
96
- if(commentBlock || programEnd)
97
- # add the line unchanged
98
- dest += line + "\n"
99
- else
100
- commentLine = (tline =~ /^#/)
101
- if(!commentLine)
102
- # throw out sequences that will
103
- # only sow confusion
104
- while tline.gsub!(/'.*?'/,"")
105
- end
106
- while tline.gsub!(/".*?"/,"")
107
- end
108
- while tline.gsub!(/\`.*?\`/,"")
109
- end
110
- while tline.gsub!(/\{[^\{]*?\}/,"")
111
- end
112
- while tline.gsub!(/\([^\(]*?\)/,"")
113
- end
114
- while tline.gsub!(/\/.*?\//,"")
115
- end
116
- while tline.gsub!(/%r(.).*?\1/,"")
117
- end
118
- tline.gsub!(/\\\"/,"'")
119
- @outdentExp.each do |re|
120
- if(tline =~ re)
121
- tab -= 1
122
- break
123
- end
124
- end
125
- end
126
- if (multiLineArray.length > 0)
127
- multiLineArray.each do |ml|
128
- dest += addLine(ml,tab)
129
- end
130
- multiLineArray.clear
131
- multiLineStr = ""
132
- else
133
- dest += addLine(line,tab)
134
- end
135
- if(!commentLine)
136
- @indentExp.each do |re|
137
- if(tline =~ re && !(tline =~ /\s+end\s*$/))
138
- tab += 1
139
- break
140
- end
141
- end
142
- end
143
- end
144
- if(tline =~ /^=end/)
145
- commentBlock = false
146
- end
147
- end
148
- STDOUT.write(dest)
149
- # uncomment this to complain about mismatched blocks
150
- # if(tab != 0)
151
- # STDERR.puts "#{path}: Indentation error: #{tab}"
152
- # end
153
- end
154
-
155
- tab_or_space = ARGV.first
156
- path = ARGV.last
157
- path.gsub!("\\", "/") if RUBY_PLATFORM =~ /mswin/
158
- contents = IO.read(path)
159
-
160
- if tab_or_space == 'space'
161
- @tabSize = 2
162
- @tabStr = " "
163
- else
164
- @tabSize = 1
165
- @tabStr = "\t"
166
- end
167
-
168
- beautifyRuby(contents)