Skip to content

Commit

Permalink
Fix spaces around type parameters in class/interface/record declarati…
Browse files Browse the repository at this point in the history
…ons #443
  • Loading branch information
mateusz-matela committed Oct 2, 2022
1 parent 5fd2839 commit f2bcccf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.jdt.core.formatter.IndentManipulation;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions;
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions.Alignment;
Expand Down Expand Up @@ -13305,4 +13306,24 @@ public void testBug576954() {
" }\n" +
"}");
}
/**
* https://github.com/eclipse-jdt/eclipse.jdt.core/issues/443
*/
public void testIssue443a() {
setComplianceLevel(CompilerOptions.VERSION_17);
this.formatterPrefs.insert_space_after_closing_angle_bracket_in_type_parameters = true;
formatSource(
"record MyRecord<A>() {\n" +
"}");
}
public void testIssue443b() {
setComplianceLevel(CompilerOptions.VERSION_17);
this.formatterPrefs.insert_space_after_closing_angle_bracket_in_type_parameters = false;
formatSource(
"class MyClass<A> extends AnotherClass {\n" +
"}\n" +
"\n" +
"sealed interface Expr<A> permits MathExpr {\n" +
"}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,12 @@ public boolean visit(TypeDeclaration node) {
List<TypeParameter> typeParameters = node.typeParameters();
handleTypeParameters(typeParameters);

if (!node.isInterface() && !node.superInterfaceTypes().isEmpty()) {
// fix for: class A<E> extends ArrayList<String>implements Callable<String>
handleToken(node.getName(), TokenNameimplements, true, false);
}
if (!node.superInterfaceTypes().isEmpty())
handleTokenAfter(node.getName(), node.isInterface() ? TokenNameextends : TokenNameimplements, true, true);
if (node.getSuperclassType() != null)
handleTokenAfter(node.getName(), TokenNameextends, true, true);
if (!node.permittedTypes().isEmpty())
handleTokenAfter(node.getName(), TokenNameRestrictedIdentifierpermits, true, true);

handleToken(node.getName(), TokenNameLBRACE,
this.options.insert_space_before_opening_brace_in_type_declaration, false);
Expand Down Expand Up @@ -366,7 +368,8 @@ private void handleTypeParameters(List<TypeParameter> typeParameters) {
this.options.insert_space_after_opening_angle_bracket_in_type_parameters);
handleTokenAfter(typeParameters.get(typeParameters.size() - 1), TokenNameGREATER,
this.options.insert_space_before_closing_angle_bracket_in_type_parameters,
this.options.insert_space_after_closing_angle_bracket_in_type_parameters);
typeParameters.get(0).getParent() instanceof RecordDeclaration ? false
: this.options.insert_space_after_closing_angle_bracket_in_type_parameters);
handleCommas(typeParameters, this.options.insert_space_before_comma_in_type_parameters,
this.options.insert_space_after_comma_in_type_parameters);
}
Expand Down

0 comments on commit f2bcccf

Please sign in to comment.