-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
BIConfigurableFormatter.class.st
1595 lines (1382 loc) · 47.4 KB
/
BIConfigurableFormatter.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"
BIConfigurableFormatter formats the Refactoring Browser's parse trees. It has many more formatting options than the default formatter used by the RB. To change the RB to use this formatter, execute ""RBProgramNode formatterClass: BIConfigurableFormatter"". For some refactorings the RB must reformat the code after the change, so it is good to have a formatter configured to your tastes.
Instance Variables:
codeStream <PositionableStream> the stream we are writing our output to
indent <Integer> how many times are we indenting a new line -- indents are normally tabs but could be any whitespace string
lineStart <Integer> the position of the character that started the current line. This is used for calculating the line length.
lookaheadCode <Dictionary key: RBProgramNode value: String> sometimes we need to lookahead while formatting, this dictionary contains the nodes that have already been formatted by lookahead
originalSource <String> the original source before we started formatting. This is used to extract the comments from the original source.
BIConfigurableFormatter new
"
Class {
#name : #BIConfigurableFormatter,
#superclass : #RBProgramNodeVisitor,
#instVars : [
'codeStream',
'indent',
'lookaheadCode',
'originalSource',
'lineStart',
'context',
'isInCascadeNode'
],
#classVars : [
'DefaultPrettyPrintContext',
'FormatAsYouReadPolicy'
],
#category : #'BlueInk-Core-Base'
}
{ #category : #private }
BIConfigurableFormatter class >> announceASettingChange [
SystemAnnouncer uniqueInstance announce: BISettingsChanged
]
{ #category : #public }
BIConfigurableFormatter class >> format: aParseTree [
^self format: aParseTree withIndents: 0
]
{ #category : #public }
BIConfigurableFormatter class >> format: aParseTree withIndents: anInteger [
^ self new
indent: anInteger;
format: aParseTree
]
{ #category : #accessing }
BIConfigurableFormatter class >> formatAsYouReadPolicy [
^ FormatAsYouReadPolicy
]
{ #category : #accessing }
BIConfigurableFormatter class >> formatAsYouReadPolicy: anObject [
FormatAsYouReadPolicy := anObject
]
{ #category : #accessing }
BIConfigurableFormatter class >> formatCommentWithStatements [
^ DefaultPrettyPrintContext formatCommentWithStatements
]
{ #category : #accessing }
BIConfigurableFormatter class >> formatCommentWithStatements: aBoolean [
DefaultPrettyPrintContext formatCommentWithStatements: aBoolean.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> indentString [
^ DefaultPrettyPrintContext indentString
]
{ #category : #accessing }
BIConfigurableFormatter class >> indentString: aString [
DefaultPrettyPrintContext indentString: aString.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> indentsForKeywords [
^ DefaultPrettyPrintContext indentsForKeywords
]
{ #category : #accessing }
BIConfigurableFormatter class >> indentsForKeywords: anInteger [
DefaultPrettyPrintContext indentsForKeywords: anInteger.
self announceASettingChange
]
{ #category : #initialization }
BIConfigurableFormatter class >> initialize [
"self initialize"
FormatAsYouReadPolicy := false.
DefaultPrettyPrintContext := BIPrettyPrinterContext new
]
{ #category : #accessing }
BIConfigurableFormatter class >> isPrettyPrinter [
^ true
]
{ #category : #accessing }
BIConfigurableFormatter class >> keepBlockInMessage [
^ DefaultPrettyPrintContext keepBlockInMessage
]
{ #category : #accessing }
BIConfigurableFormatter class >> keepBlockInMessage: aBoolean [
DefaultPrettyPrintContext keepBlockInMessage: aBoolean.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> lineUpBlockBrackets [
^ DefaultPrettyPrintContext lineUpBlockBrackets
]
{ #category : #accessing }
BIConfigurableFormatter class >> lineUpBlockBrackets: aBoolean [
DefaultPrettyPrintContext lineUpBlockBrackets: aBoolean.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> maxLineLength [
^ DefaultPrettyPrintContext maxLineLength
]
{ #category : #accessing }
BIConfigurableFormatter class >> maxLineLength: anInteger [
DefaultPrettyPrintContext maxLineLength: anInteger.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> methodSignatureOnMultipleLines [
^ DefaultPrettyPrintContext methodSignatureOnMultipleLines
]
{ #category : #accessing }
BIConfigurableFormatter class >> methodSignatureOnMultipleLines: aBoolean [
DefaultPrettyPrintContext methodSignatureOnMultipleLines: aBoolean.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> minimumNewLinesBetweenStatements [
^ DefaultPrettyPrintContext minimumNewLinesBetweenStatements
]
{ #category : #accessing }
BIConfigurableFormatter class >> minimumNewLinesBetweenStatements: anInteger [
DefaultPrettyPrintContext minimumNewLinesBetweenStatements: anInteger.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> multiLineMessages [
^ DefaultPrettyPrintContext multiLineMessages printString
]
{ #category : #accessing }
BIConfigurableFormatter class >> multiLineMessages: aString [
DefaultPrettyPrintContext multiLineMessages: (self compiler evaluate: aString).
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> newLineAfterCascade [
^ DefaultPrettyPrintContext newLineAfterCascade
]
{ #category : #accessing }
BIConfigurableFormatter class >> newLineAfterCascade: aBoolean [
DefaultPrettyPrintContext newLineAfterCascade: aBoolean.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> newLineBeforeFirstCascade [
^ DefaultPrettyPrintContext newLineBeforeFirstCascade
]
{ #category : #accessing }
BIConfigurableFormatter class >> newLineBeforeFirstCascade: aBoolean [
DefaultPrettyPrintContext newLineBeforeFirstCascade: aBoolean.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> newLineBeforeFirstKeyword [
^ DefaultPrettyPrintContext newLineBeforeFirstKeyword
]
{ #category : #accessing }
BIConfigurableFormatter class >> newLineBeforeFirstKeyword: aBoolean [
DefaultPrettyPrintContext newLineBeforeFirstKeyword: aBoolean.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> newLinesAfterMethodComment [
^ DefaultPrettyPrintContext newLinesAfterMethodComment
]
{ #category : #accessing }
BIConfigurableFormatter class >> newLinesAfterMethodComment: anInteger [
DefaultPrettyPrintContext newLinesAfterMethodComment: anInteger.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> newLinesAfterMethodPattern [
^ DefaultPrettyPrintContext newLinesAfterMethodPattern
]
{ #category : #accessing }
BIConfigurableFormatter class >> newLinesAfterMethodPattern: anInteger [
DefaultPrettyPrintContext newLinesAfterMethodPattern: anInteger.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> newLinesAfterTemporaries [
^ DefaultPrettyPrintContext newLinesAfterTemporaries
]
{ #category : #accessing }
BIConfigurableFormatter class >> newLinesAfterTemporaries: anInteger [
DefaultPrettyPrintContext newLinesAfterTemporaries: anInteger.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> numberOfArgumentsForMultiLine [
^ DefaultPrettyPrintContext numberOfArgumentsForMultiLine
]
{ #category : #accessing }
BIConfigurableFormatter class >> numberOfArgumentsForMultiLine: anInteger [
DefaultPrettyPrintContext numberOfArgumentsForMultiLine: anInteger.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> oneLineMessages [
^ DefaultPrettyPrintContext oneLineMessages printString
]
{ #category : #accessing }
BIConfigurableFormatter class >> oneLineMessages: aString [
DefaultPrettyPrintContext oneLineMessages: (self compiler evaluate: aString).
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> periodsAsTerminators [
^ DefaultPrettyPrintContext periodsAtEndOfBlock and: [ DefaultPrettyPrintContext periodsAtEndOfMethod ]
]
{ #category : #accessing }
BIConfigurableFormatter class >> periodsAsTerminators: aBoolean [
DefaultPrettyPrintContext periodsAtEndOfBlock: aBoolean.
DefaultPrettyPrintContext periodsAtEndOfMethod: aBoolean.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> periodsAtEndOfBlock [
^ DefaultPrettyPrintContext periodsAtEndOfBlock
]
{ #category : #accessing }
BIConfigurableFormatter class >> periodsAtEndOfBlock: aBoolean [
DefaultPrettyPrintContext periodsAtEndOfBlock: aBoolean.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> periodsAtEndOfMethod [
^ DefaultPrettyPrintContext periodsAtEndOfMethod
]
{ #category : #accessing }
BIConfigurableFormatter class >> periodsAtEndOfMethod: aBoolean [
DefaultPrettyPrintContext periodsAtEndOfMethod: aBoolean.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> retainBlankLinesBeforeComments [
^ DefaultPrettyPrintContext retainBlankLinesBeforeComments
]
{ #category : #accessing }
BIConfigurableFormatter class >> retainBlankLinesBeforeComments: aBoolean [
DefaultPrettyPrintContext retainBlankLinesBeforeComments: aBoolean.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> retainBlankLinesBetweenStatements [
^ DefaultPrettyPrintContext retainBlankLinesBetweenStatements
]
{ #category : #accessing }
BIConfigurableFormatter class >> retainBlankLinesBetweenStatements: aBoolean [
DefaultPrettyPrintContext retainBlankLinesBetweenStatements: aBoolean.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> selectorAndArgumentCombinedMaxSize [
^ DefaultPrettyPrintContext selectorAndArgumentCombinedMaxSize
]
{ #category : #accessing }
BIConfigurableFormatter class >> selectorAndArgumentCombinedMaxSize: anInteger [
DefaultPrettyPrintContext selectorAndArgumentCombinedMaxSize: anInteger.
self announceASettingChange
]
{ #category : #settings }
BIConfigurableFormatter class >> settingsOn: aBuilder [
<systemsettings>
(aBuilder group: #blueInkFormatter)
target: self;
parent: #codeFormatters;
label: 'BlueInk Pretty Printing';
description: 'Settings related to the formatter';
with:
[
(aBuilder setting: #formatCommentWithStatements)
default: true; label: 'Format comment with statements'.
(aBuilder setting: #indentString)
default: Character tab asString; label: 'Indent string'.
(aBuilder setting: #indentsForKeywords)
default: 1; label: 'Indents for keywords'.
(aBuilder setting: #keepBlockInMessage)
default: true;
label: 'Keep block in message';
description: (BIFormatSettingsExample >> #keepBlockInMessageExample) sourceCode.
(aBuilder setting: #lineUpBlockBrackets)
default: false;
label: 'Line up block brackets';
description: (BIFormatSettingsExample >> #lineUpBlockBracketsExample) sourceCode.
(aBuilder setting: #methodSignatureOnMultipleLines)
default: false;
label: 'Method signature on multiple lines'.
(aBuilder setting: #oneLineMessages)
default: '#(#to: #to:do: #to:by: #to:by:do:)';
label: 'One line messages'.
(aBuilder setting: #multiLineMessages)
default: '#(#ifTrue:ifFalse: #ifFalse:ifTrue: #ifTrue: #ifFalse: #on:do: #ensure: #ifCurtailed:)';
label: 'Multi line messages'.
(aBuilder setting: #minimumNewLinesBetweenStatements)
default: 1;
label: 'Minimum new lines between statements'.
(aBuilder setting: #newLineAfterCascade)
label: 'New line after cascade';
default: true;
description: (BIFormatSettingsExample >> #newLineAfterCascadeExample) sourceCode.
(aBuilder setting: #newLineBeforeFirstCascade)
label: 'New line before first cascade';
default: true;
description: (BIFormatSettingsExample >> #newLineBeforeFirstCascadeExample) sourceCode.
(aBuilder setting: #newLineBeforeFirstKeyword)
label: 'New line before first keyword';
default: false;
description: (BIFormatSettingsExample >> #newLineBeforeFirstKeywordExample) sourceCode.
(aBuilder setting: #newLinesAfterMethodComment) default: 2; label: 'New lines after method comment'.
(aBuilder setting: #newLinesAfterMethodPattern) default: 1; label: 'New lines after method pattern'.
(aBuilder setting: #newLinesAfterTemporaries) default: 1; label: 'New lines after temporaries'.
(aBuilder setting: #numberOfArgumentsForMultiLine)
default: 4;
label: 'Number of arguments for multi line'.
(aBuilder setting: #periodsAsTerminators) default: false; label: 'Periods as terminators'.
(aBuilder setting: #periodsAtEndOfBlock) default: false; label: 'Periods at end of block'.
(aBuilder setting: #periodsAtEndOfMethod) default: false; label: 'Periods at end of method'.
(aBuilder setting: #retainBlankLinesBeforeComments)
default: true;
label: 'Retain blank lines before comments'.
(aBuilder setting: #retainBlankLinesBetweenStatements)
default: false;
label: 'Retain blank lines between statements'.
(aBuilder setting: #selectorAndArgumentCombinedMaxSize)
default: 40;
label: 'Selector and argument combined in method signature max size'.
(aBuilder setting: #stringFollowingReturn) default: ' '; label: 'String following return'.
(aBuilder setting: #stringInsideBlocks) default: ' '; label: 'String inside blocks'.
(aBuilder setting: #stringInsideParentheses) default: ''; label: 'String inside parentheses'.
(aBuilder setting: #traditionalBinaryPrecedenceArray)
default: '#(#($| $& $?) #($= $~ $< $>) #($- $+) #($* $/ $% $\) #($@))';
label: 'Traditional binary precedence'.
(aBuilder setting: #useBasicCommentFormat)
default: true;
label: 'Use basic comment format'.
(aBuilder setting: #useTraditionalBinaryPrecedenceForParentheses)
default: true;
label: 'Use traditional binary precedence for parentheses' ]
]
{ #category : #accessing }
BIConfigurableFormatter class >> stringFollowingReturn [
^ DefaultPrettyPrintContext stringFollowingReturn
]
{ #category : #accessing }
BIConfigurableFormatter class >> stringFollowingReturn: aString [
DefaultPrettyPrintContext stringFollowingReturn: aString.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> stringInsideBlocks [
^ DefaultPrettyPrintContext stringInsideBlocks
]
{ #category : #accessing }
BIConfigurableFormatter class >> stringInsideBlocks: aString [
DefaultPrettyPrintContext stringInsideBlocks: aString.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> stringInsideParentheses [
^ DefaultPrettyPrintContext stringInsideParentheses
]
{ #category : #accessing }
BIConfigurableFormatter class >> stringInsideParentheses: aString [
DefaultPrettyPrintContext stringInsideParentheses: aString.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> traditionalBinaryPrecedenceArray [
^ DefaultPrettyPrintContext traditionalBinaryPrecedenceArray printString
]
{ #category : #accessing }
BIConfigurableFormatter class >> traditionalBinaryPrecedenceArray: aString [
DefaultPrettyPrintContext traditionalBinaryPrecedenceArray: (self compiler evaluate: aString).
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> useBasicCommentFormat [
^ DefaultPrettyPrintContext useBasicCommentFormat
]
{ #category : #accessing }
BIConfigurableFormatter class >> useBasicCommentFormat: aBoolean [
DefaultPrettyPrintContext useBasicCommentFormat: aBoolean.
self announceASettingChange
]
{ #category : #accessing }
BIConfigurableFormatter class >> useTraditionalBinaryPrecedenceForParentheses [
^ DefaultPrettyPrintContext useTraditionalBinaryPrecedenceForParentheses
]
{ #category : #accessing }
BIConfigurableFormatter class >> useTraditionalBinaryPrecedenceForParentheses: aBoolean [
DefaultPrettyPrintContext useTraditionalBinaryPrecedenceForParentheses: aBoolean.
self announceASettingChange
]
{ #category : #private }
BIConfigurableFormatter >> addNewLinesBeforeStatementStartingAt: anInteger [
| newLines |
newLines := self minimumNewLinesBetweenStatements
max: (self retainBlankLinesBetweenStatements
ifTrue: [self newLinesBeforeStartingAt: anInteger]
ifFalse: [0]).
newLines = 0 ifTrue: [self space] ifFalse: [self newLines: newLines]
]
{ #category : #private }
BIConfigurableFormatter >> addSpaceIfNeededForLastArgument: aPragmaNode [
aPragmaNode isUnary
ifTrue: [ ^ self ].
(self pragmaArgumentNeedsSeparator: aPragmaNode arguments last)
ifTrue: [ self space ]
]
{ #category : #private }
BIConfigurableFormatter >> basicFormatCommentFor: aComment [
codeStream
nextPut: $";
nextPutAll: aComment contents;
nextPut: $"
]
{ #category : #private }
BIConfigurableFormatter >> bracketWith: bracketString around: aBlock [
bracketString isEmpty
ifTrue: [ ^ aBlock value ].
codeStream nextPut: bracketString first.
^ aBlock
ensure: [ codeStream nextPut: bracketString last ]
]
{ #category : #private }
BIConfigurableFormatter >> characterSeparatorMethodSignatureFor: aMethodNode [
^ (self needsMethodSignatureOnMultipleLinesFor: aMethodNode)
ifTrue: [ self newLine ]
ifFalse: [ self space ]
]
{ #category : #accessing }
BIConfigurableFormatter >> codeStream [
^ codeStream
]
{ #category : #accessing }
BIConfigurableFormatter >> codeStream: anObject [
codeStream := anObject
]
{ #category : #private }
BIConfigurableFormatter >> currentLineLength [
^ codeStream position - lineStart
]
{ #category : #'public interface' }
BIConfigurableFormatter >> format: aParseTree [
originalSource := aParseTree source.
self initializeCodeStream.
self visitNode: aParseTree.
^ codeStream contents
]
{ #category : #'private-formatting' }
BIConfigurableFormatter >> formatArray: anArrayNode [
| current |
(anArrayNode statements reject: [ :each | each className = 'RBLiteralValueNode' ])
ifEmpty: [
anArrayNode statements
do: [ :each |
current := each.
self visitNode: each ]
separatedBy: [
(self isLineTooLong: current value asString)
ifTrue: [
codeStream nextPut: $..
self newLine ]
ifFalse: [ codeStream nextPutAll: ' . ' ] ] ]
ifNotEmpty: [ self formatSequenceNodeStatementsFor: anArrayNode ]
]
{ #category : #'private-formatting' }
BIConfigurableFormatter >> formatBlock: aBlockNode [
codeStream nextPutAll: self stringInsideBlocks.
self formatBlockArgumentsFor: aBlockNode.
self formatBlockCommentFor: aBlockNode.
self visitNode: aBlockNode body.
(self lineUpBlockBrackets and: [ self willBeMultiline: aBlockNode body ])
ifTrue: [ self newLine ]
ifFalse: [ codeStream nextPutAll: self stringInsideBlocks ]
]
{ #category : #'private-formatting' }
BIConfigurableFormatter >> formatBlockArgumentsFor: aBlockNode [
aBlockNode arguments isEmpty
ifTrue: [ ^ self ].
aBlockNode arguments
do:
[ :each | codeStream nextPut: $:.
self visitNode: each.
self formatCommentWithStatements
ifTrue: [ self formatCommentsFor: each ].
self space ].
codeStream nextPutAll: '| '.
(self willBeMultiline: aBlockNode body)
ifTrue: [ self newLine ]
]
{ #category : #'private-formatting' }
BIConfigurableFormatter >> formatBlockCommentFor: aBlockNode [
aBlockNode comments
do: [ :each |
self basicFormatCommentFor: each.
( self isNonEmptySingleLineBlock: aBlockNode )
ifTrue: [ self space ].
( self willBeMultiline: aBlockNode body )
ifTrue: [ self newLine ]
]
]
{ #category : #accessing }
BIConfigurableFormatter >> formatCommentWithStatements [
^ context formatCommentWithStatements
]
{ #category : #accessing }
BIConfigurableFormatter >> formatCommentWithStatements: aBoolean [
context formatCommentWithStatements: aBoolean
]
{ #category : #'private-formatting' }
BIConfigurableFormatter >> formatCommentsFor: aNode [
aNode comments
do: [ :each |
self basicFormatCommentFor: each.
self newLine
]
]
{ #category : #private }
BIConfigurableFormatter >> formatMessageNodeInAMessageStreamFor: aMessageNode [
| messageStream |
messageStream := WriteStream on: (String new: 100).
self
with: aMessageNode selectorParts
and: aMessageNode arguments
do: [ :selector :arg |
messageStream
nextPutAll: selector;
space;
nextPutAll: (self formattedSourceFor: arg) ]
separatedBy: [ messageStream space ].
^ messageStream
]
{ #category : #'private-formatting' }
BIConfigurableFormatter >> formatMethodBodyFor: aMethodNode [
self
indentAround:
[
self newLines: self newLinesAfterMethodPattern.
self formatMethodCommentFor: aMethodNode.
self formatPragmasFor: aMethodNode.
self visitNode: aMethodNode body ]
]
{ #category : #'private-formatting' }
BIConfigurableFormatter >> formatMethodCommentFor: aMethodNode [
aMethodNode comments
do: [ :each |
self useBasicCommentFormat
ifTrue: [ self basicFormatCommentFor: each ]
ifFalse: [ self resizeCommentFor: each startingAt: 0 ].
self newLines: self newLinesAfterMethodComment
]
]
{ #category : #'private-formatting' }
BIConfigurableFormatter >> formatMethodPatternFor: aMethodNode [
aMethodNode arguments isEmpty
ifTrue: [ codeStream nextPutAll: aMethodNode selector ]
ifFalse: [
(self needsMethodSignatureOnMultipleLinesFor: aMethodNode)
ifTrue: [ self privateFormatMethodPatternMultiLineFor: aMethodNode ]
ifFalse: [ self privateFormatMethodPatternMonoLineFor: aMethodNode ] ]
]
{ #category : #'private-formatting' }
BIConfigurableFormatter >> formatPragmasFor: aMethodNode [
aMethodNode pragmas do: [ :each | self visitNode: each; newLine ]
]
{ #category : #'private-formatting' }
BIConfigurableFormatter >> formatSelectorAndArguments: aMessageNode [
| newLineBetweenArguments |
newLineBetweenArguments := self isMultiLineMessage: aMessageNode.
self
indent:
(newLineBetweenArguments
ifTrue: [ self indentsForKeywords ]
ifFalse: [ 0 ])
around: [
self
formatSelectorAndArguments: aMessageNode
firstSeparator:
((newLineBetweenArguments or: [ self newLineBeforeFirstKeyword ])
ifTrue: [ [ self newLine ] ]
ifFalse: [ [ self space ] ])
restSeparator:
(newLineBetweenArguments
ifTrue: [ [ self newLine ] ]
ifFalse: [ [ self space ] ]) ]
]
{ #category : #'private-formatting' }
BIConfigurableFormatter >> formatSelectorAndArguments: aMessageNode firstSeparator: firstBlock restSeparator: restBlock [
| separatorBlock |
separatorBlock := firstBlock.
aMessageNode isUnary
ifTrue: [
self handleLineForSelector: aMessageNode selector withSeparatorBlock:
separatorBlock.
codeStream nextPutAll: aMessageNode selector ]
ifFalse: [
aMessageNode selectorParts
with: aMessageNode arguments
do: [ :selector :argument |
self handleLineForSelector: selector withSeparatorBlock: separatorBlock.
separatorBlock := restBlock.
self
indentAround: [
codeStream nextPutAll: selector.
self handleLineForArgument: argument ] ] ]
]
{ #category : #'private-formatting' }
BIConfigurableFormatter >> formatSequenceNodeStatementsFor: aSequenceNode [
| statements |
statements := aSequenceNode statements.
statements isEmpty
ifTrue: [ ^ self ].
1 to: statements size do: [ :i |
self visitNode: (statements at: i).
(i < statements size
or: [
aSequenceNode parent
ifNil: [ self periodsAsTerminators ]
ifNotNil: [ :parent |
parent isBlock
ifTrue: [ self periodsAtEndOfBlock ]
ifFalse: [ self periodsAtEndOfMethod ] ] ])
ifTrue: [ codeStream nextPut: $. ].
self formatStatementCommentsFor: (statements at: i).
i < statements size
ifTrue: [ self
addNewLinesBeforeStatementStartingAt: (statements at: i + 1) start ] ]
]
{ #category : #'private-formatting' }
BIConfigurableFormatter >> formatStatementCommentsFor: aStatementNode [
self formatCommentWithStatements
ifFalse: [ ^ self ].
aStatementNode statementComments
do: [ :each |
| count |
count := self newLinesBeforeStartingAt: each start.
self retainBlankLinesBeforeComments & ( count > 0 )
ifTrue: [ self newLines: count ]
ifFalse: [ codeStream tab ].
self useBasicCommentFormat
ifTrue: [ self basicFormatCommentFor: each ]
ifFalse: [ self resizeCommentFor: each startingAt: self currentLineLength ]
]
]
{ #category : #private }
BIConfigurableFormatter >> formatTemporariesFor: aSequenceNode [
aSequenceNode temporaries isEmpty
ifTrue: [ ^ self ].
self
bracketWith: '|'
around:
[
self space.
aSequenceNode temporaries
do:
[ :each |
self visitNode: each.
self formatCommentWithStatements
ifTrue: [ self formatStatementCommentsFor: each ].
self space ] ].
self newLines: self newLinesAfterTemporaries
]
{ #category : #private }
BIConfigurableFormatter >> formattedSourceFor: aNode [
^ lookaheadCode
at: aNode
ifAbsentPut: [
self class new
indent: self indent;
installNewContext: context;
format: aNode ]
]
{ #category : #private }
BIConfigurableFormatter >> handleLineForArgument: anArgument [
(self keepBlockInMessage and: [ anArgument isBlock ])
ifTrue: [ (self willBeMultiline: anArgument)
ifTrue: [ (self formattedSourceFor: anArgument) lines first size > self maxLineLength
ifTrue: [ self newLine ]
ifFalse: [ self space ] ]
ifFalse: [ anArgument sourceInterval size > self maxLineLength
ifTrue: [ self newLine ]
ifFalse: [ self space ] ] ]
ifFalse: [ ((self willBeMultiline: anArgument)
or: [ anArgument sourceInterval size > self maxLineLength ])
ifTrue: [ self isInCascadeNode
ifTrue: [ codeStream nextPut: Character cr.
self indent - 1 timesRepeat: [ codeStream nextPutAll: self indentString ] ]
ifFalse: [ self newLine ] ]
ifFalse: [ self space ] ].
self visitNode: anArgument
]
{ #category : #private }
BIConfigurableFormatter >> handleLineForSelector: selector withSeparatorBlock: aBlock [
(self isLineTooLong: selector)
ifTrue: [ self newLine ]
ifFalse: [ aBlock value ]
]
{ #category : #private }
BIConfigurableFormatter >> hasAMultiLineMessageArgument: anArgumentsCollection [
^ anArgumentsCollection
anySatisfy: [ :each |
self isInCascadeNode
ifTrue: [ self indent: 0 around: [ self willBeMultiline: each ] ]
ifFalse: [
self
indent: self indentsForKeywords + 1
around: [ self willBeMultiline: each ] ] ]
]
{ #category : #accessing }
BIConfigurableFormatter >> indent [
^ indent
]
{ #category : #accessing }
BIConfigurableFormatter >> indent: anInteger [
indent := anInteger
]
{ #category : #private }
BIConfigurableFormatter >> indent: anInteger around: aBlock [
self indent: self indent + anInteger.
^aBlock ensure: [self indent: self indent - anInteger]
]
{ #category : #private }
BIConfigurableFormatter >> indentAround: aBlock [
self indent: 1 around: aBlock
]
{ #category : #accessing }
BIConfigurableFormatter >> indentString [
^ context indentString
]
{ #category : #accessing }
BIConfigurableFormatter >> indentString: aString [
context indentString: aString
]
{ #category : #accessing }
BIConfigurableFormatter >> indentsForKeywords [
^ context indentsForKeywords
]
{ #category : #accessing }
BIConfigurableFormatter >> indentsForKeywords: anInteger [
context indentsForKeywords: anInteger
]
{ #category : #initialization }
BIConfigurableFormatter >> initialize [
super initialize.
lineStart := 0.
self indent: 0.
self isInCascadeNode: false.
lookaheadCode := IdentityDictionary new.
self initializeCodeStream.
context := DefaultPrettyPrintContext
]
{ #category : #initialization }
BIConfigurableFormatter >> initializeCodeStream [
codeStream := WriteStream on: (String new: 256)
]
{ #category : #'public interface' }
BIConfigurableFormatter >> installNewContext: aContext [
context := aContext
]
{ #category : #'public interface' }
BIConfigurableFormatter >> installNewValueInContext: aCol [
"self new installNewValueInContext: { (#cascadedMessageInsideParentheses: -> true) }"
context := context copy.
"needed because by default context point to the DefaultPrettyPrintContext"
aCol do: [ :each | self perform: each key with: each value]
]
{ #category : #accessing }
BIConfigurableFormatter >> isInCascadeNode [
^ isInCascadeNode
]
{ #category : #accessing }
BIConfigurableFormatter >> isInCascadeNode: aBoolean [
isInCascadeNode := aBoolean
]
{ #category : #private }
BIConfigurableFormatter >> isLineTooLong: aString [
^ self currentLineLength + (aString indexOf: Character cr ifAbsent: [ aString size ])
>= self maxLineLength
]
{ #category : #private }
BIConfigurableFormatter >> isMultiLineMessage: aMessageNode [
(self multiLineMessages includes: aMessageNode selector)
ifTrue: [ ^ true ].
(self oneLineMessages includes: aMessageNode selector)
ifTrue: [ ^ false ].
self numberOfArgumentsForMultiLine <= aMessageNode arguments size
ifTrue: [ ^ true ].
(self hasAMultiLineMessageArgument: aMessageNode arguments)
ifTrue: [ ^ true ].
aMessageNode isUnary
ifTrue: [ ^ self isLineTooLong: aMessageNode selector ].
^ self isLineTooLong: (self formatMessageNodeInAMessageStreamFor: aMessageNode) contents
]
{ #category : #private }
BIConfigurableFormatter >> isNonEmptySingleLineBlock: aBlockNode [
^ ((self willBeMultiline: aBlockNode body) or: [ aBlockNode body statements isEmpty ]) not
]
{ #category : #accessing }
BIConfigurableFormatter >> keepBlockInMessage [
^ context keepBlockInMessage
]
{ #category : #accessing }
BIConfigurableFormatter >> keepBlockInMessage: aBoolean [
context keepBlockInMessage: aBoolean
]
{ #category : #accessing }
BIConfigurableFormatter >> lineStart [
^ lineStart
]
{ #category : #accessing }
BIConfigurableFormatter >> lineStart: anObject [
lineStart := anObject
]
{ #category : #accessing }
BIConfigurableFormatter >> lineUpBlockBrackets [
^ context lineUpBlockBrackets
]
{ #category : #accessing }
BIConfigurableFormatter >> lineUpBlockBrackets: aBoolean [
context lineUpBlockBrackets: aBoolean
]
{ #category : #accessing }
BIConfigurableFormatter >> maxLineLength [
^ context maxLineLength
]
{ #category : #accessing }
BIConfigurableFormatter >> maxLineLength: anInteger [
context maxLineLength: anInteger
]
{ #category : #accessing }
BIConfigurableFormatter >> methodSignatureOnMultipleLines [
^ context methodSignatureOnMultipleLines
]
{ #category : #accessing }
BIConfigurableFormatter >> methodSignatureOnMultipleLines: aBoolean [
context methodSignatureOnMultipleLines: aBoolean
]
{ #category : #accessing }
BIConfigurableFormatter >> minimumNewLinesBetweenStatements [
^ context minimumNewLinesBetweenStatements
]
{ #category : #accessing }
BIConfigurableFormatter >> minimumNewLinesBetweenStatements: anInteger [
context minimumNewLinesBetweenStatements: anInteger
]
{ #category : #accessing }
BIConfigurableFormatter >> multiLineMessages [
^ context multiLineMessages
]
{ #category : #accessing }
BIConfigurableFormatter >> multiLineMessages: anArray [
context multiLineMessages: anArray
]