syntax_tree 3.0.1 → 3.1.0

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
  SHA256:
3
- metadata.gz: 499aba04c732c3101f3e3ebb7d487a19842781a0a97b67fad047c2cf9bbe2bcb
4
- data.tar.gz: dc9e28898951ae74f3a053b261b8d1e752567917bbb8eb34a4aa22af6b49b716
3
+ metadata.gz: 3364d1c9d427dd3e40d76f2598569399f3099870f247e1fc740d0d5c06d72e36
4
+ data.tar.gz: 24272efb6a8272b32446a3865425ed74cbb223a64798611012732c102321dfd6
5
5
  SHA512:
6
- metadata.gz: 3a8cdd3933efaaa8898cfa25bebd3e52d3b6632081319d8ddded59dacb996ebd11232d1060e408eb93bdb4211aa4d2775bfce970ba868264f7846dcb63873e5e
7
- data.tar.gz: 673c601ce6a25cc4875b2041710b4a85f5114a24deb1cb5e3bf69a464e072f72f24f2e13cb3aa89d95076a5d7643beb6469ccd669018e75d9ac998aeb0a10ddf
6
+ metadata.gz: b824e69bf60fbf5c0d4a77cb4981243913cdfd22b09bcfd88c0bdd758b92be9d89462cf106b1bce3098e777b811aa87bc076dc0eac3369f33009719381290135
7
+ data.tar.gz: 9a5dad257dd35c7a45fbef150c189e01823595ec079df9e09aafe70347a0e41278d9cca3cb5b184b49b789d9460d5332e9c516629122d08f529b0961d42a2284
data/CHANGELOG.md CHANGED
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [3.1.0] - 2022-07-19
10
+
11
+ ### Added
12
+
13
+ - [#115](https://github.com/ruby-syntax-tree/syntax_tree/pull/115) - Support the `--print-width` option in the CLI for the actions that support it.
14
+
9
15
  ## [3.0.1] - 2022-07-15
10
16
 
11
17
  ### Changed
@@ -294,7 +300,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
294
300
 
295
301
  - 🎉 Initial release! 🎉
296
302
 
297
- [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v3.0.1...HEAD
303
+ [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v3.1.0...HEAD
304
+ [3.1.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v3.0.1...v3.1.0
298
305
  [3.0.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v3.0.0...v3.0.1
299
306
  [3.0.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.9.0...v3.0.0
300
307
  [2.9.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.8.0...v2.9.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_tree (3.0.1)
4
+ syntax_tree (3.1.0)
5
5
  prettier_print
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -118,6 +118,12 @@ If there are files with unformatted code, you will receive:
118
118
  The listed files did not match the expected format.
119
119
  ```
120
120
 
121
+ To change the print width that you are checking against, specify the `--print-width` option, as in:
122
+
123
+ ```sh
124
+ stree check --print-width=100 path/to/file.rb
125
+ ```
126
+
121
127
  ### format
122
128
 
123
129
  This command will output the formatted version of each of the listed files. Importantly, it will not write that content back to the source files. It is meant to display the formatted version only.
@@ -132,6 +138,12 @@ For a file that contains `1 + 1`, you will receive:
132
138
  1 + 1
133
139
  ```
134
140
 
141
+ To change the print width that you are formatting with, specify the `--print-width` option, as in:
142
+
143
+ ```sh
144
+ stree format --print-width=100 path/to/file.rb
145
+ ```
146
+
135
147
  ### json
136
148
 
137
149
  This command will output a JSON representation of the syntax tree that is functionally equivalent to the input. This is mostly used in contexts where you need to access the tree from JavaScript or serialize it over a network.
@@ -213,6 +225,12 @@ This will list every file that is being formatted. It will output light gray if
213
225
  path/to/file.rb 0ms
214
226
  ```
215
227
 
228
+ To change the print width that you are writing with, specify the `--print-width` option, as in:
229
+
230
+ ```sh
231
+ stree write --print-width=100 path/to/file.rb
232
+ ```
233
+
216
234
  ## Library
217
235
 
218
236
  Syntax Tree can be used as a library to access the syntax tree underlying Ruby source code.
@@ -91,9 +91,17 @@ module SyntaxTree
91
91
  class UnformattedError < StandardError
92
92
  end
93
93
 
94
+ attr_reader :print_width
95
+
96
+ def initialize(print_width:)
97
+ @print_width = print_width
98
+ end
99
+
94
100
  def run(item)
95
101
  source = item.source
96
- raise UnformattedError if source != item.handler.format(source)
102
+ if source != item.handler.format(source, print_width)
103
+ raise UnformattedError
104
+ end
97
105
  rescue StandardError
98
106
  warn("[#{Color.yellow("warn")}] #{item.filepath}")
99
107
  raise
@@ -114,13 +122,21 @@ module SyntaxTree
114
122
  class NonIdempotentFormatError < StandardError
115
123
  end
116
124
 
125
+ attr_reader :print_width
126
+
127
+ def initialize(print_width:)
128
+ @print_width = print_width
129
+ end
130
+
117
131
  def run(item)
118
132
  handler = item.handler
119
133
 
120
134
  warning = "[#{Color.yellow("warn")}] #{item.filepath}"
121
- formatted = handler.format(item.source)
135
+ formatted = handler.format(item.source, print_width)
122
136
 
123
- raise NonIdempotentFormatError if formatted != handler.format(formatted)
137
+ if formatted != handler.format(formatted, print_width)
138
+ raise NonIdempotentFormatError
139
+ end
124
140
  rescue StandardError
125
141
  warn(warning)
126
142
  raise
@@ -148,8 +164,14 @@ module SyntaxTree
148
164
 
149
165
  # An action of the CLI that formats the input source and prints it out.
150
166
  class Format < Action
167
+ attr_reader :print_width
168
+
169
+ def initialize(print_width:)
170
+ @print_width = print_width
171
+ end
172
+
151
173
  def run(item)
152
- puts item.handler.format(item.source)
174
+ puts item.handler.format(item.source, print_width)
153
175
  end
154
176
  end
155
177
 
@@ -173,12 +195,18 @@ module SyntaxTree
173
195
  # An action of the CLI that formats the input source and writes the
174
196
  # formatted output back to the file.
175
197
  class Write < Action
198
+ attr_reader :print_width
199
+
200
+ def initialize(print_width:)
201
+ @print_width = print_width
202
+ end
203
+
176
204
  def run(item)
177
205
  filepath = item.filepath
178
206
  start = Time.now
179
207
 
180
208
  source = item.source
181
- formatted = item.handler.format(source)
209
+ formatted = item.handler.format(source, print_width)
182
210
  File.write(filepath, formatted) if filepath != :stdin
183
211
 
184
212
  color = source == formatted ? Color.gray(filepath) : filepath
@@ -194,43 +222,44 @@ module SyntaxTree
194
222
  # The help message displayed if the input arguments are not correctly
195
223
  # ordered or formatted.
196
224
  HELP = <<~HELP
197
- #{Color.bold("stree ast [OPTIONS] [FILE]")}
225
+ #{Color.bold("stree ast [--plugins=...] [--print-width=NUMBER] FILE")}
198
226
  Print out the AST corresponding to the given files
199
227
 
200
- #{Color.bold("stree check [OPTIONS] [FILE]")}
228
+ #{Color.bold("stree check [--plugins=...] [--print-width=NUMBER] FILE")}
201
229
  Check that the given files are formatted as syntax tree would format them
202
230
 
203
- #{Color.bold("stree debug [OPTIONS] [FILE]")}
231
+ #{Color.bold("stree debug [--plugins=...] [--print-width=NUMBER] FILE")}
204
232
  Check that the given files can be formatted idempotently
205
233
 
206
- #{Color.bold("stree doc [OPTIONS] [FILE]")}
234
+ #{Color.bold("stree doc [--plugins=...] FILE")}
207
235
  Print out the doc tree that would be used to format the given files
208
236
 
209
- #{Color.bold("stree format [OPTIONS] [FILE]")}
237
+ #{Color.bold("stree format [--plugins=...] [--print-width=NUMBER] FILE")}
210
238
  Print out the formatted version of the given files
211
239
 
212
- #{Color.bold("stree json [OPTIONS] [FILE]")}
240
+ #{Color.bold("stree json [--plugins=...] FILE")}
213
241
  Print out the JSON representation of the given files
214
242
 
215
- #{Color.bold("stree match [OPTIONS] [FILE]")}
243
+ #{Color.bold("stree match [--plugins=...] FILE")}
216
244
  Print out a pattern-matching Ruby expression that would match the given files
217
245
 
218
246
  #{Color.bold("stree help")}
219
247
  Display this help message
220
248
 
221
- #{Color.bold("stree lsp [OPTIONS]")}
249
+ #{Color.bold("stree lsp [--plugins=...]")}
222
250
  Run syntax tree in language server mode
223
251
 
224
252
  #{Color.bold("stree version")}
225
253
  Output the current version of syntax tree
226
254
 
227
- #{Color.bold("stree write [OPTIONS] [FILE]")}
255
+ #{Color.bold("stree write [--plugins=...] [--print-width=NUMBER] FILE")}
228
256
  Read, format, and write back the source of the given files
229
257
 
230
- [OPTIONS]
231
-
232
258
  --plugins=...
233
259
  A comma-separated list of plugins to load.
260
+
261
+ --print-width=NUMBER
262
+ The maximum line width to use when formatting.
234
263
  HELP
235
264
 
236
265
  class << self
@@ -238,19 +267,31 @@ module SyntaxTree
238
267
  # passed to the invocation.
239
268
  def run(argv)
240
269
  name, *arguments = argv
241
-
242
- # If there are any plugins specified on the command line, then load them
243
- # by requiring them here. We do this by transforming something like
244
- #
245
- # stree format --plugins=haml template.haml
246
- #
247
- # into
248
- #
249
- # require "syntax_tree/haml"
250
- #
251
- if arguments.first&.start_with?("--plugins=")
252
- plugins = arguments.shift[/^--plugins=(.*)$/, 1]
253
- plugins.split(",").each { |plugin| require "syntax_tree/#{plugin}" }
270
+ print_width = DEFAULT_PRINT_WIDTH
271
+
272
+ while arguments.first&.start_with?("--")
273
+ case (argument = arguments.shift)
274
+ when /^--plugins=(.+)$/
275
+ # If there are any plugins specified on the command line, then load
276
+ # them by requiring them here. We do this by transforming something
277
+ # like
278
+ #
279
+ # stree format --plugins=haml template.haml
280
+ #
281
+ # into
282
+ #
283
+ # require "syntax_tree/haml"
284
+ #
285
+ $1.split(",").each { |plugin| require "syntax_tree/#{plugin}" }
286
+ when /^--print-width=(\d+)$/
287
+ # If there is a print width specified on the command line, then
288
+ # parse that out here and use it when formatting.
289
+ print_width = Integer($1)
290
+ else
291
+ warn("Unknown CLI option: #{argument}")
292
+ warn(HELP)
293
+ return 1
294
+ end
254
295
  end
255
296
 
256
297
  case name
@@ -271,9 +312,9 @@ module SyntaxTree
271
312
  when "a", "ast"
272
313
  AST.new
273
314
  when "c", "check"
274
- Check.new
315
+ Check.new(print_width: print_width)
275
316
  when "debug"
276
- Debug.new
317
+ Debug.new(print_width: print_width)
277
318
  when "doc"
278
319
  Doc.new
279
320
  when "j", "json"
@@ -281,9 +322,9 @@ module SyntaxTree
281
322
  when "m", "match"
282
323
  Match.new
283
324
  when "f", "format"
284
- Format.new
325
+ Format.new(print_width: print_width)
285
326
  when "w", "write"
286
- Write.new
327
+ Write.new(print_width: print_width)
287
328
  else
288
329
  warn(HELP)
289
330
  return 1
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SyntaxTree
4
- VERSION = "3.0.1"
4
+ VERSION = "3.1.0"
5
5
  end
data/lib/syntax_tree.rb CHANGED
@@ -29,6 +29,11 @@ module SyntaxTree
29
29
  HANDLERS = {}
30
30
  HANDLERS.default = SyntaxTree
31
31
 
32
+ # This is the default print width when formatting. It can be overridden in the
33
+ # CLI by passing the --print-width option or here in the API by passing the
34
+ # optional second argument to ::format.
35
+ DEFAULT_PRINT_WIDTH = 80
36
+
32
37
  # This is a hook provided so that plugins can register themselves as the
33
38
  # handler for a particular file type.
34
39
  def self.register_handler(extension, handler)
@@ -43,7 +48,7 @@ module SyntaxTree
43
48
  end
44
49
 
45
50
  # Parses the given source and returns the formatted source.
46
- def self.format(source, maxwidth = 80)
51
+ def self.format(source, maxwidth = DEFAULT_PRINT_WIDTH)
47
52
  formatter = Formatter.new(source, [], maxwidth)
48
53
  parse(source).format(formatter)
49
54
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syntax_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Newton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-15 00:00:00.000000000 Z
11
+ date: 2022-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prettier_print