prettyrb 0.1.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,157 @@
1
+ require 'delegate'
2
+
3
+ module Prettyrb
4
+ class Writer
5
+ extend Forwardable
6
+
7
+ def_delegators :@writer, :indent_level
8
+
9
+ def initialize(builder, indent_level: 0, max_length: 100, force_break: false)
10
+ @builder = builder
11
+ @writer = writer
12
+ @indent_level = indent_level
13
+ @max_length = max_length
14
+ @force_break = force_break
15
+ end
16
+
17
+ def render_group(builder)
18
+ # render without force
19
+ # render only non-groups with force
20
+ # render only groups with force
21
+ # render non-groups and groups with force
22
+
23
+ # without breaks
24
+ content = builder.parts.compact.map do |part|
25
+ write_child(part, force_break: false)
26
+ end.compact.join("")
27
+
28
+ # break self
29
+ if content.split("\n").any? { |l| l.length > max_length }
30
+ content = builder.parts.compact.map do |part|
31
+ write_child(part, force_break: !part.is_a?(Document::Group))
32
+ end.join("")
33
+ end
34
+
35
+ # break child groups if over length, but not self
36
+ if content.split("\n").any? { |l| l.length > max_length }
37
+ content = builder.parts.compact.map do |part|
38
+ if part.is_a?(Document::Group)
39
+ possible_output = write_child(part, force_break: false)
40
+
41
+ if possible_output && possible_output.split("\n").any? { |l| l.length > max_length }
42
+ content = write_child(part, force_break: true)
43
+ else
44
+ content = possible_output
45
+ end
46
+ else
47
+ write_child(part, force_break: false)
48
+ end
49
+ end.join("")
50
+ end
51
+
52
+ # always break self, attempt to break child groups too
53
+ if content.split("\n").any? { |l| l.length > max_length }
54
+ content = builder.parts.compact.map do |part|
55
+ if part.is_a?(Document::Group)
56
+ possible_output = write_child(part, force_break: false)
57
+
58
+ if possible_output && possible_output.split("\n").any? { |l| l.length > max_length }
59
+ content = write_child(part, force_break: true)
60
+ else
61
+ content = possible_output
62
+ end
63
+ else
64
+ write_child(part, force_break: true)
65
+ end
66
+ end.join("")
67
+ end
68
+
69
+ content
70
+ end
71
+
72
+ def to_s
73
+ case builder
74
+ when Document::Join
75
+ separator = break_up? ? builder.separator : builder.separator + " "
76
+ parts = builder.parts.compact
77
+
78
+ output = []
79
+ parts.each do |part|
80
+ output << write_child(part)
81
+ output << separator if part != parts.last
82
+ end
83
+ output.join("")
84
+ when Document::Concat
85
+ builder.parts.compact.map { |p| write_child(p) }.compact.join("")
86
+ when Document::Indent
87
+ builder.parts.compact.map do |part|
88
+ if builder.only_when_break && !break_up?
89
+ write_child(part)
90
+ else
91
+ write_child(part, indent_level: indent_level + 1)
92
+ end
93
+ end.compact.join("")
94
+ when Document::Dedent
95
+ builder.parts.compact.map do |part|
96
+ write_child(part, indent_level: indent_level - 1)
97
+ end.compact.join("")
98
+ when Document::Group
99
+ render_group(builder)
100
+ when Document::Hardline
101
+ if builder.skip_indent
102
+ "\n" * builder.count
103
+ else
104
+ "\n" * builder.count + indent_string
105
+ end
106
+ when Document::Softline
107
+ if break_up?
108
+ "\n" + indent_string
109
+ else
110
+ builder.fallback
111
+ end
112
+ when Document::IfBreak
113
+ if break_up?
114
+ builder.with_break
115
+ else
116
+ builder.without_break
117
+ end
118
+ when Symbol, String
119
+ builder.to_s
120
+ when nil
121
+ nil
122
+ else
123
+ raise "unhandled type: #{builder.class}"
124
+ end
125
+ end
126
+
127
+ protected
128
+
129
+ attr_reader :builder, :writer, :indent_level, :break_group_levels, :max_length, :force_break
130
+
131
+ private
132
+
133
+ def break_up?
134
+ force_break
135
+ end
136
+
137
+ def write_softline?
138
+ break_up?
139
+ end
140
+
141
+ def indent_string(extra: 0)
142
+ " " * (indent_level + extra)
143
+ end
144
+
145
+ def write_child(child, indent_level: nil, group_level: nil, force_break: nil)
146
+ indent_level ||= self.indent_level
147
+ force_break ||= self.force_break
148
+
149
+ self.class.new(
150
+ child,
151
+ indent_level: indent_level,
152
+ max_length: max_length,
153
+ force_break: force_break,
154
+ ).to_s
155
+ end
156
+ end
157
+ end
@@ -32,9 +32,12 @@ Gem::Specification.new do |spec|
32
32
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
33
  spec.require_paths = ["lib"]
