@@ -232,34 +232,41 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integ
232232 .orElseThrow (() -> new IllegalArgumentException ("Could not parse as Gradle" ));
233233
234234 if (FindMethods .find (cu , "RewriteGradleProject plugins(..)" ).isEmpty () && FindMethods .find (cu , "RewriteSettings plugins(..)" ).isEmpty ()) {
235- if (cu .getSourcePath ().endsWith (Paths .get ("settings.gradle" )) &&
236- !cu .getStatements ().isEmpty () &&
237- cu .getStatements ().get (0 ) instanceof J .MethodInvocation &&
238- "pluginManagement" .equals (((J .MethodInvocation ) cu .getStatements ().get (0 )).getSimpleName ())) {
239- return cu .withStatements (ListUtils .insert (cu .getStatements (), autoFormat (statement .withPrefix (Space .format ("\n \n " )), ctx , getCursor ()), 1 ));
235+ int insertAtIdx = 0 ;
236+
237+ if (cu .getSourcePath ().endsWith (Paths .get ("settings.gradle" ))) {
238+ // For settings.gradle, find the last position of pluginManagement or buildscript
239+ for (int i = 0 ; i < cu .getStatements ().size (); i ++) {
240+ Statement existingStatement = cu .getStatements ().get (i );
241+ if (existingStatement instanceof J .MethodInvocation ) {
242+ String methodName = ((J .MethodInvocation ) existingStatement ).getSimpleName ();
243+ if ("pluginManagement" .equals (methodName ) || "buildscript" .equals (methodName )) {
244+ insertAtIdx = i + 1 ;
245+ }
246+ }
247+ }
240248 } else {
241- int insertAtIdx = 0 ;
242249 for (int i = 0 ; i < cu .getStatements ().size (); i ++) {
243250 Statement existingStatement = cu .getStatements ().get (i );
244251 if (existingStatement instanceof J .MethodInvocation && "buildscript" .equals (((J .MethodInvocation ) existingStatement ).getSimpleName ())) {
245252 insertAtIdx = i + 1 ;
246253 break ;
247254 }
248255 }
249- if (insertAtIdx == 0 ) {
250- Comment licenseHeader = getLicenseHeader (cu );
251- if (licenseHeader != null ) {
252- cu = (G .CompilationUnit ) removeLicenseHeader (cu );
253- statement = statement .withComments (singletonList (licenseHeader ));
254- }
255- Space leadingSpace = Space .firstPrefix (cu .getStatements ());
256- return cu .withStatements (ListUtils .insert (
257- Space .formatFirstPrefix (cu .getStatements (), leadingSpace .withWhitespace ("\n \n " + leadingSpace .getWhitespace ())),
258- autoFormat (statement , ctx , getCursor ()),
259- insertAtIdx ));
260- } else {
261- return cu .withStatements (ListUtils .insert (cu .getStatements (), autoFormat (statement .withPrefix (Space .format ("\n \n " )), ctx , getCursor ()), insertAtIdx ));
256+ }
257+ if (insertAtIdx == 0 ) {
258+ Comment licenseHeader = getLicenseHeader (cu );
259+ if (licenseHeader != null ) {
260+ cu = (G .CompilationUnit ) removeLicenseHeader (cu );
261+ statement = statement .withComments (singletonList (licenseHeader ));
262262 }
263+ Space leadingSpace = Space .firstPrefix (cu .getStatements ());
264+ return cu .withStatements (ListUtils .insert (
265+ Space .formatFirstPrefix (cu .getStatements (), leadingSpace .withWhitespace ("\n \n " + leadingSpace .getWhitespace ())),
266+ autoFormat (statement , ctx , getCursor ()),
267+ insertAtIdx ));
268+ } else {
269+ return cu .withStatements (ListUtils .insert (cu .getStatements (), autoFormat (statement .withPrefix (Space .format ("\n \n " )), ctx , getCursor ()), insertAtIdx ));
263270 }
264271 } else {
265272 MethodMatcher buildPluginsMatcher = new MethodMatcher ("RewriteGradleProject plugins(groovy.lang.Closure)" );
@@ -345,7 +352,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Atomi
345352 return ((K .CompilationUnit ) parsed );
346353 })
347354 .map (parsed -> parsed .getStatements ().get (0 ))
348- .map (block -> ((J .Block )block ).getStatements ().get (0 ))
355+ .map (block -> ((J .Block ) block ).getStatements ().get (0 ))
349356 .orElseThrow (() -> new IllegalArgumentException ("Could not parse as Gradle" ));
350357
351358 AtomicBoolean hasPluginsBlock = new JavaIsoVisitor <AtomicBoolean >() {
@@ -360,40 +367,47 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Atomi
360367 }.reduce (cu , new AtomicBoolean ());
361368 if (!hasPluginsBlock .get ()) {
362369 J .Block block = (J .Block ) cu .getStatements ().get (0 );
363- if (cu .getSourcePath ().endsWith (Paths .get ("settings.gradle" )) &&
364- !block .getStatements ().isEmpty () &&
365- block .getStatements ().get (0 ) instanceof J .MethodInvocation &&
366- "pluginManagement" .equals (((J .MethodInvocation ) block .getStatements ().get (0 )).getSimpleName ())) {
367- block = block .withStatements (ListUtils .insert (block .getStatements (), autoFormat (statement .withPrefix (Space .format ("\n \n " )), ctx , getCursor ()), 1 ));
370+ int insertAtIdx = 0 ;
371+
372+ if (cu .getSourcePath ().endsWith (Paths .get ("settings.gradle.kts" ))) {
373+ for (int i = 0 ; i < block .getStatements ().size (); i ++) {
374+ Statement existingStatement = block .getStatements ().get (i );
375+ if (existingStatement instanceof J .MethodInvocation ) {
376+ String methodName = ((J .MethodInvocation ) existingStatement ).getSimpleName ();
377+ if ("pluginManagement" .equals (methodName ) || "buildscript" .equals (methodName )) {
378+ insertAtIdx = i + 1 ;
379+ }
380+ }
381+ }
368382 } else {
369- int insertAtIdx = 0 ;
370- for (int i = 0 ; i < cu .getStatements ().size (); i ++) {
371- Statement existingStatement = cu .getStatements ().get (i );
383+ for (int i = 0 ; i < block .getStatements ().size (); i ++) {
384+ Statement existingStatement = block .getStatements ().get (i );
372385 if (existingStatement instanceof J .MethodInvocation && "buildscript" .equals (((J .MethodInvocation ) existingStatement ).getSimpleName ())) {
373386 insertAtIdx = i + 1 ;
374387 break ;
375388 }
376389 }
377- if (insertAtIdx == 0 ) {
378- Comment licenseHeader = getLicenseHeader (cu );
379- if (licenseHeader != null ) {
380- cu = (K .CompilationUnit ) removeLicenseHeader (cu );
381- block = (J .Block ) cu .getStatements ().get (0 );
382- statement = statement .withComments (singletonList (licenseHeader ));
383- }
384- Space leadingSpace = Space .firstPrefix (block .getStatements ());
385- block = block .withStatements (ListUtils .insert (
386- Space .formatFirstPrefix (block .getStatements (), leadingSpace .withWhitespace ("\n \n " + leadingSpace .getWhitespace ())),
387- statement ,
388- insertAtIdx ));
389- } else {
390- block = block .withStatements (ListUtils .insert (block .getStatements (), statement .withPrefix (Space .format ("\n \n " )), insertAtIdx ));
390+ }
391+
392+ if (insertAtIdx == 0 ) {
393+ Comment licenseHeader = getLicenseHeader (cu );
394+ if (licenseHeader != null ) {
395+ cu = (K .CompilationUnit ) removeLicenseHeader (cu );
396+ block = (J .Block ) cu .getStatements ().get (0 );
397+ statement = statement .withComments (singletonList (licenseHeader ));
391398 }
399+ Space leadingSpace = Space .firstPrefix (block .getStatements ());
400+ block = block .withStatements (ListUtils .insert (
401+ Space .formatFirstPrefix (block .getStatements (), leadingSpace .withWhitespace ("\n \n " + leadingSpace .getWhitespace ())),
402+ statement ,
403+ insertAtIdx ));
404+ } else {
405+ block = block .withStatements (ListUtils .insert (block .getStatements (), statement .withPrefix (Space .format ("\n \n " )), insertAtIdx ));
392406 }
393407 J .Block newStatement = block ;
394408 return cu .withStatements (ListUtils .mapFirst (cu .getStatements (), __ -> newStatement ));
395409 } else {
396- J .MethodInvocation pluginDef = (J .MethodInvocation )(((J .Block ) ((J .Lambda ) ((J .MethodInvocation ) statement ).getArguments ().get (0 )).getBody ()).getStatements ().get (0 ));
410+ J .MethodInvocation pluginDef = (J .MethodInvocation ) (((J .Block ) ((J .Lambda ) ((J .MethodInvocation ) statement ).getArguments ().get (0 )).getBody ()).getStatements ().get (0 ));
397411 return cu .withStatements (ListUtils .mapFirst (cu .getStatements (), b -> {
398412 if (b instanceof J .Block ) {
399413 J .Block block = (J .Block ) b ;
0 commit comments