prettier_print 1.0.2 → 1.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: 81a51b0f4acf29f5f26a1eb71aa7f682c48a1a4cde776eb27140304a0b2e970f
4
- data.tar.gz: 7b72b908730bcff4c4c71205e2c3ee8f3ea77d791873543195c55efb07a78320
3
+ metadata.gz: cd1acc23437a5cf57c8f770a8f6ae6273d14c4716a64566f7966c19fe0f63276
4
+ data.tar.gz: bee4b7fcdd680e7a3747ad025054830426638f166bd14229e1a3386350fd542e
5
5
  SHA512:
6
- metadata.gz: c5f85439e98bb48f9357b7ef1fbb98f5f85153d921e220ffd77c6e0f4d9aad76975374f8ba4f056770622d807232e5dbb5cdf43938e609f5d22d02a64a67e4fb
7
- data.tar.gz: f4de2a0f56adb16425bc26861e1a20c05b29db8f8e47fce164e5c1695e731f878e048ab6e042f94f7774fab9b55a6e32c0eb4ac058d82e56c7f640ef57124a6a
6
+ metadata.gz: e59fb5d9131f40d75e458b45ad069329da4ae83df63f3111f7ffba01347d1ddf1a214f636a4bf5afa08502458c171ed7d17a8e358294ef101c9f1db68a924e20
7
+ data.tar.gz: ae31224da56d3983e6c3fa4329dbaa1bb86a4cf8b500852cfc105cfb01d69b11d61b285860e924768664f652964e300c72dbca141441e3f31222dad7be1545e7
@@ -0,0 +1,22 @@
1
+ name: Dependabot auto-merge
2
+ on: pull_request
3
+
4
+ permissions:
5
+ contents: write
6
+ pull-requests: write
7
+
8
+ jobs:
9
+ dependabot:
10
+ runs-on: ubuntu-latest
11
+ if: ${{ github.actor == 'dependabot[bot]' }}
12
+ steps:
13
+ - name: Dependabot metadata
14
+ id: metadata
15
+ uses: dependabot/fetch-metadata@v1.3.3
16
+ with:
17
+ github-token: "${{ secrets.GITHUB_TOKEN }}"
18
+ - name: Enable auto-merge for Dependabot PRs
19
+ run: gh pr merge --auto --merge "$PR_URL"
20
+ env:
21
+ PR_URL: ${{github.event.pull_request.html_url}}
22
+ GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
@@ -24,19 +24,3 @@ jobs:
24
24
  ruby-version: ${{ matrix.ruby }}
25
25
  - name: Test
26
26
  run: bundle exec rake test
27
-
28
- automerge:
29
- name: AutoMerge
30
- needs:
31
- - ci
32
- runs-on: ubuntu-latest
33
- if: github.event_name == 'pull_request_target' && github.actor == 'dependabot[bot]'
34
- steps:
35
- - uses: actions/github-script@v3
36
- with:
37
- script: |
38
- github.pulls.merge({
39
- owner: context.payload.repository.owner.login,
40
- repo: context.payload.repository.name,
41
- pull_number: context.payload.pull_request.number
42
- })
data/CHANGELOG.md CHANGED
@@ -6,6 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.2.0] - 2022-12-23
10
+
11
+ ### Added
12
+
13
+ - You can now supply the base indentation level for the output buffer.
14
+
15
+ ## [1.1.0] - 2022-11-08
16
+
17
+ ### Added
18
+
19
+ - `prettier_print` now works with Ractors.
20
+
9
21
  ## [1.0.2] - 2022-10-19
10
22
 
11
23
  ### Changed
@@ -43,7 +55,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
43
55
 
44
56
  - 🎉 Initial release! 🎉
45
57
 
46
- [unreleased]: https://github.com/ruby-syntax-tree/prettier_print/compare/v1.0.2...HEAD
58
+ [unreleased]: https://github.com/ruby-syntax-tree/prettier_print/compare/v1.2.0...HEAD
59
+ [1.2.0]: https://github.com/ruby-syntax-tree/prettier_print/compare/v1.1.0...v1.2.0
60
+ [1.1.0]: https://github.com/ruby-syntax-tree/prettier_print/compare/v1.0.2...v1.1.0
47
61
  [1.0.2]: https://github.com/ruby-syntax-tree/prettier_print/compare/v1.0.1...v1.0.2
48
62
  [1.0.1]: https://github.com/ruby-syntax-tree/prettier_print/compare/v1.0.0...v1.0.1
49
63
  [1.0.0]: https://github.com/ruby-syntax-tree/prettier_print/compare/v0.1.0...v1.0.0
