ruby-beautify 0.94.0 → 0.94.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 894e285bf45a3569ff950d814687a9df9fd9ac1f
4
- data.tar.gz: 913f62f88fd4e65783adcc71f2193fc3ee65bf1d
3
+ metadata.gz: c0d8f2666a10cd5f951233487ef5bcbc18226747
4
+ data.tar.gz: a43bca59db59c2aa94b1ea8538c7aa29ba0a19f8
5
5
  SHA512:
6
- metadata.gz: 0769f3ed536d176898707c9fdbd2f46c26ec8a84c84be60f81e01a90b72a78324f4aa81f257e90ba3300eebc5b355ffbf130311983d6c5aff74aea7c993e6871
7
- data.tar.gz: f5ee63bf346accda19c6a5337003e0775c3c06d583099b797ef38e57d24901d5f1ba194b7c756176d71b726152b352ecc9a591b4e251160964c7fa94b9ca7ba6
6
+ metadata.gz: 87e379a981c675498389680e58e60cc398d3709c2572bd4eef4cd39120a41679bb992663c86dae23ad02f2c06d0c3fcdba6fc28cfe7a115069f46c3356bc6a3a
7
+ data.tar.gz: 5a6dbf3aa9c9f2ab1e92e6d7c00006f0e92bc598093512d4e4d36ec882b464cebe4b776d2abd4854f24d5eca2463a36d7a31e0a217d9085cae05e532591cd562
data/WHATSNEW.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.94.1
2
+ * Multiline string and embedded doc support thanks to @veelenga.
3
+
1
4
  ## 0.94.0
2
5
  * Added a very basic spec.
3
6
  * Added better support for piping and redirection (thanks @veelenga)
data/bin/rbeautify CHANGED
@@ -17,71 +17,53 @@ Options.parse!
17
17
  @open_block_do = ['do', '{']
18
18
  @close_block = ['end', '}']
19
19
 
20
- @open_brackets = [:on_lparen, :on_lbracket, :on_lbrace]
21
- @close_brackets = [:on_rparen, :on_rbracket, :on_rbrace]
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
+
22
24
  @indent_token = "\t" unless @indent_token
23
25
  @indent_count = 1 unless @indent_count
24
26
 
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
27
  def puts_indented_line(level, string)
41
- if string.empty? or string =~ /^\n$/
28
+ if string =~ /^\n$/
42
29
  puts
43
30
  else
44
31
  indent = (@indent_token * @indent_count) * level
45
- puts indent + string
32
+ puts "#{indent}#{string.lstrip}"
46
33
  end
47
34
  end
48
35
 
49
36
  def pretty(content)
50
- # it's far easier if the content is 'flat', we don't have to worry about
51
- # pre-existing indent levels. So lets lex that.
52
37
  begin
53
- lex = Ripper.lex(flatten_content(content))
38
+ lex = Ripper.lex(content)
54
39
  rescue
55
40
  exit 255
56
41
  end
57
42
 
58
- # quick way to get our total lines.
59
- total_lines = lex.last[0][0]
60
- line_index = 1
61
-
62
43
  indent_level = 0
44
+ line_lex = []
63
45
 
64
- # walk through our lines.
65
- while (line_index <= total_lines)
66
- # a 'lex' copy of our line.
67
- line_lex = lex.select {|l| l[0][0] == line_index}
68
- # a string version.
69
- line_string = line_lex.map {|l| l.last}.join("")
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
70
50
 
71
- # did we just close something? if so, lets bring it down a level.
72
- if closing_block?(line_lex) || closing_assignment?(line_lex)
73
- indent_level -=1 if indent_level > 0
74
- end
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
75
55
 
76
- # print our line, in place.
77
- puts_indented_line(indent_level, line_string)
56
+ # print our line, in place.
57
+ line_string = line_lex.map {|l| l.last}.join
58
+ puts_indented_line(indent_level, line_string)
78
59
 
79
- # oh, we opened something did we? lets indent for the next run.
80
- if opening_block?(line_lex) || opening_assignment?(line_lex)
81
- indent_level +=1
82
- end
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
83
64
 
84
- line_index += 1
65
+ line_lex.clear
66
+ end
85
67
  end
86
68
 
87
69
  return nil
@@ -99,12 +81,22 @@ end
99
81
 
100
82
  # is the first word a key word?
101
83
  def starts_block?(line_lex)
102
- line_lex.first[1] == :on_kw && @open_block_start.include?(line_lex.first[2])
84
+ line_lex.each do |x|
85
+ # search for a first non-space token
86
+ if not x[1] == :on_sp
87
+ return x[1] == :on_kw && @open_block_start.include?(x[2])
88
+ end
89
+ end
103
90
  end
104
91
 
105
92
  # is the first word one of our 'both' keywords?
106
93
  def both_block?(line_lex)