34
34
 
35
- spec.add_dependency "ruby_parser", "~> 3.14"
35
+ spec.add_dependency "parser", "~> 2.7.0.5"
36
+ spec.add_dependency "thor"
36
37
 
37
- spec.add_development_dependency "bundler", "~> 1.17"
38
- spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "bundler", "~> 2.1.4"
39
+ spec.add_development_dependency "rake", "~> 12.3.3"
39
40
  spec.add_development_dependency "minitest", "~> 5.0"
41
+ spec.add_development_dependency "pry", "~> 0.13"
42
+ spec.add_development_dependency "simplecov", "~> 0.18"
40
43
  end
metadata CHANGED
@@ -1,57 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prettyrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Williams
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-28 00:00:00.000000000 Z
11
+ date: 2020-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: ruby_parser
14
+ name: parser
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.14'
19
+ version: 2.7.0.5
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3.14'
26
+ version: 2.7.0.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '1.17'
47
+ version: 2.1.4
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '1.17'
54
+ version: 2.1.4
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - "~>"
46
60
  - !ruby/object:Gem::Version
47
- version: '10.0'
61
+ version: 12.3.3
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
- version: '10.0'
68
+ version: 12.3.3
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: minitest
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -66,14 +80,45 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.13'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.13'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.18'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.18'
69
111
  description: Ruby source code formatter
70
112
  email:
71
113
  - blake@blakewilliams.me
72
- executables: []
114
+ executables:
115
+ - prettyrb
73
116
  extensions: []
74
117
  extra_rdoc_files: []
75
118
  files:
119
+ - ".github/workflows/ruby.yml"
76
120
  - ".gitignore"
121
+ - ".ruby-version"
77
122
  - ".travis.yml"
78
123
  - Gemfile
79
124
  - Gemfile.lock
@@ -82,8 +127,26 @@ files:
82
127
  - Rakefile
83
128
  - bin/console
84
129
  - bin/setup
130
+ - exe/prettyrb
85
131
  - lib/prettyrb.rb
132
+ - lib/prettyrb/builder.rb
133
+ - lib/prettyrb/cli.rb
134
+ - lib/prettyrb/document.rb
135
+ - lib/prettyrb/formatter.rb
136
+ - lib/prettyrb/nodes/and_node.rb
137
+ - lib/prettyrb/nodes/base_node.rb
138
+ - lib/prettyrb/nodes/def_node.rb
139
+ - lib/prettyrb/nodes/dstr_node.rb
140
+ - lib/prettyrb/nodes/if_node.rb
141
+ - lib/prettyrb/nodes/logical_operator_helper.rb
142
+ - lib/prettyrb/nodes/or_node.rb
143
+ - lib/prettyrb/nodes/regexp_node.rb
144
+ - lib/prettyrb/nodes/send_node.rb
145
+ - lib/prettyrb/nodes/str_node.rb
146
+ - lib/prettyrb/nodes/string_helper.rb
86
147
  - lib/prettyrb/version.rb
148
+ - lib/prettyrb/visitor.rb
149
+ - lib/prettyrb/writer.rb
87
150
  - prettyrb.gemspec
88
151
  homepage: https://github.com/blakewilliams/prettyrb
89
152
  licenses: