|
17 | 17 | package org.openrewrite.staticanalysis; |
18 | 18 |
|
19 | 19 | import java.time.Duration; |
20 | | -import java.util.ArrayList; |
21 | 20 | import java.util.Collections; |
22 | 21 | import java.util.List; |
23 | 22 | import java.util.Set; |
24 | 23 |
|
25 | 24 | import org.openrewrite.ExecutionContext; |
26 | 25 | import org.openrewrite.Recipe; |
27 | 26 | import org.openrewrite.TreeVisitor; |
| 27 | +import org.openrewrite.internal.ListUtils; |
28 | 28 | import org.openrewrite.java.JavaIsoVisitor; |
29 | 29 | import org.openrewrite.java.tree.Comment; |
30 | 30 | import org.openrewrite.java.tree.Space; |
@@ -60,53 +60,37 @@ public TreeVisitor<?, ExecutionContext> getVisitor() { |
60 | 60 | public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) { |
61 | 61 | Space s = super.visitSpace(space, loc, ctx); |
62 | 62 |
|
63 | | - List<Comment> comments = s.getComments(); |
64 | | - if (comments.isEmpty()) { |
| 63 | + if (s.getComments().isEmpty()) { |
65 | 64 | return s; |
66 | 65 | } |
67 | 66 |
|
68 | | - boolean changed = false; |
69 | | - List<Comment> updatedComments = new ArrayList<>(comments.size()); |
70 | | - |
71 | | - for (Comment comment : comments) { |
72 | | - Comment updated = comment; |
73 | | - |
| 67 | + List<Comment> updatedComments = ListUtils.map(s.getComments(), comment -> { |
74 | 68 | if (comment instanceof TextComment) { |
75 | 69 | TextComment tc = (TextComment) comment; |
76 | 70 | String text = tc.getText(); |
77 | 71 |
|
78 | 72 | // Skip empty comments |
79 | 73 | if (text.isEmpty()) { |
80 | | - updatedComments.add(comment); |
81 | | - continue; |
| 74 | + return comment; |
82 | 75 | } |
83 | 76 |
|
84 | 77 | // Skip special comments like //language=java |
85 | 78 | if (text.startsWith("language=")) { |
86 | | - updatedComments.add(comment); |
87 | | - continue; |
| 79 | + return comment; |
88 | 80 | } |
89 | 81 |
|
90 | | - // If already has space after // |
| 82 | + // Already has space after // |
91 | 83 | if (text.startsWith(" ")) { |
92 | | - updatedComments.add(comment); |
93 | | - continue; |
| 84 | + return comment; |
94 | 85 | } |
95 | 86 |
|
96 | | - // Add space after // |
97 | | - String newText = " " + text; |
98 | | - |
99 | | - if (!newText.equals(text)) { |
100 | | - updated = tc.withText(newText); |
101 | | - changed = true; |
102 | | - } |
| 87 | + // Add space |
| 88 | + return tc.withText(" " + text); |
103 | 89 | } |
| 90 | + return comment; |
| 91 | + }); |
104 | 92 |
|
105 | | - updatedComments.add(updated); |
106 | | - } |
107 | | - |
108 | | - // Only return new object if something changed (important for single-cycle rule) |
109 | | - return changed ? s.withComments(updatedComments) : s; |
| 93 | + return s.withComments(updatedComments); |
110 | 94 | } |
111 | 95 | }; |
112 | 96 | } |
|
0 commit comments