Skip to content

Commit 8394dd8

Browse files
Feature: Added ability to add property in unordered manner at end of file (#6052)
* Feature: Added ability to add property in unordered manner at end of file * Fixed referenced calls by adding new parameters * space * space * More concise description --------- Co-authored-by: Greg Oledzki <greg.oledzki@moderne.io>
1 parent 905c3e7 commit 8394dd8

3 files changed

Lines changed: 75 additions & 8 deletions

File tree

rewrite-gradle/src/main/java/org/openrewrite/gradle/AddProperty.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
130130
sourceFile :
131131
new ChangePropertyValue(key, value, null, false, null)
132132
.getVisitor().visitNonNull(sourceFile, ctx);
133-
return new org.openrewrite.properties.AddProperty(key, value, null, null)
133+
return new org.openrewrite.properties.AddProperty(key, value, null, null, null)
134134
.getVisitor()
135135
.visitNonNull(t, ctx);
136136
}
@@ -139,7 +139,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
139139
sourceFile :
140140
new ChangePropertyValue(key, value, null, false, null)
141141
.getVisitor().visitNonNull(sourceFile, ctx);
142-
return new org.openrewrite.properties.AddProperty(key, value, null, null)
142+
return new org.openrewrite.properties.AddProperty(key, value, null, null, null)
143143
.getVisitor()
144144
.visitNonNull(t, ctx);
145145
}

rewrite-properties/src/main/java/org/openrewrite/properties/AddProperty.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public class AddProperty extends Recipe {
6262
@Nullable
6363
String delimiter;
6464

65+
@Option(displayName = "Ordered property insertion",
66+
description = "Whether to attempt adding the property in an order following alphabetic sorting. The default value is `true`.",
67+
required = false,
68+
example = "false"
69+
)
70+
@Nullable
71+
Boolean orderedInsertion;
72+
6573
@Override
6674
public String getDisplayName() {
6775
return "Add a new property";
@@ -99,7 +107,7 @@ public Properties.File visitFile(Properties.File file, ExecutionContext ctx) {
99107
Properties.Entry.Delimiter delimitedBy = StringUtils.isNotEmpty(delimiter) ? Properties.Entry.Delimiter.getDelimiter(delimiter) : Properties.Entry.Delimiter.EQUALS;
100108
String beforeEquals = delimitedBy == Properties.Entry.Delimiter.NONE ? delimiter : "";
101109
Properties.Entry entry = new Properties.Entry(randomId(), "\n", Markers.EMPTY, property, beforeEquals, delimitedBy, propertyValue);
102-
int insertionIndex = sortedInsertionIndex(entry, p.getContent());
110+
103111

104112
List<Properties.Content> newContents;
105113
if(StringUtils.isBlank(comment)) {
@@ -116,9 +124,17 @@ public Properties.File visitFile(Properties.File file, ExecutionContext ctx) {
116124
}
117125

118126
List<Properties.Content> contentList = new ArrayList<>(p.getContent().size() + 1);
119-
contentList.addAll(p.getContent().subList(0, insertionIndex));
120-
contentList.addAll(newContents);
121-
contentList.addAll(p.getContent().subList(insertionIndex, p.getContent().size()));
127+
if (orderedInsertion == null || orderedInsertion) {
128+
int insertionIndex = sortedInsertionIndex(entry, p.getContent());
129+
contentList.addAll(p.getContent().subList(0, insertionIndex));
130+
contentList.addAll(newContents);
131+
contentList.addAll(p.getContent().subList(insertionIndex, p.getContent().size()));
132+
}
133+
else {
134+
contentList.addAll(p.getContent());
135+
contentList.addAll(newContents);
136+
}
137+
122138

123139
// First entry in the file does not need a newline, but every other entry does
124140
contentList = ListUtils.map(contentList, (i, c) -> {

rewrite-properties/src/test/java/org/openrewrite/properties/AddPropertyTest.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void newProperty() {
3535
"management.metrics.enable.process.files",
3636
"true",
3737
null,
38+
null,
3839
null
3940
)),
4041
properties(
@@ -56,6 +57,7 @@ void emptyProperty() {
5657
"",
5758
"true",
5859
null,
60+
null,
5961
null
6062
)),
6163
properties(
@@ -73,6 +75,7 @@ void emptyValue() {
7375
"management.metrics.enable.process.files",
7476
"",
7577
null,
78+
null,
7679
null
7780
)),
7881
properties(
@@ -90,6 +93,7 @@ void containsProperty() {
9093
"management.metrics.enable.process.files",
9194
"true",
9295
null,
96+
null,
9397
null
9498
)),
9599
properties(
@@ -108,7 +112,8 @@ void delimitedByColon() {
108112
"management.metrics.enable.process.files",
109113
"true",
110114
null,
111-
":"
115+
":",
116+
null
112117
)),
113118
properties(
114119
"""
@@ -130,7 +135,8 @@ void delimitedByWhitespace() {
130135
"management.metrics.enable.process.files",
131136
"true",
132137
null,
133-
" "
138+
" ",
139+
null
134140
)),
135141
properties(
136142
"""
@@ -151,6 +157,7 @@ void addToEmptyFile() {
151157
"management.metrics.enable.process.files",
152158
"true",
153159
null,
160+
null,
154161
null
155162
)),
156163
properties(
@@ -169,6 +176,7 @@ void addCommentedPropertyToEmptyFile() {
169176
"management.metrics.enable.process.files",
170177
"true",
171178
"Management metrics",
179+
null,
172180
null
173181
)),
174182
properties(
@@ -188,6 +196,7 @@ void addCommentedPropertyToExistingFile() {
188196
"management.metrics.enable.process.files",
189197
"true",
190198
"Management metrics",
199+
null,
191200
null
192201
)),
193202
properties(
@@ -208,6 +217,7 @@ void keepPropertyValueWithLineContinuations() {
208217
"management.metrics.enable.process.files",
209218
"true",
210219
null,
220+
null,
211221
null
212222
)),
213223
properties(
@@ -236,6 +246,7 @@ void orderedInsertionBeginning() {
236246
"com.sam",
237247
"true",
238248
"sam",
249+
null,
239250
null
240251
)),
241252
properties(
@@ -258,6 +269,7 @@ void orderedInsertionMiddle() {
258269
"com.sam",
259270
"true",
260271
"sam",
272+
null,
261273
null
262274
)),
263275
properties(
@@ -294,6 +306,7 @@ void orderedInsertionEnd() {
294306
"com.sam",
295307
"true",
296308
"sam",
309+
null,
297310
null
298311
)),
299312
properties(
@@ -308,4 +321,42 @@ void orderedInsertionEnd() {
308321
)
309322
);
310323
}
324+
325+
@Test
326+
void unorderedInsertion() {
327+
rewriteRun(
328+
spec -> spec.recipe(new AddProperty(
329+
"com.sam",
330+
"true",
331+
"sam",
332+
null,
333+
false
334+
)),
335+
properties(
336+
"""
337+
# amy
338+
com.amy=true
339+
# bea
340+
com.bea=true
341+
# seb
342+
com.seb=true
343+
# zoe
344+
com.zoe=true
345+
""",
346+
"""
347+
# amy
348+
com.amy=true
349+
# bea
350+
com.bea=true
351+
# seb
352+
com.seb=true
353+
# zoe
354+
com.zoe=true
355+
# sam
356+
com.sam=true
357+
"""
358+
)
359+
);
360+
}
361+
311362
}

0 commit comments

Comments
 (0)