Skip to content

Commit 98a4f10

Browse files
committed
refactoring
1 parent 38fc289 commit 98a4f10

File tree

4 files changed

+97
-80
lines changed

4 files changed

+97
-80
lines changed

jcp/src/main/java/com/igormaznitsa/jcp/containers/FileInfoContainer.java

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.igormaznitsa.jcp.utils.ResetablePrinter;
3737
import java.io.File;
3838
import java.io.IOException;
39+
import java.util.ArrayList;
3940
import java.util.Collection;
4041
import java.util.HashSet;
4142
import java.util.List;
@@ -45,6 +46,7 @@
4546
import java.util.regex.Matcher;
4647
import java.util.regex.Pattern;
4748
import java.util.stream.Collectors;
49+
import java.util.stream.IntStream;
4850
import lombok.Data;
4951

5052
/**
@@ -434,10 +436,12 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
434436
return this.preprocessFileWithNotification(state, context, true);
435437
}
436438

439+
private static final String EOL_MARKER = "-=$$$$$$$$__EOL__$$$$$$$$=-";
440+
437441
private void flushTextBufferForRemovedComments(
438442
final AtomicReference<Map.Entry<String, String>> firstDetectedUncommentLinePtr,
439443
final int stringIndex,
440-
final StringBuilder textBuffer,
444+
final List<String> textPieces,
441445
final ResetablePrinter resetablePrinter,
442446
final PreprocessingState state,
443447
final PreprocessorContext context)
@@ -446,11 +450,22 @@ private void flushTextBufferForRemovedComments(
446450
final Map.Entry<String, String> firstUncommentLine =
447451
firstDetectedUncommentLinePtr.getAndSet(null);
448452

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 {
450467
final List<CommentTextProcessor> processors = context.getCommentTextProcessors();
451-
final String textToProcess = textBuffer.toString();
452-
textBuffer.setLength(0);
453-
String text = textToProcess;
468+
String text = accumulated;
454469

455470
if (!processors.isEmpty()) {
456471
final FilePositionInfo filePositionInfo =
@@ -459,12 +474,12 @@ private void flushTextBufferForRemovedComments(
459474

460475
final List<String> results = processors
461476
.stream()
462-
.filter(x -> x.isEnabled(this, filePositionInfo, context, state))
477+
.filter(x -> x.isAllowed(this, filePositionInfo, context, state))
463478
.map(x -> {
464479
try {
465480
return x.processUncommentedText(
466481
indent,
467-
textToProcess,
482+
accumulated,
468483
this,
469484
filePositionInfo,
470485
context,
@@ -474,18 +489,18 @@ private void flushTextBufferForRemovedComments(
474489
throw new PreprocessorException(
475490
"Error during external comment text processor call: " +
476491
x.getClass().getCanonicalName(),
477-
textToProcess, state.makeIncludeStack(), ex);
492+
accumulated, state.makeIncludeStack(), ex);
478493
}
479494
}).collect(Collectors.toList());
480495

481496
if (results.isEmpty()) {
482497
context.logDebug("No any result from processors for text block at " + filePositionInfo);
483-
text = textToProcess;
498+
text = accumulated;
484499
} else {
485-
text = String.join("", results);
500+
text = results.stream().collect(Collectors.joining(context.getEol()));
486501
}
487502
}
488-
resetablePrinter.print(text);
503+
resetablePrinter.print(text + (lastEol ? context.getEol() : ""));
489504
}
490505
}
491506

@@ -526,7 +541,7 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
526541
String leftTrimmedString = null;
527542

528543
TextFileDataContainer lastTextFileDataContainer = null;
529-
final StringBuilder textBlockBuffer = new StringBuilder();
544+
final List<String> textPieces = new ArrayList<>();
530545

531546
Integer firstBlockLineIndex = null;
532547
try {
@@ -560,7 +575,8 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
560575
this.flushTextBufferForRemovedComments(
561576
firstUncommentLine,
562577
requireNonNullElse(firstBlockLineIndex, findLastReadLineIndex(theState)),
563-
textBlockBuffer, thePrinter,
578+
textPieces,
579+
thePrinter,
564580
theState,
565581
context);
566582
firstBlockLineIndex = null;
@@ -588,12 +604,12 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
588604
}
589605

590606
String stringToBeProcessed = leftTrimmedString;
591-
final boolean doPrintLn = presentedNextLine || !context.isCareForLastEol();
607+
final boolean doPrintEol = presentedNextLine || !context.isCareForLastEol();
592608

593609
if (isHashPrefixed(stringToBeProcessed, context)) {
594610
this.flushTextBufferForRemovedComments(firstUncommentLine,
595611
requireNonNullElse(firstBlockLineIndex, findLastReadLineIndex(theState)),
596-
textBlockBuffer,
612+
textPieces,
597613
thePrinter,
598614
theState,
599615
context);
@@ -607,7 +623,7 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
607623
final String text = stringPrefix +
608624
AbstractDirectiveHandler.PREFIX_FOR_KEEPING_LINES_PROCESSED_DIRECTIVES +
609625
extractedDirective;
610-
if (doPrintLn) {
626+
if (doPrintEol) {
611627
thePrinter.println(text, context.getEol());
612628
} else {
613629
thePrinter.print(text);
@@ -619,7 +635,7 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
619635
final String text = stringPrefix +
620636
AbstractDirectiveHandler.PREFIX_FOR_KEEPING_LINES_PROCESSED_DIRECTIVES +
621637
extractedDirective;
622-
if (doPrintLn) {
638+
if (doPrintEol) {
623639
thePrinter.println(text, context.getEol());
624640
} else {
625641
thePrinter.print(text);
@@ -658,26 +674,27 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
658674
firstBlockLineIndex = findLastReadLineIndex(theState);
659675
firstUncommentLine.set(indentText);
660676
}
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);
664680
}
665681
} else {
666682
this.flushTextBufferForRemovedComments(firstUncommentLine,
667683
requireNonNullElse(firstBlockLineIndex, findLastReadLineIndex(theState)),
668-
textBlockBuffer,
684+
textPieces,
669685
thePrinter,
670686
theState, context);
671687
firstBlockLineIndex = null;
672688

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);
677694
}
678695
this.flushTextBufferForRemovedComments(firstUncommentLine,
679696
requireNonNullElse(firstBlockLineIndex, findLastReadLineIndex(theState)),
680-
textBlockBuffer,
697+
textPieces,
681698
thePrinter,
682699
theState, context);
683700
firstBlockLineIndex = null;
@@ -701,27 +718,29 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
701718
firstUncommentLine.set(indentText);
702719
}
703720

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);
707724
}
708725
} else {
709726
this.flushTextBufferForRemovedComments(firstUncommentLine,
710727
requireNonNullElse(firstBlockLineIndex, findLastReadLineIndex(theState)),
711-
textBlockBuffer,
728+
textPieces,
712729
thePrinter,
713730
theState, context);
714731
firstBlockLineIndex = null;
715732

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);
720739
}
721740
this.flushTextBufferForRemovedComments(
722741
firstUncommentLine,
723742
requireNonNullElse(firstBlockLineIndex, findLastReadLineIndex(theState)),
724-
textBlockBuffer,
743+
textPieces,
725744
thePrinter,
726745
theState, context);
727746
firstBlockLineIndex = null;
@@ -730,7 +749,7 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
730749
// Just string
731750
this.flushTextBufferForRemovedComments(firstUncommentLine,
732751
requireNonNullElse(firstBlockLineIndex, findLastReadLineIndex(theState)),
733-
textBlockBuffer,
752+
textPieces,
734753
thePrinter,
735754
theState, context);
736755
firstBlockLineIndex = null;
@@ -745,24 +764,23 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
745764
}
746765

747766
thePrinter.print(stringPrefix);
748-
if (doPrintLn) {
767+
if (doPrintEol) {
749768
thePrinter.println(strToOut, context.getEol());
750769
} else {
751770
thePrinter.print(strToOut);
752771
}
753772
}
754773
} else if (context.isKeepLines()) {
755-
756774
flushTextBufferForRemovedComments(firstUncommentLine,
757775
requireNonNullElse(firstBlockLineIndex, findLastReadLineIndex(theState)),
758-
textBlockBuffer,
776+
textPieces,
759777
thePrinter,
760778
theState,
761779
context);
762780
firstBlockLineIndex = null;
763781

764782
final String text = AbstractDirectiveHandler.PREFIX_FOR_KEEPING_LINES + rawString;
765-
if (doPrintLn) {
783+
if (doPrintEol) {
766784
thePrinter.println(text, context.getEol());
767785
} else {
768786
thePrinter.print(text);

jcp/src/main/java/com/igormaznitsa/jcp/context/CommentTextProcessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public interface CommentTextProcessor extends PreprocessorContextListener {
1616

1717
/**
1818
* Process uncommented text detected in //$ or //$$ sections. If processing not needed then the provided text must be returned.
19-
* Returned text should contain EOL in the end because will be printed into result directly.
2019
*
2120
* @param recommendedIndent indent to be recommended for the processed text if it will be processed
2221
* @param uncommentedText the text which was uncommented and needs processing, must not be null
@@ -36,13 +35,13 @@ String processUncommentedText(
3635
PreprocessingState state);
3736

3837
/**
39-
* Returns flag that the preprocessor is enabled and can be called. It gets full info about current context and file so can make decision on fly.
38+
* Returns flag that the preprocessor is allowed and can be called. It gets full info about current context and file so can make decision on fly.
4039
* It will be called before every processor call.
4140
*
4241
* @return true if the preprocessor allowed, false if not
4342
* @since 7.2.1
4443
*/
45-
boolean isEnabled(
44+
boolean isAllowed(
4645
FileInfoContainer fileContainer,
4746
FilePositionInfo positionInfo,
4847
PreprocessorContext context,

jcp/src/test/java/com/igormaznitsa/jcp/directives/SpecialDirectivesBlockTest.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void onContextStopped(PreprocessorContext context, Throwable error) {
6262
}
6363

6464
@Override
65-
public boolean isEnabled(FileInfoContainer fileContainer, FilePositionInfo positionInfo,
65+
public boolean isAllowed(FileInfoContainer fileContainer, FilePositionInfo positionInfo,
6666
PreprocessorContext context, PreprocessingState state) {
6767
return true;
6868
}
@@ -78,13 +78,13 @@ public String processUncommentedText(int recommendedIndent, String uncommentedTe
7878
assertNotNull(context);
7979
assertNotNull(state);
8080

81-
calledForText.append("...\n").append(uncommentedText);
81+
calledForText.append("\n...\n").append(uncommentedText);
8282

8383
final String indent = context.isPreserveIndents() ? " ".repeat(recommendedIndent) : "";
8484

8585
return Arrays.stream(uncommentedText.split("\\R"))
8686
.map(x -> indent + x)
87-
.collect(Collectors.joining(context.getEol(), "", context.getEol()));
87+
.collect(Collectors.joining(context.getEol()));
8888
}
8989
};
9090

@@ -95,35 +95,35 @@ public String processUncommentedText(int recommendedIndent, String uncommentedTe
9595
});
9696
assertTrue(started.get());
9797
assertTrue(stopped.get());
98-
assertEquals("...\n" +
98+
assertEquals("\n...\n" +
9999
" hello 223 world\n" +
100-
" next\n" +
101-
"...\n" +
100+
" next" +
101+
"\n...\n" +
102102
" hello /*$111+112$*/ world\n" +
103-
" next/*$111+112$*/\n" +
104-
"...\n" +
103+
" next/*$111+112$*/" +
104+
"\n...\n" +
105105
" hello /*$111+112$*/ world\n" +
106106
" middle\n" +
107-
" next/*$111+112$*/\n" +
108-
"...\n" +
109-
" hello /*$111+112$*/ world\n" +
110-
"...\n" +
111-
" split\n" +
112-
"...\n" +
113-
" next/*$111+112$*/\n" +
114-
"...\n" +
115-
" hello 223 world\n" +
116-
"...\n" +
117-
" split\n" +
118-
"...\n" +
119-
" next223\n" +
120-
"...\n" +
121-
" line1\n" +
122-
"...\n" +
123-
" line2\n" +
124-
"...\n" +
107+
" next/*$111+112$*/" +
108+
"\n...\n" +
109+
" hello /*$111+112$*/ world" +
110+
"\n...\n" +
111+
" split" +
112+
"\n...\n" +
113+
" next/*$111+112$*/" +
114+
"\n...\n" +
115+
" hello 223 world" +
116+
"\n...\n" +
117+
" split" +
118+
"\n...\n" +
119+
" next223" +
120+
"\n...\n" +
121+
" line1" +
122+
"\n...\n" +
123+
" line2" +
124+
"\n...\n" +
125125
" hello\n" +
126-
" world earth\n"
126+
" world earth"
127127
, calledForText.toString());
128128
}
129129

0 commit comments

Comments
 (0)