data/Gemfile CHANGED
@@ -4,5 +4,6 @@ source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
6
 
7
+ gem "simplecov"
7
8
  gem "rake"
8
9
  gem "test-unit"
data/Gemfile.lock CHANGED
@@ -1,14 +1,21 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prettier_print (1.0.2)
4
+ prettier_print (1.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- power_assert (2.0.1)
9
+ docile (1.4.0)
10
+ power_assert (2.0.2)
10
11
  rake (13.0.6)
11
- test-unit (3.5.5)
12
+ simplecov (0.21.2)
13
+ docile (~> 1.1)
14
+ simplecov-html (~> 0.11)
15
+ simplecov_json_formatter (~> 0.1)
16
+ simplecov-html (0.12.3)
17
+ simplecov_json_formatter (0.1.4)
18
+ test-unit (3.5.7)
12
19
  power_assert
13
20
 
14
21
  PLATFORMS
@@ -19,6 +26,7 @@ PLATFORMS
19
26
  DEPENDENCIES
20
27
  prettier_print!
21
28
  rake
29
+ simplecov
22
30
  test-unit
23
31
 
24
32
  BUNDLED WITH
@@ -0,0 +1,166 @@
1
+ # frozen_string_literal: true
2
+
3
+ class PrettierPrint
4
+ # PrettierPrint::SingleLine is used by PrettierPrint.singleline_format
5
+ #
6
+ # It is passed to be similar to a PrettierPrint object itself, by responding to
7
+ # all of the same print tree node builder methods, as well as the #flush
8
+ # method.
9
+ #
10
+ # The significant difference here is that there are no line breaks in the
11
+ # output. If an IfBreak node is used, only the flat contents are printed.
12
+ # LineSuffix nodes are printed at the end of the buffer when #flush is called.
13
+ class SingleLine
14
+ # The output object. It stores rendered text and should respond to <<.
15
+ attr_reader :output
16
+
17
+ # The current array of contents that the print tree builder methods should
18
+ # append to.
19
+ attr_reader :target
20
+
21
+ # A buffer output that wraps any calls to line_suffix that will be flushed
22
+ # at the end of printing.
23
+ attr_reader :line_suffixes
24
+
25
+ # Create a PrettierPrint::SingleLine object
26
+ #
27
+ # Arguments:
28
+ # * +output+ - String (or similar) to store rendered text. Needs to respond
29
+ # to '<<'.
30
+ # * +maxwidth+ - Argument position expected to be here for compatibility.
31
+ # This argument is a noop.
32
+ # * +newline+ - Argument position expected to be here for compatibility.
33
+ # This argument is a noop.
34
+ def initialize(output, _maxwidth = nil, _newline = nil)
35
+ @output = Buffer.for(output)
36
+ @target = @output
37
+ @line_suffixes = Buffer::ArrayBuffer.new
38
+ end
39
+
40
+ # Flushes the line suffixes onto the output buffer.
41
+ def flush
42
+ line_suffixes.output.each { |doc| output << doc }
43
+ end
44
+
45
+ # --------------------------------------------------------------------------
46
+ # Markers node builders
47
+ # --------------------------------------------------------------------------
48
+
49
+ # Appends +separator+ to the text to be output. By default +separator+ is
50
+ # ' '
51
+ #
52
+ # The +width+, +indent+, and +force+ arguments are here for compatibility.
53
+ # They are all noop arguments.
54
+ def breakable(
55
+ separator = " ",
56
+ _width = separator.length,
57
+ indent: nil,
58
+ force: nil
59
+ )
60
+ target << separator
61
+ end
62
+
63
+ # Here for compatibility, does nothing.
64
+ def break_parent
65
+ end
66
+
67
+ # Appends +separator+ to the output buffer. +width+ is a noop here for
68
+ # compatibility.
69
+ def fill_breakable(separator = " ", _width = separator.length)
70
+ target << separator
71
+ end
72
+
73
+ # Immediately trims the output buffer.
74
+ def trim
75
+ target.trim!
76
+ end
77
+
78
+ # --------------------------------------------------------------------------
79
+ # Container node builders
80
+ # --------------------------------------------------------------------------
81
+
82
+ # Opens a block for grouping objects to be pretty printed.
83
+ #
84
+ # Arguments:
85
+ # * +indent+ - noop argument. Present for compatibility.
86
+ # * +open_obj+ - text appended before the &block. Default is ''
87
+ # * +close_obj+ - text appended after the &block. Default is ''
88
+ # * +open_width+ - noop argument. Present for compatibility.
89
+ # * +close_width+ - noop argument. Present for compatibility.
90
+ def group(
91
+ _indent = nil,
92
+ open_object = "",
93
+ close_object = "",
94
+ _open_width = nil,
95
+ _close_width = nil
96
+ )
97
+ target << open_object
98
+ yield
99
+ target << close_object
100
+ end
101
+
102
+ # A class that wraps the ability to call #if_flat. The contents of the
103
+ # #if_flat block are executed immediately, so effectively this class and the
104
+ # #if_break method that triggers it are unnecessary, but they're here to
105
+ # maintain compatibility.
106
+ class IfBreakBuilder
107
+ def if_flat
108
+ yield
109
+ end
110
+ end
111
+
112
+ # Effectively unnecessary, but here for compatibility.
113
+ def if_break
114
+ IfBreakBuilder.new
115
+ end
116
+
117
+ # Also effectively unnecessary, but here for compatibility.
118
+ def if_flat
119
+ end
120
+
121
+ # A noop that immediately yields.
122
+ def indent
123
+ yield
124
+ end
125
+
126
+ # Changes the target output buffer to the line suffix output buffer which
127
+ # will get flushed at the end of printing.
128
+ def line_suffix
129
+ previous_target, @target = @target, line_suffixes
130
+ yield
131
+ @target = previous_target
132
+ end
133
+
134
+ # Takes +indent+ arg, but does nothing with it.
135
+ #
136
+ # Yields to a block.
137
+ def nest(_indent)
138
+ yield
139
+ end
140
+
141
+ # Add +object+ to the text to be output.
142
+ #
143
+ # +width+ argument is here for compatibility. It is a noop argument.
144
+ def text(object = "", _width = nil)
145
+ target << object
146
+ end
147
+ end
148
+
149
+ # This is similar to PrettierPrint::format but the result has no breaks.
150
+ #
151
+ # +maxwidth+, +newline+ and +genspace+ are ignored.
152
+ #
153
+ # The invocation of +breakable+ in the block doesn't break a line and is
154
+ # treated as just an invocation of +text+.
155
+ #
156
+ def self.singleline_format(
157
+ output = +"",
158
+ _maxwidth = nil,
159
+ _newline = nil,
160
+ _genspace = nil
161
+ )
162
+ q = SingleLine.new(output)
163
+ yield q
164
+ output
165
+ end
166
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class PrettierPrint
4
- VERSION = "1.0.2"
4
+ VERSION = "1.2.0"
5
5
  end
@@ -122,10 +122,10 @@ class PrettierPrint
122
122
 
123
123
  # Below here are the most common combination of options that are created when
124
124
  # creating new breakables. They are here to cut down on some allocations.
125
- BREAKABLE_SPACE = Breakable.new(" ", 1, indent: true, force: false)
126
- BREAKABLE_EMPTY = Breakable.new("", 0, indent: true, force: false)
127
- BREAKABLE_FORCE = Breakable.new(" ", 1, indent: true, force: true)
128
- BREAKABLE_RETURN = Breakable.new(" ", 1, indent: false, force: true)
125
+ BREAKABLE_SPACE = Breakable.new(" ", 1, indent: true, force: false).freeze
126
+ BREAKABLE_EMPTY = Breakable.new("", 0, indent: true, force: false).freeze
127
+ BREAKABLE_FORCE = Breakable.new(" ", 1, indent: true, force: true).freeze
128
+ BREAKABLE_RETURN = Breakable.new(" ", 1, indent: false, force: true).freeze
129
129
 
130
130
  # A node in the print tree that forces the surrounding group to print out in
131
131
  # the "break" mode as opposed to the "flat" mode. Useful for when you need to
@@ -138,7 +138,7 @@ class PrettierPrint
138
138
 
139
139
  # Since there's really no difference in these instances, just using the same
140
140
  # one saves on some allocations.
141
- BREAK_PARENT = BreakParent.new
141
+ BREAK_PARENT = BreakParent.new.freeze
142
142
 
143
143
  # A node in the print tree that represents a group of items which the printer
144
144
  # should try to fit onto one line. This is the basic command to tell the
@@ -267,7 +267,7 @@ class PrettierPrint
267
267
 
268
268
  # Since all of the instances here are the same, we can reuse the same one to
269
269
  # cut down on allocations.
270
- TRIM = Trim.new
270
+ TRIM = Trim.new.freeze
271
271
 
272
272
  # When building up the contents in the output buffer, it's convenient to be
273
273
  # able to trim trailing whitespace before newlines. If the output object is a
@@ -338,151 +338,6 @@ class PrettierPrint
338
338
  end
339
339
  end
340
340
 
341
- # PrettierPrint::SingleLine is used by PrettierPrint.singleline_format
342
- #
343
- # It is passed to be similar to a PrettierPrint object itself, by responding to
344
- # all of the same print tree node builder methods, as well as the #flush
345
- # method.
346
- #
347
- # The significant difference here is that there are no line breaks in the
348
- # output. If an IfBreak node is used, only the flat contents are printed.
349
- # LineSuffix nodes are printed at the end of the buffer when #flush is called.
350
- class SingleLine
351
- # The output object. It stores rendered text and should respond to <<.
352
- attr_reader :output
353
-
354
- # The current array of contents that the print tree builder methods should
355
- # append to.
356
- attr_reader :target
357
-
358
- # A buffer output that wraps any calls to line_suffix that will be flushed
359
- # at the end of printing.
360
- attr_reader :line_suffixes
361
-
362
- # Create a PrettierPrint::SingleLine object
363
- #
364
- # Arguments:
365
- # * +output+ - String (or similar) to store rendered text. Needs to respond
366
- # to '<<'.
367
- # * +maxwidth+ - Argument position expected to be here for compatibility.
368
- # This argument is a noop.
369
- # * +newline+ - Argument position expected to be here for compatibility.
370
- # This argument is a noop.
371
- def initialize(output, _maxwidth = nil, _newline = nil)
372
- @output = Buffer.for(output)
373
- @target = @output
374
- @line_suffixes = Buffer::ArrayBuffer.new
375
- end
376
-
377
- # Flushes the line suffixes onto the output buffer.
378
- def flush
379
- line_suffixes.output.each { |doc| output << doc }
380
- end
381
-
382
- # --------------------------------------------------------------------------
383
- # Markers node builders
384
- # --------------------------------------------------------------------------
385
-
386
- # Appends +separator+ to the text to be output. By default +separator+ is
387
- # ' '
388
- #
389
- # The +width+, +indent+, and +force+ arguments are here for compatibility.
390
- # They are all noop arguments.
391
- def breakable(
392
- separator = " ",
393
- _width = separator.length,
394
- indent: nil,
395
- force: nil
396
- )
397
- target << separator
398
- end
399
-
400
- # Here for compatibility, does nothing.
401
- def break_parent
402
- end
403
-
404
- # Appends +separator+ to the output buffer. +width+ is a noop here for
405
- # compatibility.
406
- def fill_breakable(separator = " ", _width = separator.length)
407
- target << separator
408
- end
409
-
410
- # Immediately trims the output buffer.
411
- def trim
412
- target.trim!
413
- end
414
-
415
- # --------------------------------------------------------------------------
416
- # Container node builders
417
- # --------------------------------------------------------------------------
418
-
419
- # Opens a block for grouping objects to be pretty printed.
420
- #
421
- # Arguments:
422
- # * +indent+ - noop argument. Present for compatibility.
423
- # * +open_obj+ - text appended before the &block. Default is ''
424
- # * +close_obj+ - text appended after the &block. Default is ''
425
- # * +open_width+ - noop argument. Present for compatibility.
426
- # * +close_width+ - noop argument. Present for compatibility.
427
- def group(
428
- _indent = nil,
429
- open_object = "",
430
- close_object = "",
431
- _open_width = nil,
432
- _close_width = nil
433
- )
434
- target << open_object
435
- yield
436
- target << close_object
437
- end
438
-
439
- # A class that wraps the ability to call #if_flat. The contents of the
440
- # #if_flat block are executed immediately, so effectively this class and the
441
- # #if_break method that triggers it are unnecessary, but they're here to
442
- # maintain compatibility.
443
- class IfBreakBuilder
444
- def if_flat
445
- yield
446
- end
447
- end
448
-
449
- # Effectively unnecessary, but here for compatibility.
450
- def if_break
451
- IfBreakBuilder.new
452
- end
453
-
454
- # Also effectively unnecessary, but here for compatibility.
455
- def if_flat
456
- end
457
-
458
- # A noop that immediately yields.
459
- def indent
460
- yield
461
- end
462
-
463
- # Changes the target output buffer to the line suffix output buffer which
464
- # will get flushed at the end of printing.
465
- def line_suffix
466
- previous_target, @target = @target, line_suffixes
467
- yield
468
- @target = previous_target
469
- end
470
-
471
- # Takes +indent+ arg, but does nothing with it.
472
- #
473
- # Yields to a block.
474
- def nest(_indent)
475
- yield
476
- end
477
-
478
- # Add +object+ to the text to be output.
479
- #
480
- # +width+ argument is here for compatibility. It is a noop argument.
481
- def text(object = "", _width = nil)
482
- target << object
483
- end
484
- end
485
-
486
341
  # When printing, you can optionally specify the value that should be used
487
342
  # whenever a group needs to be broken onto multiple lines. In this case the
488
343
  # default is \n.
@@ -493,6 +348,7 @@ class PrettierPrint
493
348
  # behavior (for instance to use tabs) by passing a different genspace
494
349
  # procedure.
495
350
  DEFAULT_GENSPACE = ->(n) { " " * n }
351
+ Ractor.make_shareable(DEFAULT_GENSPACE) if defined?(Ractor)
496
352
 
497
353
  # There are two modes in printing, break and flat. When we're in break mode,
498
354
  # any lines will use their newline, any if-breaks will use their break
@@ -504,6 +360,11 @@ class PrettierPrint
504
360
  # a forced line, or the maximum width.
505
361
  MODE_FLAT = 2
506
362
 
363
+ # The default indentation for printing is zero, assuming that the code starts
364
+ # at the top level. That can be changed if desired to start from a different
365
+ # indentation level.
366
+ DEFAULT_INDENTATION = 0
367
+
507
368
  # This is a convenience method which is same as follows:
508
369
  #
509
370
  # begin
@@ -517,29 +378,12 @@ class PrettierPrint
517
378
  output = "".dup,
518
379
  maxwidth = 80,
519
380
  newline = DEFAULT_NEWLINE,
520
- genspace = DEFAULT_GENSPACE
381
+ genspace = DEFAULT_GENSPACE,
382
+ indentation = DEFAULT_INDENTATION
521
383
  )
