prettyrb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +26 -0
- data/LICENSE.txt +21 -0
- data/README.md +31 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/prettyrb/version.rb +3 -0
- data/lib/prettyrb.rb +191 -0
- data/prettyrb.gemspec +40 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6627cfb70e4dee209000644f4be5d57d6794c240fda5767b4b85d877c7cb2ed5
|
4
|
+
data.tar.gz: ae15fc8e9b154777b0bdaad845047b8a8c6709ee0e8e7616bcabb77cf12f3f06
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5e0543768d14fbb1fa9715a2c1657bbcc4d376939e342cd7badc14cddde7a0b8c8354d3ead3321b0a14ad5143fae8381c4eaf55e4d3c66785ac0eb8300ae75d7
|
7
|
+
data.tar.gz: 7d186ac5763c031e88d4357d00cae4afe6ba002d1a82381bf8f00b8050b8c33ca9e3f9a4ca8dde1312488fba002196544ffea134bdb144cefca2a05350a55c13
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
prettyrb (0.1.0)
|
5
|
+
ruby_parser (~> 3.14)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
minitest (5.14.0)
|
11
|
+
rake (10.5.0)
|
12
|
+
ruby_parser (3.14.2)
|
13
|
+
sexp_processor (~> 4.9)
|
14
|
+
sexp_processor (4.14.1)
|
15
|
+
|
16
|
+
PLATFORMS
|
17
|
+
ruby
|
18
|
+
|
19
|
+
DEPENDENCIES
|
20
|
+
bundler (~> 1.17)
|
21
|
+
minitest (~> 5.0)
|
22
|
+
prettyrb!
|
23
|
+
rake (~> 10.0)
|
24
|
+
|
25
|
+
BUNDLED WITH
|
26
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Blake Williams
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Prettyrb
|
2
|
+
|
3
|
+
Super experimental gem for auto-formatting Ruby files. Pronounced "pretty-erb"
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'prettyrb'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install prettyrb
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
PrettyRb.new(source_code).format
|
25
|
+
```
|
26
|
+
|
27
|
+
CLI helper coming soon™
|
28
|
+
|
29
|
+
## License
|
30
|
+
|
31
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "prettyrb"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/prettyrb.rb
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
require "ruby_parser"
|
2
|
+
require "prettyrb/version"
|
3
|
+
|
4
|
+
module Prettyrb
|
5
|
+
class Error < StandardError; end
|
6
|
+
|
7
|
+
class Formatter
|
8
|
+
def self.for(sexp)
|
9
|
+
case sexp.sexp_type
|
10
|
+
when :if
|
11
|
+
IfFormatter
|
12
|
+
when :str
|
13
|
+
StringFormatter
|
14
|
+
when :or
|
15
|
+
OrFormatter
|
16
|
+
when :and
|
17
|
+
AndFormatter
|
18
|
+
when :lit
|
19
|
+
LitFormatter
|
20
|
+
when :call
|
21
|
+
CallFormatter
|
22
|
+
when :class
|
23
|
+
ClassFormatter
|
24
|
+
when :cdecl
|
25
|
+
CdeclFormatter
|
26
|
+
when :array
|
27
|
+
ArrayFormatter
|
28
|
+
else
|
29
|
+
raise "can't handle #{sexp}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def initialize(code)
|
34
|
+
@code = code
|
35
|
+
end
|
36
|
+
|
37
|
+
def format
|
38
|
+
sexp_tree = RubyParser.new.parse(@code)
|
39
|
+
|
40
|
+
self.class.for(sexp_tree).new(sexp_tree, 0, nil).format
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def format_type(sexp, indentation)
|
46
|
+
case sexp.sexp_type
|
47
|
+
when :if
|
48
|
+
IfFormatter.new(sexp, indentation).format
|
49
|
+
else
|
50
|
+
raise "can't handle #{sexp}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
attr_reader :code
|
55
|
+
end
|
56
|
+
|
57
|
+
class BaseFormatter
|
58
|
+
attr_reader :sexp, :indentation, :parent
|
59
|
+
|
60
|
+
def initialize(sexp, indentation, parent)
|
61
|
+
@sexp = sexp
|
62
|
+
@indentation = indentation
|
63
|
+
@parent = parent
|
64
|
+
end
|
65
|
+
|
66
|
+
def type
|
67
|
+
sexp.sexp_type
|
68
|
+
end
|
69
|
+
|
70
|
+
def indents
|
71
|
+
' ' * indentation
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class LitFormatter < BaseFormatter
|
76
|
+
def format
|
77
|
+
sexp[1]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class StringFormatter < BaseFormatter
|
82
|
+
def format
|
83
|
+
if parent.type == :array
|
84
|
+
"\"#{sexp[1]}\""
|
85
|
+
else
|
86
|
+
"#{indents}\"#{sexp[1]}\""
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class IfFormatter < BaseFormatter
|
92
|
+
def format
|
93
|
+
start = "#{' ' * @indentation}if "
|
94
|
+
condition = Formatter.for(sexp[1]).new(@sexp[1], @indentation + 2, self).format
|
95
|
+
body = Formatter.for(sexp[2]).new(@sexp[2], @indentation + 2, self).format
|
96
|
+
ending = "#{' ' * @indentation}end"
|
97
|
+
|
98
|
+
"#{start}#{condition}\n#{body}\n#{ending}"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class OrFormatter < BaseFormatter
|
103
|
+
def format
|
104
|
+
left = Formatter.for(sexp[1]).new(sexp[1], @indentation, self).format
|
105
|
+
right = Formatter.for(sexp[2]).new(sexp[2], @indentation, self).format
|
106
|
+
|
107
|
+
if needs_parens?
|
108
|
+
"(#{left} || #{right})"
|
109
|
+
else
|
110
|
+
"#{left} || #{right}"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
def needs_parens?
|
117
|
+
parent&.type == :or || parent&.type == :and
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
class AndFormatter < BaseFormatter
|
122
|
+
def format
|
123
|
+
left = Formatter.for(sexp[1]).new(sexp[1], @indentation, self).format
|
124
|
+
right = Formatter.for(sexp[2]).new(sexp[2], @indentation, self).format
|
125
|
+
|
126
|
+
if needs_parens?
|
127
|
+
"(#{left} && #{right})"
|
128
|
+
else
|
129
|
+
"#{left} && #{right}"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
private
|
134
|
+
|
135
|
+
def needs_parens?
|
136
|
+
parent&.type == :or || parent&.type == :and
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
class CallFormatter < BaseFormatter
|
141
|
+
def format
|
142
|
+
if infix?
|
143
|
+
left = Formatter.for(sexp[1]).new(sexp[1], @indentation, self).format
|
144
|
+
right = Formatter.for(sexp[3]).new(sexp[3], @indentation, self).format
|
145
|
+
|
146
|
+
if needs_parens?
|
147
|
+
"(#{left} #{sexp[2]} #{right})"
|
148
|
+
else
|
149
|
+
"#{left} #{sexp[2]} #{right}"
|
150
|
+
end
|
151
|
+
else
|
152
|
+
content = Formatter.for(sexp[1]).new(sexp[1], @indentation, self).format
|
153
|
+
"#{content}.#{sexp[2]}"
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
private
|
158
|
+
|
159
|
+
def infix?
|
160
|
+
sexp.length == 4
|
161
|
+
end
|
162
|
+
|
163
|
+
def needs_parens?
|
164
|
+
parent&.type == :or || parent&.type == :and
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
class ClassFormatter < BaseFormatter
|
169
|
+
def format
|
170
|
+
content = Formatter.for(sexp[3]).new(sexp[3], indentation + 2, self).format
|
171
|
+
"#{indents}class #{sexp[1]}\n#{content}\n#{indents}end" # TODO handle inheritance
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
class CdeclFormatter < BaseFormatter
|
176
|
+
def format
|
177
|
+
content = Formatter.for(sexp[2]).new(sexp[2], indentation + 2, self).format
|
178
|
+
"#{indents}#{sexp[1]} = #{content}"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
class ArrayFormatter < BaseFormatter
|
183
|
+
def format
|
184
|
+
elements = sexp[1..-1].map do |nested_sexp|
|
185
|
+
Formatter.for(nested_sexp).new(nested_sexp, indentation + 2, self).format
|
186
|
+
end
|
187
|
+
|
188
|
+
"[#{elements.join(", ")}]"
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
data/prettyrb.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "prettyrb/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "prettyrb"
|
8
|
+
spec.version = Prettyrb::VERSION
|
9
|
+
spec.authors = ["Blake Williams"]
|
10
|
+
spec.email = ["blake@blakewilliams.me"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby source code formatter}
|
13
|
+
spec.description = %q{Ruby source code formatter}
|
14
|
+
spec.homepage = "https://github.com/blakewilliams/prettyrb"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
end
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_dependency "ruby_parser", "~> 3.14"
|
36
|
+
|
37
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
38
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
39
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prettyrb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Blake Williams
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ruby_parser
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.14'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.17'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.17'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
description: Ruby source code formatter
|
70
|
+
email:
|
71
|
+
- blake@blakewilliams.me
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- Gemfile.lock
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/prettyrb.rb
|
86
|
+
- lib/prettyrb/version.rb
|
87
|
+
- prettyrb.gemspec
|
88
|
+
homepage: https://github.com/blakewilliams/prettyrb
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata:
|
92
|
+
homepage_uri: https://github.com/blakewilliams/prettyrb
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubygems_version: 3.0.1
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Ruby source code formatter
|
112
|
+
test_files: []
|