@@ -122,10 +122,10 @@ def pretty_print(q)
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 @@ def pretty_print(q)
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 @@ def pretty_print(q)
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 @@ def self.for(output)
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 @@ def text(object = "", _width = nil)
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
@@ -525,24 +381,6 @@ def self.format(
525
381
output
526
382
end
527
383
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
543
- output
544
- end
545
-
546
384
# The output object. It represents the final destination of the contents of
547
385
# the print tree. It should respond to <<.
548
386
#
@@ -1251,3 +1089,5 @@ def remove_breaks_with(doc, replace)
1251
1089
end
1252
1090
end
1253
1091
end
1092
+
1093
+ require_relative "prettier_print/single_line"
0 commit comments