522
384
  q = new(output, maxwidth, newline, &genspace)
523
385
  yield q
524
- q.flush
525
- output
526
- end
527
-
528
- # This is similar to PrettierPrint::format but the result has no breaks.
529
- #
530
- # +maxwidth+, +newline+ and +genspace+ are ignored.
531
- #
532
- # The invocation of +breakable+ in the block doesn't break a line and is
533
- # treated as just an invocation of +text+.
534
- #
535
- def self.singleline_format(
536
- output = +"",
537
- _maxwidth = nil,
538
- _newline = nil,
539
- _genspace = nil
540
- )
541
- q = SingleLine.new(output)
542
- yield q
386
+ q.flush(indentation)
543
387
  output
544
388
  end
545
389
 
@@ -643,17 +487,20 @@ class PrettierPrint
643
487
 
644
488
  # Flushes all of the generated print tree onto the output buffer, then clears
645
489
  # the generated tree from memory.
646
- def flush
490
+ def flush(base_indentation = DEFAULT_INDENTATION)
647
491
  # First, get the root group, since we placed one at the top to begin with.
648
492
  doc = groups.first
649
493
 
650
494
  # This represents how far along the current line we are. It gets reset
651
495
  # back to 0 when we encounter a newline.
652
- position = 0
496
+ position = base_indentation
497
+
498
+ # Start the buffer with the base indentation level.
499
+ buffer << genspace.call(base_indentation) if base_indentation > 0
653
500
 
