syntax_tree 3.0.0 → 3.2.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: 28783cde4387bbc7d38d9de500564804cd885733abe59e017be73ce04981a854
4
- data.tar.gz: 6a54750102b8978df75e7f8a7d611e596067bb69863f91d5a8daa9aec8509c31
3
+ metadata.gz: a048616da9a9591130add971fae818025454369630a12bd4bb6386b3734de556
4
+ data.tar.gz: 16ea5138a4097551063ef9eeb6be7413c1f69e41c49f2b7e8a20e3dd4ab7f0a1
5
5
  SHA512:
6
- metadata.gz: c2f61f01bfa53604686798c96a53d8c98abd30145038db3b7f73f92c5c6b282447f3fe355382f6b3325b3ee98ce60ddcc5c102234b78e9c8d1fe9cbf006cb38d
7
- data.tar.gz: e5a63e7fdc4f7e70e354a5861e42b8c0dffbbd2ffde38754c0a1fca8a6033be2deecd52ea3ec5fb7a2aa05e0d07ab93d3b4d69762ca3c911f36a6e898ef2ffaf
6
+ metadata.gz: 680d5745acbcdcfeb9cbb92fb99927b142e9c5f4f7df65f909dc812baca22d14ede3beae973f3db05d6a81953af9f8c53113fe5ccdf86ae2592932079d82cc59
7
+ data.tar.gz: f6ac53a377cfd0150d6480611b9cc63714d3e893ef453d2b0881f4e54a8ba5e1450b5f88a38e9e7c7ed3e8eb18f45e6006fff2c5c714fd40db01cb99a615bc15
data/CHANGELOG.md CHANGED
@@ -6,6 +6,24 @@ 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.2.0] - 2022-07-19
10
+
11
+ ### Added
12
+
13
+ - [#116](https://github.com/ruby-syntax-tree/syntax_tree/pull/116) - Pass the `--print-width` option in the CLI to the language server.
14
+
15
+ ## [3.1.0] - 2022-07-19
16
+
17
+ ### Added
18
+
19
+ - [#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.
20
+
21
+ ## [3.0.1] - 2022-07-15
22
+
23
+ ### Changed
24
+
25
+ - [#112](https://github.com/ruby-syntax-tree/syntax_tree/pull/112) - Fix parallel CLI execution by not short-circuiting with the `||` operator.
26
+
9
27
  ## [3.0.0] - 2022-07-04
10
28
 
11
29
  ### Changed
@@ -288,7 +306,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
288
306
 
289
307
  - 🎉 Initial release! 🎉
290
308
 
291
- [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.9.0...HEAD
309
+ [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v3.2.0...HEAD
310
+ [3.2.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v3.1.0...v3.2.0
311
+ [3.1.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v3.0.1...v3.1.0
312
+ [3.0.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v3.0.0...v3.0.1
313
+ [3.0.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.9.0...v3.0.0
292
314
  [2.9.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.8.0...v2.9.0
293
315
  [2.8.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.7.1...v2.8.0
294
316
  [2.7.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.7.0...v2.7.1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_tree (3.0.0)
4
+ syntax_tree (3.2.0)
5
5
  prettier_print
6
6
 
7
7
  GEM
@@ -19,7 +19,7 @@ GEM
19
19
  rake (13.0.6)
20
20
  regexp_parser (2.5.0)
21
21
  rexml (3.2.5)
22
- rubocop (1.31.1)
22
+ rubocop (1.31.2)
23
23
  json (~> 2.3)
24
24
  parallel (~> 1.10)
25
25
  parser (>= 3.1.0.0)
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=...] [--print-width=NUMBER]")}
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
@@ -259,7 +300,7 @@ module SyntaxTree
259
300
  return 0
260
301
  when "lsp"
261
302
  require "syntax_tree/language_server"
262
- LanguageServer.new.run
303
+ LanguageServer.new(print_width: print_width).run
263
304
  return 0
264
305
  when "version"
265
306
  puts SyntaxTree::VERSION
@@ -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
@@ -315,23 +356,7 @@ module SyntaxTree
315
356
 
316
357
  # At the end, we're going to return whether or not this worker ever
317
358
  # encountered an error.
318
- errored =
319
- with_workers(queue) do |item|
320
- action.run(item)
321
- false
322
- rescue Parser::ParseError => error
323
- warn("Error: #{error.message}")
324
- highlight_error(error, item.source)
325
- true
326
- rescue Check::UnformattedError, Debug::NonIdempotentFormatError
327
- true
328
- rescue StandardError => error
329
- warn(error.message)
330
- warn(error.backtrace)
331
- true
332
- end
333
-
334
- if errored
359
+ if process_queue(queue, action)
335
360
  action.failure
336
361
  1
337
362
  else
@@ -342,13 +367,11 @@ module SyntaxTree
342
367
 
343
368
  private
344
369
 
345
- def with_workers(queue)
346
- # If the queue is just 1 item, then we're not going to bother going
347
- # through the whole ceremony of parallelizing the work.
348
- return yield queue.shift if queue.size == 1
349
-
370
+ # Processes each item in the queue with the given action. Returns whether
371
+ # or not any errors were encountered.
372
+ def process_queue(queue, action)
350
373
  workers =
351
- Etc.nprocessors.times.map do
374
+ [Etc.nprocessors, queue.size].min.times.map do
352
375
  Thread.new do
353
376
  # Propagate errors in the worker threads up to the parent thread.
354
377
  Thread.current.abort_on_exception = true
@@ -360,7 +383,25 @@ module SyntaxTree
360
383
 
361
384
  # While there is still work left to do, shift off the queue and
362
385
  # process the item.
363
- (errored ||= yield queue.shift) until queue.empty?
386
+ until queue.empty?
387
+ item = queue.shift
388
+ errored |=
389
+ begin
390
+ action.run(item)
391
+ false
392
+ rescue Parser::ParseError => error
393
+ warn("Error: #{error.message}")
394
+ highlight_error(error, item.source)
395
+ true
396
+ rescue Check::UnformattedError,
397
+ Debug::NonIdempotentFormatError
398
+ true
399
+ rescue StandardError => error
400
+ warn(error.message)
401
+ warn(error.backtrace)
402
+ true
403
+ end
404
+ end
364
405
 
365
406
  # At the end, we're going to return whether or not this worker
366
407
  # ever encountered an error.
@@ -368,7 +409,7 @@ module SyntaxTree
368
409
  end
369
410
  end
370
411
 
371
- workers.inject(false) { |accum, thread| accum || thread.value }
412
+ workers.map(&:value).inject(:|)
372
413
  end
373
414
 
374
415
  # Highlights a snippet from a source and parse error.
@@ -13,11 +13,16 @@ module SyntaxTree
13
13
  # stree lsp
14
14
  #
15
15
  class LanguageServer
16
- attr_reader :input, :output
16
+ attr_reader :input, :output, :print_width
17
17
 
18
- def initialize(input: $stdin, output: $stdout)
18
+ def initialize(
19
+ input: $stdin,
20
+ output: $stdout,
21
+ print_width: DEFAULT_PRINT_WIDTH
22
+ )
19
23
  @input = input.binmode
20
24
  @output = output.binmode
25
+ @print_width = print_width
21
26
  end
22
27
 
23
28
  # rubocop:disable Layout/LineLength
@@ -93,7 +98,7 @@ module SyntaxTree
93
98
  character: 0
94
99
  }
95
100
  },
96
- newText: SyntaxTree.format(source)
101
+ newText: SyntaxTree.format(source, print_width)
97
102
  }
98
103
  end
99
104
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SyntaxTree
4
- VERSION = "3.0.0"
4
+ VERSION = "3.2.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.0
4
+ version: 3.2.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-05 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