Skip to content

Commit 72e9e01

Browse files
committed
Scala parser: support parameterless methods, annotated methods, remove while fallback
- Parameterless methods (def name: Type) now map to J.MethodDeclaration with OmitBraces marker on empty param container — printer omits () - Annotated methods (@deprecated def ...) now map to J.MethodDeclaration with annotations extracted and def-modifier carrying annotation-to-def spacing - Remove while loop fallback — while loops in method bodies handled by parser - Remove AppliedTypeTree fallback (already fixed in #7260) - 3 remaining fallbacks: procedure syntax, nested braces, function type params - 5 test failures: 3 complex annotations (with args), 2 while body block whitespace
1 parent 9258932 commit 72e9e01

File tree

4 files changed

+297
-139
lines changed

4 files changed

+297
-139
lines changed

rewrite-scala/src/main/java/org/openrewrite/scala/ScalaPrinter.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -287,31 +287,45 @@ public J visitCase(J.Case case_, PrintOutputCapture<P> p) {
287287
public J visitMethodDeclaration(J.MethodDeclaration method, PrintOutputCapture<P> p) {
288288
beforeSyntax(method, Space.Location.METHOD_DECLARATION_PREFIX, p);
289289
visit(method.getLeadingAnnotations(), p);
290+
boolean defAlreadyPrinted = false;
290291
for (J.Modifier m : method.getModifiers()) {
291-
visit(m, p);
292+
if ("def".equals(m.getKeyword()) && m.getType() == J.Modifier.Type.LanguageExtension) {
293+
visitSpace(m.getPrefix(), Space.Location.MODIFIER_PREFIX, p);
294+
p.append("def");
295+
defAlreadyPrinted = true;
296+
} else {
297+
visit(m, p);
298+
}
292299
}
293-
294-
if (!method.getModifiers().isEmpty()) {
295-
p.append(" ");
300+
if (!defAlreadyPrinted) {
301+
if (!method.getModifiers().isEmpty()) {
302+
p.append(" ");
303+
}
304+
p.append("def");
296305
}
297-
p.append("def");
298306
visit(method.getName(), p);
299307

300308
if (method.getPadding().getTypeParameters() != null) {
301309
visit(method.getPadding().getTypeParameters(), p);
302310
}
303311

304-
// Print parameters (name: Type)
312+
// Print parameters — skip parens for parameterless methods (marked with OmitBraces)
305313
JContainer<Statement> params = method.getPadding().getParameters();
306-
visitSpace(params.getBefore(), Space.Location.METHOD_DECLARATION_PARAMETERS, p);
307-
p.append('(');
314+
boolean hasParens = !params.getMarkers().findFirst(
315+
org.openrewrite.scala.marker.OmitBraces.class).isPresent();
316+
if (hasParens) {
317+
visitSpace(params.getBefore(), Space.Location.METHOD_DECLARATION_PARAMETERS, p);
318+
p.append('(');
319+
}
308320
List<JRightPadded<Statement>> paramList = params.getPadding().getElements();
309321
for (int i = 0; i < paramList.size(); i++) {
310322
JRightPadded<Statement> param = paramList.get(i);
311323
Statement element = param.getElement();
312324
if (element instanceof J.VariableDeclarations) {
313325
J.VariableDeclarations varDecl = (J.VariableDeclarations) element;
314326
visitSpace(varDecl.getPrefix(), Space.Location.VARIABLE_DECLARATIONS_PREFIX, p);
327+
// Print parameter annotations (@unchecked, etc.)
328+
visit(varDecl.getLeadingAnnotations(), p);
315329
if (!varDecl.getVariables().isEmpty()) {
316330
visit(varDecl.getVariables().get(0).getName(), p);
317331
}
@@ -342,22 +356,28 @@ public J visitMethodDeclaration(J.MethodDeclaration method, PrintOutputCapture<P
342356
p.append(',');
343357
}
344358
}
345-
p.append(')');
359+
if (hasParens) {
360+
p.append(')');
361+
}
346362

347363
if (method.getReturnTypeExpression() != null) {
348364
p.append(':');
349365
visit(method.getReturnTypeExpression(), p);
350366
}
351367

352368
if (method.getBody() != null) {
369+
// Procedure syntax (OmitBraces on method) — no " =" before body
370+
boolean procedureSyntax = method.getMarkers().findFirst(
371+
org.openrewrite.scala.marker.OmitBraces.class).isPresent();
353372
J.Block body = method.getBody();
354-
boolean omitBraces = body.getMarkers().findFirst(
373+
boolean omitBodyBraces = body.getMarkers().findFirst(
355374
org.openrewrite.scala.marker.OmitBraces.class).isPresent();
356-
if (omitBraces && body.getStatements().size() == 1) {
375+
if (!procedureSyntax) {
357376
p.append(" =");
377+
}
378+
if (omitBodyBraces && body.getStatements().size() == 1) {
358379
visit(body.getStatements().get(0), p);
359380
} else {
360-
p.append(" =");
361381
visit(body, p);
362382
}
363383
}

0 commit comments

Comments
 (0)