107
- return line_lex.first[1] == :on_kw && @both_block.include?(line_lex.first[2])
94
+ line_lex.each do |x|
95
+ # search for a first non-space token
96
+ if not x[1] == :on_sp
97
+ return x[1] == :on_kw && @both_block.include?(x[2])
98
+ end
99
+ end
108
100
  end
109
101
 
110
102
  # kinda complex, we count open/close to determine if we ultimately have a
data/bin/ruby-beautify CHANGED
@@ -17,71 +17,53 @@ Options.parse!
17
17
  @open_block_do = ['do', '{']
18
18
  @close_block = ['end', '}']
19
19
 
20
- @open_brackets = [:on_lparen, :on_lbracket, :on_lbrace]
21
- @close_brackets = [:on_rparen, :on_rbracket, :on_rbrace]
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
+
22
24
  @indent_token = "\t" unless @indent_token
23
25
  @indent_count = 1 unless @indent_count
24
26
 
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
27
  def puts_indented_line(level, string)
41
- if string.empty? or string =~ /^\n$/
28
+ if string =~ /^\n$/
42
29
  puts
43
30
  else
44
31
  indent = (@indent_token * @indent_count) * level
45
- puts indent + string
32
+ puts "#{indent}#{string.lstrip}"
46
33
  end
47
34
  end
48
35
 
49
36
  def pretty(content)
50
- # it's far easier if the content is 'flat', we don't have to worry about
51
- # pre-existing indent levels. So lets lex that.
52
37
  begin
53
- lex = Ripper.lex(flatten_content(content))
38
+ lex = Ripper.lex(content)
54
39
  rescue
55
40
  exit 255
56
41
  end
57
42
 
58
- # quick way to get our total lines.
59
- total_lines = lex.last[0][0]
60
- line_index = 1
61
-
62
43
  indent_level = 0
44
+ line_lex = []
63
45
 
64
- # walk through our lines.
65
- while (line_index <= total_lines)
66
- # a 'lex' copy of our line.
67
- line_lex = lex.select {|l| l[0][0] == line_index}
68
- # a string version.
69
- line_string = line_lex.map {|l| l.last}.join("")
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
70
50
 
71
- # did we just close something? if so, lets bring it down a level.
72
- if closing_block?(line_lex) || closing_assignment?(line_lex)
73
- indent_level -=1 if indent_level > 0
74
- end
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
75
55
 
76
- # print our line, in place.
77
- puts_indented_line(indent_level, line_string)
56
+ # print our line, in place.
57
+ line_string = line_lex.map {|l| l.last}.join
58
+ puts_indented_line(indent_level, line_string)
78
59
 
79
- # oh, we opened something did we? lets indent for the next run.
80
- if opening_block?(line_lex) || opening_assignment?(line_lex)
81
- indent_level +=1
82
- end
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
83
64
 
84
- line_index += 1
65
+ line_lex.clear
66
+ end
85
67
  end
86
68
 
87
69
  return nil
@@ -99,12 +81,22 @@ end
99
81
 
100
82
  # is the first word a key word?
101
83
  def starts_block?(line_lex)
102
- line_lex.first[1] == :on_kw && @open_block_start.include?(line_lex.first[2])
84
+ line_lex.each do |x|
85
+ # search for a first non-space token
86
+ if not x[1] == :on_sp
87
+ return x[1] == :on_kw && @open_block_start.include?(x[2])
88
+ end
89
+ end
103
90
  end
104
91
 
105
92
  # is the first word one of our 'both' keywords?
106
93
  def both_block?(line_lex)
107
- return line_lex.first[1] == :on_kw && @both_block.include?(line_lex.first[2])
94
+ line_lex.each do |x|
95
+ # search for a first non-space token
96
+ if not x[1] == :on_sp
97
+ return x[1] == :on_kw && @both_block.include?(x[2])
98
+ end
99
+ end
108
100
  end
109
101
 
110
102
  # kinda complex, we count open/close to determine if we ultimately have a
@@ -1,3 +1,3 @@
1
1
  module RBeautify
2
- VERSION = "0.94.0"
2
+ VERSION = "0.94.1"
3
3
  end
@@ -11,7 +11,7 @@ require 'digest/md5'
11
11
 
12
12
  describe "Ruby Beautify" do
13
13
  before (:all) do
14
- @good_md5_sum = 'bd409d320cfe6fd0e2bf405e47cd2f2c'
14
+ @good_md5_sum = Digest::MD5.hexdigest File.read('spec/example_beautified.rb')
15
15
  @bad_file = 'spec/example.rb'
16
16
  @bin = "#{Dir.pwd}/bin/ruby-beautify"
17
17
  end
data/spec/example.rb CHANGED
@@ -68,3 +68,27 @@ puts 'never!'
68
68
  else
69
69
  puts 'not likely.'
70
70
  end
