Skip to content

Commit 9d61b2b

Browse files
committed
Support chomping of @SuppressWarnings
Closes gh-33
1 parent d7c2e39 commit 9d61b2b

File tree

6 files changed

+57
-3
lines changed

6 files changed

+57
-3
lines changed

README.adoc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ The attribute value is a space delimited list containing one or more of the foll
355355
| Flag | Description
356356

357357
| `default`
358-
| `tags` `formatters`
358+
| `tags` `formatters` `suppresswarnings`
359359

360360
| `all`
361361
| Enable all chomping
@@ -371,6 +371,12 @@ The attribute value is a space delimited list containing one or more of the foll
371371

372372
| `tags`
373373
| Chomp on the comment tags listed above
374+
375+
| `formatters`
376+
| Chomp any `@formatter:on`/`@formatter:off` comments
377+
378+
| `suppresswarnings`
379+
| Chomp any `@SuppressWarnings` annotations
374380
|===
375381

376382
You can prefix the flag with `+` or `-` at the block level if you want to override a document setting.

src/main/java/io/spring/asciidoctor/backend/codetools/ChompListingContentConverter.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public String convert(Block listingBlock, String content) {
5252
if (options.has(ChompOption.FORMATTERS)) {
5353
chompers.add(new FormatterChomper());
5454
}
55+
if (options.has(ChompOption.SUPPRESSWARNINGS)) {
56+
chompers.add(new SuppressWarningsChomper());
57+
}
5558
return chomp(content, chompers);
5659
}
5760
return content;
@@ -177,4 +180,22 @@ public String chomp(String content) {
177180

178181
}
179182

183+
private static class SuppressWarningsChomper implements Chomper {
184+
185+
@Override
186+
public String chomp(String content) {
187+
StringBuilder result = new StringBuilder(content.length());
188+
String[] lines = content.split("\n");
189+
for (String line : lines) {
190+
String updatedline = line.replaceAll("@SuppressWarnings\\(.*\\)", "");
191+
if (updatedline.equals(line) || !updatedline.trim().isEmpty()) {
192+
result.append(updatedline);
193+
result.append("\n");
194+
}
195+
}
196+
return result.toString();
197+
}
198+
199+
}
200+
180201
}

src/main/java/io/spring/asciidoctor/backend/codetools/ChompOption.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ enum ChompOption {
4141
/**
4242
* Chomp and formatter on/off comments.
4343
*/
44-
FORMATTERS;
44+
FORMATTERS,
4545

46-
static final ChompOption[] DEFAULTS = { TAGS };
46+
/**
47+
* Chomp {@code @SuppressWarnings} annotation.
48+
*/
49+
SUPPRESSWARNINGS;
50+
51+
static final ChompOption[] DEFAULTS = { TAGS, FORMATTERS, SUPPRESSWARNINGS };
4752

4853
}

src/test/java/io/spring/asciidoctor/backend/codetools/ChompListingContentConverterTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ void convertWhenMethodFormattersChompReturnsChompedLines(ConvertedHtml html, Exp
9292
assertThat(html.getElementHtml("code")).satisfies(expected);
9393
}
9494

95+
@Test
96+
void convertWhenSuppressWarningsChompReturnsChompedLines(ConvertedHtml html, ExpectedHtml expected) {
97+
assertThat(html.getElementHtml("code")).satisfies(expected);
98+
}
99+
95100
@Test
96101
void convertWhenMultipleChompsReturnsChompedLines(ConvertedHtml html, ExpectedHtml expected) {
97102
assertThat(html.getElementHtml("code")).satisfies(expected);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
= Test Document
2+
:chomp: suppresswarnings
3+
4+
[source,java]
5+
----
6+
public class Example {
7+
8+
@SuppressWarnings("unused")
9+
private String field;
10+
11+
}
12+
----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Example {
2+
3+
private String field;
4+
5+
}

0 commit comments

Comments
 (0)