Skip to content

Commit 0181356

Browse files
committed
#46 improved JCP comment line remover, now removing comments for jcp markers
1 parent fea3db2 commit 0181356

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

jcp/src/main/java/com/igormaznitsa/jcp/removers/JcpCommentLineRemover.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
package com.igormaznitsa.jcp.removers;
2323

24+
import static com.igormaznitsa.jcp.directives.AbstractDirectiveHandler.PREFIX_FOR_KEEPING_LINES;
25+
import static com.igormaznitsa.jcp.directives.AbstractDirectiveHandler.PREFIX_FOR_KEEPING_LINES_PROCESSED_DIRECTIVES;
26+
2427
import java.io.IOException;
2528
import java.io.Reader;
2629
import java.io.Writer;
@@ -71,18 +74,35 @@ public Writer process() throws IOException {
7174
switch (chr) {
7275
case '$':
7376
case '#': {
74-
jcpBuffer.setLength(0);
75-
skipTillNextString();
76-
state = STATE_NORMAL;
77-
}
78-
break;
79-
default: {
80-
if (Character.isSpaceChar(chr) && this.whiteSpaceAllowed) {
81-
jcpBuffer.append((char) chr);
77+
if (jcpBuffer.toString().equals("//") ||
78+
(jcpBuffer.substring(2).trim().isEmpty() && this.whiteSpaceAllowed)) {
79+
jcpBuffer.setLength(0);
80+
skipTillNextString();
81+
state = STATE_NORMAL;
8282
} else {
8383
this.dstWriter.write(jcpBuffer.toString());
8484
this.dstWriter.write(chr);
85+
this.copyTillNextString();
86+
}
87+
}
88+
break;
89+
default: {
90+
jcpBuffer.append((char) chr);
91+
final String currentBuffer = jcpBuffer.toString();
92+
if (currentBuffer.startsWith(PREFIX_FOR_KEEPING_LINES) ||
93+
currentBuffer.startsWith(PREFIX_FOR_KEEPING_LINES_PROCESSED_DIRECTIVES)) {
94+
jcpBuffer.setLength(0);
95+
this.skipTillNextString();
96+
state = STATE_NORMAL;
97+
} else if (chr == '\n') {
98+
this.dstWriter.write(currentBuffer);
99+
jcpBuffer.setLength(0);
100+
state = STATE_NORMAL;
101+
} else if ((!PREFIX_FOR_KEEPING_LINES.startsWith(currentBuffer) &&
102+
!PREFIX_FOR_KEEPING_LINES_PROCESSED_DIRECTIVES.startsWith(currentBuffer)) &&
103+
(!this.whiteSpaceAllowed || !Character.isSpaceChar(chr))) {
85104
jcpBuffer.setLength(0);
105+
this.dstWriter.write(currentBuffer);
86106
this.copyTillNextString();
87107
state = STATE_NORMAL;
88108
}

jcp/src/test/java/com/igormaznitsa/jcp/removers/JcpCommentLineRemoverTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ public void testCommentedJcpDirective() throws Exception {
9393
}
9494
}
9595

96+
@Test
97+
public void testCommentedJcpDirectiveAndJcpMarkedLines() throws Exception {
98+
this.assertCommentRemove(
99+
"/\n//\n// JCP> test\n//JCP test\n//JCP! some jcp\n/** some multiline //$ //# test\nwith jcp directives*/\n//$ some\n//JCP> some line \n//$$$ some two\n// hello world\n////#if DEBUG\nSystem.out.println(\"DEBUG\");\n//#else\nSystem.out.println(\"RELEASE\");\n//#endif\n// end\n//JCP",
100+
"/\n//\n// JCP> test\n//JCP test\n\n/** some multiline //$ //# test\nwith jcp directives*/\n\n\n\n// hello world\n////#if DEBUG\nSystem.out.println(\"DEBUG\");\n\nSystem.out.println(\"RELEASE\");\n\n// end\n//JCP"
101+
);
102+
103+
if (this.whiteSpaced) {
104+
this.assertCommentRemove(
105+
"/\n//\n// JCP> test\n//JCP test\n//JCP! some jcp\n/** some multiline //$ //# test\nwith jcp directives*/\n// $ some\n//JCP> some line \n// $$$ some two\n// hello world\n//// #if DEBUG\nSystem.out.println(\"DEBUG\");\n// # else\nSystem.out.println(\"RELEASE\");\n// #endif\n// end\n//JCP",
106+
"/\n//\n// JCP> test\n//JCP test\n\n/** some multiline //$ //# test\nwith jcp directives*/\n\n\n\n// hello world\n//// #if DEBUG\nSystem.out.println(\"DEBUG\");\n\nSystem.out.println(\"RELEASE\");\n\n// end\n//JCP"
107+
);
108+
}
109+
}
110+
96111
@Test
97112
public void testLineCommentInTheEnd() throws Exception {
98113
this.assertCommentRemove(

0 commit comments

Comments
 (0)