36
36
import com .igormaznitsa .jcp .utils .ResetablePrinter ;
37
37
import java .io .File ;
38
38
import java .io .IOException ;
39
+ import java .util .ArrayList ;
39
40
import java .util .Collection ;
40
41
import java .util .HashSet ;
41
42
import java .util .List ;
45
46
import java .util .regex .Matcher ;
46
47
import java .util .regex .Pattern ;
47
48
import java .util .stream .Collectors ;
49
+ import java .util .stream .IntStream ;
48
50
import lombok .Data ;
49
51
50
52
/**
@@ -434,10 +436,12 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
434
436
return this .preprocessFileWithNotification (state , context , true );
435
437
}
436
438
439
+ private static final String EOL_MARKER = "-=$$$$$$$$__EOL__$$$$$$$$=-" ;
440
+
437
441
private void flushTextBufferForRemovedComments (
438
442
final AtomicReference <Map .Entry <String , String >> firstDetectedUncommentLinePtr ,
439
443
final int stringIndex ,
440
- final StringBuilder textBuffer ,
444
+ final List < String > textPieces ,
441
445
final ResetablePrinter resetablePrinter ,
442
446
final PreprocessingState state ,
443
447
final PreprocessorContext context )
@@ -446,11 +450,22 @@ private void flushTextBufferForRemovedComments(
446
450
final Map .Entry <String , String > firstUncommentLine =
447
451
firstDetectedUncommentLinePtr .getAndSet (null );
448
452
449
- if (textBuffer .length () > 0 ) {
453
+ final boolean lastEol = !textPieces .isEmpty () && textPieces .get (textPieces .size () - 1 ) ==
454
+ EOL_MARKER ;
455
+ final String accumulated = (lastEol ? IntStream .range (0 , textPieces .size () - 1 ) :
456
+ IntStream .range (0 , textPieces .size ()))
457
+ .mapToObj (textPieces ::get )
458
+ .map (x -> (x == EOL_MARKER ? context .getEol () : x ))
459
+ .collect (Collectors .joining ());
460
+ textPieces .clear ();
461
+
462
+ if (accumulated .isEmpty ()) {
463
+ if (lastEol ) {
464
+ resetablePrinter .print (context .getEol ());
465
+ }
466
+ } else {
450
467
final List <CommentTextProcessor > processors = context .getCommentTextProcessors ();
451
- final String textToProcess = textBuffer .toString ();
452
- textBuffer .setLength (0 );
453
- String text = textToProcess ;
468
+ String text = accumulated ;
454
469
455
470
if (!processors .isEmpty ()) {
456
471
final FilePositionInfo filePositionInfo =
@@ -459,12 +474,12 @@ private void flushTextBufferForRemovedComments(
459
474
460
475
final List <String > results = processors
461
476
.stream ()
462
- .filter (x -> x .isEnabled (this , filePositionInfo , context , state ))
477
+ .filter (x -> x .isAllowed (this , filePositionInfo , context , state ))
463
478
.map (x -> {
464
479
try {
465
480
return x .processUncommentedText (
466
481
indent ,
467
- textToProcess ,
482
+ accumulated ,
468
483
this ,
469
484
filePositionInfo ,
470
485
context ,
@@ -474,18 +489,18 @@ private void flushTextBufferForRemovedComments(
474
489
throw new PreprocessorException (
475
490
"Error during external comment text processor call: " +
476
491
x .getClass ().getCanonicalName (),
477
- textToProcess , state .makeIncludeStack (), ex );
492
+ accumulated , state .makeIncludeStack (), ex );
478
493
}
479
494
}).collect (Collectors .toList ());
480
495
481
496
if (results .isEmpty ()) {
482
497
context .logDebug ("No any result from processors for text block at " + filePositionInfo );
483
- text = textToProcess ;
498
+ text = accumulated ;
484
499
} else {
485
- text = String . join ( "" , results );
500
+ text = results . stream (). collect ( Collectors . joining ( context . getEol ()) );
486
501
}
487
502
}
488
- resetablePrinter .print (text );
503
+ resetablePrinter .print (text + ( lastEol ? context . getEol () : "" ) );
489
504
}
490
505
}
491
506
@@ -526,7 +541,7 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
526
541
String leftTrimmedString = null ;
527
542
528
543
TextFileDataContainer lastTextFileDataContainer = null ;
529
- final StringBuilder textBlockBuffer = new StringBuilder ();
544
+ final List < String > textPieces = new ArrayList <> ();
530
545
531
546
Integer firstBlockLineIndex = null ;
532
547
try {
@@ -560,7 +575,8 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
560
575
this .flushTextBufferForRemovedComments (
561
576
firstUncommentLine ,
562
577
requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
563
- textBlockBuffer , thePrinter ,
578
+ textPieces ,
579
+ thePrinter ,
564
580
theState ,
565
581
context );
566
582
firstBlockLineIndex = null ;
@@ -588,12 +604,12 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
588
604
}
589
605
590
606
String stringToBeProcessed = leftTrimmedString ;
591
- final boolean doPrintLn = presentedNextLine || !context .isCareForLastEol ();
607
+ final boolean doPrintEol = presentedNextLine || !context .isCareForLastEol ();
592
608
593
609
if (isHashPrefixed (stringToBeProcessed , context )) {
594
610
this .flushTextBufferForRemovedComments (firstUncommentLine ,
595
611
requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
596
- textBlockBuffer ,
612
+ textPieces ,
597
613
thePrinter ,
598
614
theState ,
599
615
context );
@@ -607,7 +623,7 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
607
623
final String text = stringPrefix +
608
624
AbstractDirectiveHandler .PREFIX_FOR_KEEPING_LINES_PROCESSED_DIRECTIVES +
609
625
extractedDirective ;
610
- if (doPrintLn ) {
626
+ if (doPrintEol ) {
611
627
thePrinter .println (text , context .getEol ());
612
628
} else {
613
629
thePrinter .print (text );
@@ -619,7 +635,7 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
619
635
final String text = stringPrefix +
620
636
AbstractDirectiveHandler .PREFIX_FOR_KEEPING_LINES_PROCESSED_DIRECTIVES +
621
637
extractedDirective ;
622
- if (doPrintLn ) {
638
+ if (doPrintEol ) {
623
639
thePrinter .println (text , context .getEol ());
624
640
} else {
625
641
thePrinter .print (text );
@@ -658,26 +674,27 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
658
674
firstBlockLineIndex = findLastReadLineIndex (theState );
659
675
firstUncommentLine .set (indentText );
660
676
}
661
- textBlockBuffer . append (indentText .getValue ());
662
- if (doPrintLn ) {
663
- textBlockBuffer . append ( context . getEol () );
677
+ textPieces . add (indentText .getValue ());
678
+ if (doPrintEol ) {
679
+ textPieces . add ( EOL_MARKER );
664
680
}
665
681
} else {
666
682
this .flushTextBufferForRemovedComments (firstUncommentLine ,
667
683
requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
668
- textBlockBuffer ,
684
+ textPieces ,
669
685
thePrinter ,
670
686
theState , context );
671
687
firstBlockLineIndex = null ;
672
688
673
- textBlockBuffer .append (stringPrefix ).append (indentText .getKey ())
674
- .append (indentText .getValue ());
675
- if (doPrintLn ) {
676
- textBlockBuffer .append (context .getEol ());
689
+ textPieces .add (stringPrefix );
690
+ textPieces .add (indentText .getKey ());
691
+ textPieces .add (indentText .getValue ());
692
+ if (doPrintEol ) {
693
+ textPieces .add (EOL_MARKER );
677
694
}
678
695
this .flushTextBufferForRemovedComments (firstUncommentLine ,
679
696
requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
680
- textBlockBuffer ,
697
+ textPieces ,
681
698
thePrinter ,
682
699
theState , context );
683
700
firstBlockLineIndex = null ;
@@ -701,27 +718,29 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
701
718
firstUncommentLine .set (indentText );
702
719
}
703
720
704
- textBlockBuffer . append (indentText .getValue ());
705
- if (doPrintLn ) {
706
- textBlockBuffer . append ( context . getEol () );
721
+ textPieces . add (indentText .getValue ());
722
+ if (doPrintEol ) {
723
+ textPieces . add ( EOL_MARKER );
707
724
}
708
725
} else {
709
726
this .flushTextBufferForRemovedComments (firstUncommentLine ,
710
727
requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
711
- textBlockBuffer ,
728
+ textPieces ,
712
729
thePrinter ,
713
730
theState , context );
714
731
firstBlockLineIndex = null ;
715
732
716
- textBlockBuffer .append (stringPrefix ).append (indentText .getKey ())
717
- .append (indentText .getValue ());
718
- if (doPrintLn ) {
719
- textBlockBuffer .append (context .getEol ());
733
+ textPieces .add (stringPrefix );
734
+ textPieces .add (indentText .getKey ());
735
+ textPieces .add (indentText .getValue ());
736
+
737
+ if (doPrintEol ) {
738
+ textPieces .add (EOL_MARKER );
720
739
}
721
740
this .flushTextBufferForRemovedComments (
722
741
firstUncommentLine ,
723
742
requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
724
- textBlockBuffer ,
743
+ textPieces ,
725
744
thePrinter ,
726
745
theState , context );
727
746
firstBlockLineIndex = null ;
@@ -730,7 +749,7 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
730
749
// Just string
731
750
this .flushTextBufferForRemovedComments (firstUncommentLine ,
732
751
requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
733
- textBlockBuffer ,
752
+ textPieces ,
734
753
thePrinter ,
735
754
theState , context );
736
755
firstBlockLineIndex = null ;
@@ -745,24 +764,23 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
745
764
}
746
765
747
766
thePrinter .print (stringPrefix );
748
- if (doPrintLn ) {
767
+ if (doPrintEol ) {
749
768
thePrinter .println (strToOut , context .getEol ());
750
769
} else {
751
770
thePrinter .print (strToOut );
752
771
}
753
772
}
754
773
} else if (context .isKeepLines ()) {
755
-
756
774
flushTextBufferForRemovedComments (firstUncommentLine ,
757
775
requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
758
- textBlockBuffer ,
776
+ textPieces ,
759
777
thePrinter ,
760
778
theState ,
761
779
context );
762
780
firstBlockLineIndex = null ;
763
781
764
782
final String text = AbstractDirectiveHandler .PREFIX_FOR_KEEPING_LINES + rawString ;
765
- if (doPrintLn ) {
783
+ if (doPrintEol ) {
766
784
thePrinter .println (text , context .getEol ());
767
785
} else {
768
786
thePrinter .print (text );
0 commit comments