Skip to content

Commit 76c2a1c

Browse files
committed
skip markdown doc comments for java <23
1 parent 7439a8a commit 76c2a1c

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/main/java/io/papermc/typewriter/context/SourcesMetadata.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.papermc.typewriter.context;
22

3+
import com.google.common.base.Preconditions;
34
import org.checkerframework.checker.nullness.qual.NonNull;
45
import org.checkerframework.framework.qual.DefaultQualifier;
56
import org.jetbrains.annotations.Contract;
@@ -14,9 +15,11 @@
1415
*
1516
* @param importLayout the import layout used
1617
* @param indentUnit the default indent unit used if no one is found in the source file
18+
* @param classpath the targeted classpath to resolve class reference during import parsing
19+
* @param javaVersion the version of java the generated code must be compliant to
1720
*/
1821
@DefaultQualifier(NonNull.class)
19-
public record SourcesMetadata(ImportLayout importLayout, IndentUnit indentUnit, Set<Path> classpath) {
22+
public record SourcesMetadata(ImportLayout importLayout, IndentUnit indentUnit, Set<Path> classpath, int javaVersion) {
2023

2124
/**
2225
* Constructs a file metadata with the default import layout and
@@ -33,11 +36,16 @@ public static SourcesMetadata of(IndentUnit indentUnit) {
3336
return of(indentUnit, UnaryOperator.identity());
3437
}
3538

39+
public boolean canSkipMarkdownDocComments() {
40+
return Boolean.getBoolean("typewriter.lexer.ignoreMarkdownDocComments") || this.javaVersion < 23;
41+
}
42+
3643
public static class Builder {
3744

3845
private final IndentUnit indentUnit;
3946
private ImportLayout layout = ImportLayout.DEFAULT;
4047
private Set<Path> classpath = Collections.emptySet();
48+
private int javaVersion = Runtime.version().feature();
4149

4250
Builder(IndentUnit indentUnit) {
4351
this.indentUnit = indentUnit;
@@ -55,11 +63,19 @@ public Builder classpath(Set<Path> classpath) {
5563
return this;
5664
}
5765

66+
@Contract(value = "_ -> this", mutates = "this")
67+
public Builder javaVersion(int featureVersion) {
68+
Preconditions.checkArgument(featureVersion >= 0, "Feature version must be non-negative");
69+
this.javaVersion = featureVersion;
70+
return this;
71+
}
72+
5873
SourcesMetadata complete() {
5974
return new SourcesMetadata(
6075
this.layout,
6176
this.indentUnit,
62-
this.classpath
77+
this.classpath,
78+
this.javaVersion
6379
);
6480
}
6581
}

src/main/java/io/papermc/typewriter/parser/Lexer.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222

2323
public class Lexer extends UnicodeTranslator implements Tokenizer {
2424

25-
private static final boolean CHECK_MARKDOWN_DOC_COMMENTS = !Boolean.getBoolean("typewriter.lexer.ignoreMarkdownDocComments");
26-
2725
private final StringBuilder buffer; // generic buffer for single line element or used as temporary storage before being pushed into line buffer
2826
private final List<String> lineBuffer; // line buffer for multi line block
2927
private RelativeTextBlock textBlock; // text block support
3028

29+
// toggleable features
30+
public boolean checkMarkdownDocComments = true;
31+
3132
public Lexer(char[] input) {
3233
super(input);
3334
this.buffer = new StringBuilder();
@@ -448,7 +449,7 @@ public Token readToken() {
448449
tokenPos.begin();
449450
this.incrCursor();
450451
if (match('/')) {
451-
if (CHECK_MARKDOWN_DOC_COMMENTS && match('/')) {
452+
if (this.checkMarkdownDocComments && match('/')) {
452453
type = TokenType.MARKDOWN_JAVADOC;
453454
this.readMarkdownJavadoc(tokenPos);
454455
} else {

src/main/java/io/papermc/typewriter/replace/SearchReplaceRewriterBase.java

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void writeToFile(Path parent, SourcesMetadata sourcesMetadata, ClassResol
5252
final Lexer lex;
5353
try (BufferedReader buffer = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
5454
lex = Lexer.fromReader(buffer);
55+
lex.checkMarkdownDocComments = !sourcesMetadata.canSkipMarkdownDocComments();
5556
} catch (ReaderException ex) {
5657
throw ex.withAdditionalContext(file);
5758
}

0 commit comments

Comments
 (0)