71
+
72
+ # Test for already indented blocks
73
+ class There2 < There
74
+ def m1()
75
+ puts "m1"
76
+ end
77
+ def m2()
78
+ puts "m2"
79
+ end
80
+ def m3()
81
+ puts "m3"
82
+ end
83
+ end
84
+
85
+ # Test for multiline string
86
+ def m (x)
87
+ puts "This is multi-line string. It's line1 \
88
+ It's line 2\
89
+ And this is\n line3
90
+ This line should not be mutated"
91
+ puts "This is multi line interpolated string #{
92
+ x
93
+ }"
94
+ end
@@ -0,0 +1,94 @@
1
+ require 'ripper'
2
+
3
+ module Here
4
+ class There
5
+ def why?(argument = nil)
6
+ @array = [
7
+ 1,
8
+ 2,
9
+ 3
10
+ ]
11
+ hash = {
12
+ one:1,
13
+ two:2,
14
+ three:3
15
+ }
16
+ end
17
+
18
+ # a comment
19
+ def why_not?(argument: {})
20
+ @array = [4,5,6]
21
+ hash = {four:4, five:5, six:6}
22
+ s = "a #{"complex"} string."
23
+ end
24
+
25
+ def complex_method(one, two, three, four)
26
+ regex = /regex/
27
+ end
28
+
29
+ def with_block
30
+ run = Proc.new { |argument| puts arugment }
31
+ run do |arugment|
32
+ puts argument
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ h = Here::There.new
39
+
40
+ h.why?({
41
+ here: 'there',
42
+ there: 'here'
43
+ })
44
+
45
+ h.with_block {
46
+ puts 'yahooie'
47
+ }
48
+
49
+
50
+ h.complex_method('asdkjasflkjdglksjglksfgjlfkgjdf',
51
+ 'alfkjsdlkfjsflgkjfglk',
52
+ 'alfkjsdlkfjsflgkjfglk',
53
+ 'alfkjsdlkfjsflgkjfglk'
54
+ )
55
+
56
+ h.complex_method('asdkjasflkjdglksjglksfgjlfkgjdf',
57
+ 'alfkjsdlkfjsflgkjfglk',
58
+ 'alfkjsdlkfjsflgkjfglk',
59
+ 'alfkjsdlkfjsflgkjfglk'
60
+ ).map do |i|
61
+ i
62
+ end.map! { |i| i }
63
+
64
+ if 1 > 0
65
+ puts 'something'
66
+ elsif 1 < 0
67
+ puts 'never!'
68
+ else
69
+ puts 'not likely.'
70
+ end
71
+
72
+ # Test for already indented blocks
73
+ class There2 < There
74
+ def m1()
75
+ puts "m1"
76
+ end
77
+ def m2()
78
+ puts "m2"
79
+ end
80
+ def m3()
81
+ puts "m3"
82
+ end
83
+ end
84
+
85
+ # Test for multiline string
86
+ def m (x)
87
+ puts "This is multi-line string. It's line1 \
88
+ It's line 2\
89
+ And this is\n line3
90
+ This line should not be mutated"
91
+ puts "This is multi line interpolated string #{
92
+ x
93
+ }"
94
+ end
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.94.0
4
+ version: 0.94.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernie Brodeur
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-10 00:00:00.000000000 Z
11
+ date: 2014-12-23 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,9 @@ executables:
60
60
  extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
- - .gitignore
64
- - .rspec
65
- - .travis.yml
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
66
  - Gemfile
67
67
  - Guardfile
68
68
  - LICENSE
@@ -76,6 +76,7 @@ files:
76
76
  - ruby-beautify.gemspec
77
77
  - spec/bin/ruby-beautify_spec.rb
78
78
  - spec/example.rb
79
+ - spec/example_beautified.rb
79
80
  - spec/spec_helper.rb
80
81
  homepage: https://github.com/erniebrodeur/ruby-beautify
81
82
  licenses: []
@@ -86,21 +87,22 @@ require_paths:
86
87
  - lib
87
88
  required_ruby_version: !ruby/object:Gem::Requirement
88
89
  requirements:
89
- - - '>='
90
+ - - ">="
90
91
  - !ruby/object:Gem::Version
91
92
  version: '0'
92
93
  required_rubygems_version: !ruby/object:Gem::Requirement
93
94
  requirements:
94
- - - '>='
95
+ - - ">="
95
96
  - !ruby/object:Gem::Version
96
97
  version: '0'
97
98
  requirements: []
98
99
  rubyforge_project:
99
- rubygems_version: 2.0.14
100
+ rubygems_version: 2.2.2
100
101
  signing_key:
101
102
  specification_version: 4
102
103
  summary: a cli tool (and module) to beautify ruby code.
103
104
  test_files:
104
105
  - spec/bin/ruby-beautify_spec.rb
105
106
  - spec/example.rb
107
+ - spec/example_beautified.rb
106
108
  - spec/spec_helper.rb