654
501
  # This is our command stack. A command consists of a triplet of an
655
502
  # indentation level, the mode (break or flat), and a doc node.
656
- commands = [[0, MODE_BREAK, doc]]
503
+ commands = [[base_indentation, MODE_BREAK, doc]]
657
504
 
658
505
  # This is a small optimization boolean. It keeps track of whether or not
659
506
  # when we hit a group node we should check if it fits on the same line.
@@ -1251,3 +1098,5 @@ class PrettierPrint
1251
1098
  end
1252
1099
  end
1253
1100
  end
1101
+
1102
+ require_relative "prettier_print/single_line"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prettier_print
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.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-10-19 00:00:00.000000000 Z
11
+ date: 2022-12-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -18,6 +18,7 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - ".github/dependabot.yml"
21
+ - ".github/workflows/auto-merge.yml"
21
22
  - ".github/workflows/main.yml"
22
23
  - ".gitignore"
23
24
  - CHANGELOG.md
@@ -28,6 +29,7 @@ files:
28
29
  - Rakefile
29
30
  - bin/console
30
31
  - lib/prettier_print.rb
32
+ - lib/prettier_print/single_line.rb
31
33
  - lib/prettier_print/version.rb
32
34
  - prettier_print.gemspec
33
35
  homepage: https://github.com/ruby-syntax-tree/prettier_print