@@ -121,4 +121,50 @@ void addImportInPresenceOfDuplicateOtherImport() {
121
121
.containsExactlyInAnyOrder (
122
122
import1 , import1 , new JRightPadded <>(importToAdd , Space .EMPTY , Markers .EMPTY ));
123
123
}
124
+
125
+ @ Test
126
+ @ Issue ("https://github.com/openrewrite/rewrite/issues/4241" )
127
+ void addImportWithNewLineInUnsortedImportList () {
128
+ ImportLayoutStyle style = new ImportLayoutStyle (
129
+ Integer .MAX_VALUE , Integer .MAX_VALUE , Collections .emptyList (), Collections .emptyList ());
130
+ JRightPadded <J .Import > import0 = new JRightPadded <>(
131
+ new J .Import (
132
+ randomId (),
133
+ Space .EMPTY ,
134
+ Markers .EMPTY ,
135
+ new JLeftPadded <>(Space .SINGLE_SPACE , true , Markers .EMPTY ),
136
+ TypeTree .build ("pkg.AAA.MEMBER_0" ).withPrefix (Space .SINGLE_SPACE ),
137
+ null ),
138
+ Space .EMPTY ,
139
+ Markers .EMPTY );
140
+ JRightPadded <J .Import > import1 = new JRightPadded <>(
141
+ new J .Import (
142
+ randomId (),
143
+ Space .EMPTY ,
144
+ Markers .EMPTY ,
145
+ new JLeftPadded <>(Space .SINGLE_SPACE , true , Markers .EMPTY ),
146
+ TypeTree .build ("pkg.Clazz.MEMBER_3" ).withPrefix (Space .SINGLE_SPACE ),
147
+ null ),
148
+ Space .EMPTY ,
149
+ Markers .EMPTY );
150
+ JRightPadded <J .Import > import3 = new JRightPadded <>(
151
+ new J .Import (
152
+ randomId (),
153
+ Space .EMPTY ,
154
+ Markers .EMPTY ,
155
+ new JLeftPadded <>(Space .SINGLE_SPACE , true , Markers .EMPTY ),
156
+ TypeTree .build ("pkg.Clazz.MEMBER_1" ).withPrefix (Space .SINGLE_SPACE ),
157
+ null ),
158
+ Space .EMPTY ,
159
+ Markers .EMPTY );
160
+ J .Import importToAdd = new J .Import (
161
+ randomId (),
162
+ Space .EMPTY ,
163
+ Markers .EMPTY ,
164
+ new JLeftPadded <>(Space .EMPTY , true , Markers .EMPTY ),
165
+ TypeTree .build ("pkg.Clazz.MEMBER_2" ).withPrefix (Space .SINGLE_SPACE ),
166
+ null );
167
+
168
+ assertThat (style .addImport (List .of (import0 , import1 , import3 ), importToAdd , null , Collections .emptyList ()).get (1 ).getElement ().getPrefix ()).isEqualTo (Space .format ("\n " ));
169
+ }
124
170
}
0 commit comments