@@ -208,6 +208,86 @@ public J visitTypeCast(J.TypeCast typeCast, PrintOutputCapture<P> p) {
208208 return typeCast ;
209209 }
210210
211+ @ Override
212+ public J visitMethodDeclaration (J .MethodDeclaration method , PrintOutputCapture <P > p ) {
213+ beforeSyntax (method , Space .Location .METHOD_DECLARATION_PREFIX , p );
214+ visit (method .getLeadingAnnotations (), p );
215+ for (J .Modifier m : method .getModifiers ()) {
216+ visit (m , p );
217+ }
218+
219+ p .append ("def" );
220+ visit (method .getName (), p );
221+
222+ if (method .getPadding ().getTypeParameters () != null ) {
223+ visit (method .getPadding ().getTypeParameters (), p );
224+ }
225+
226+ // Print parameters (name: Type)
227+ JContainer <Statement > params = method .getPadding ().getParameters ();
228+ visitSpace (params .getBefore (), Space .Location .METHOD_DECLARATION_PARAMETERS , p );
229+ p .append ('(' );
230+ List <JRightPadded <Statement >> paramList = params .getPadding ().getElements ();
231+ for (int i = 0 ; i < paramList .size (); i ++) {
232+ JRightPadded <Statement > param = paramList .get (i );
233+ Statement element = param .getElement ();
234+ if (element instanceof J .VariableDeclarations ) {
235+ J .VariableDeclarations varDecl = (J .VariableDeclarations ) element ;
236+ visitSpace (varDecl .getPrefix (), Space .Location .VARIABLE_DECLARATIONS_PREFIX , p );
237+ if (!varDecl .getVariables ().isEmpty ()) {
238+ visit (varDecl .getVariables ().get (0 ).getName (), p );
239+ }
240+ if (varDecl .getTypeExpression () != null ) {
241+ // The colon and space between name and type in Scala parameter syntax
242+ // Type prefix from parser may or may not include the space
243+ TypeTree typeExpr = varDecl .getTypeExpression ();
244+ if (typeExpr .getPrefix ().isEmpty ()) {
245+ p .append (": " );
246+ } else {
247+ p .append (":" );
248+ }
249+ visit (typeExpr , p );
250+ }
251+ if (!varDecl .getVariables ().isEmpty () &&
252+ varDecl .getVariables ().get (0 ).getPadding ().getInitializer () != null ) {
253+ JLeftPadded <Expression > init = varDecl .getVariables ().get (0 ).getPadding ().getInitializer ();
254+ if (init .getBefore ().isEmpty ()) {
255+ p .append (" " );
256+ }
257+ visitLeftPadded ("=" , init , JLeftPadded .Location .VARIABLE_INITIALIZER , p );
258+ }
259+ } else {
260+ visit (element , p );
261+ }
262+ if (i < paramList .size () - 1 ) {
263+ visitSpace (param .getAfter (), JRightPadded .Location .METHOD_DECLARATION_PARAMETER .getAfterLocation (), p );
264+ p .append (',' );
265+ }
266+ }
267+ p .append (')' );
268+
269+ if (method .getReturnTypeExpression () != null ) {
270+ p .append (':' );
271+ visit (method .getReturnTypeExpression (), p );
272+ }
273+
274+ if (method .getBody () != null ) {
275+ J .Block body = method .getBody ();
276+ boolean omitBraces = body .getMarkers ().findFirst (
277+ org .openrewrite .scala .marker .OmitBraces .class ).isPresent ();
278+ if (omitBraces && body .getStatements ().size () == 1 ) {
279+ p .append (" =" );
280+ visit (body .getStatements ().get (0 ), p );
281+ } else {
282+ p .append (" =" );
283+ visit (body , p );
284+ }
285+ }
286+
287+ afterSyntax (method , p );
288+ return method ;
289+ }
290+
211291 @ Override
212292 protected void printStatementTerminator (Statement s , PrintOutputCapture <P > p ) {
213293 // In Scala, semicolons are optional and generally not used
0 commit comments