Skip to content

Commit

Permalink
[formatter] Recognize header comments in module-info.java (eclipse-jd…
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-matela committed Mar 17, 2024
1 parent 560a301 commit b6a1206
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7774,4 +7774,45 @@ public void testBug552012d() {
"}";
formatSource(source);
}
/**
* https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2127
*/
public void testIssue2127a() {
setComplianceLevel(CompilerOptions.VERSION_9);
String source = """
/** This is a header comment */
/** This is a module javadoc */
module test{}
""";
formatSource(source, """
/** This is a header comment */
/** This is a module javadoc */
module test {
}
""",
CodeFormatter.K_MODULE_INFO | CodeFormatter.F_INCLUDE_COMMENTS);
}
/**
* https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2127
*/
public void testIssue2127b() {
setComplianceLevel(CompilerOptions.VERSION_9);
this.formatterPrefs.comment_format_header = true;
String source = """
/** This is a header comment */
/** This is a module javadoc */
module test{}
""";
formatSource(source, """
/** This is a header comment */
/** This is a module javadoc */
module test {
}
""",
CodeFormatter.K_MODULE_INFO | CodeFormatter.F_INCLUDE_COMMENTS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,11 @@ private List<Token> prepareFormattedCode(String source, int kind) {
}

private void findHeader() {
if (this.astRoot instanceof CompilationUnit) {
CompilationUnit unit = (CompilationUnit) this.astRoot;
if (this.astRoot instanceof CompilationUnit unit) {
List<TypeDeclaration> types = unit.types();
ASTNode firstElement = types.isEmpty() ? unit.getPackage() : types.get(0);
ASTNode firstElement = !types.isEmpty() ? types.get(0)
: unit.getModule() != null ? unit.getModule()
: unit.getPackage();
if (firstElement != null) {
int headerEndIndex = this.tokenManager.firstIndexIn(firstElement, -1);
this.tokenManager.setHeaderEndIndex(headerEndIndex);
Expand Down

0 comments on commit b6a1206

Please sign in to comment.