Skip to content

Commit 6794a62

Browse files
author
Killian Perlin
committed
Refactoring: make usage of Java Streams
1 parent 16dc654 commit 6794a62

File tree

2 files changed

+32
-61
lines changed

2 files changed

+32
-61
lines changed

lkql_jit/cli/src/main/java/com/adacore/lkql_jit/cli/LKQLRefactor.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,23 @@ private enum RefactoringKind {
6767
public Refactoring getRefactoring(AnalysisUnit unit) {
6868
return switch (refactoring) {
6969
case IS_TO_COLON -> (Refactoring.State state) -> {
70-
for (var det : Refactoring.findAll(unit.getRoot(), n ->
71-
n instanceof NodePatternDetail
72-
)) {
73-
var tokIs = Refactoring.firstWithPred(
74-
det.tokenStart(),
75-
t -> t.kind == TokenKind.LKQL_IS
76-
);
70+
Refactoring.stream(state.unit.getRoot())
71+
.filter(n -> n instanceof NodePatternDetail)
72+
.forEach(det -> {
73+
Refactoring.streamFrom(det.tokenStart())
74+
.filter(t -> t.kind == TokenKind.LKQL_IS)
75+
.findFirst()
76+
.ifPresent(tokIs -> {
77+
// Replace "is" -> ":"
78+
state.replace(tokIs, ":");
7779

78-
if (!tokIs.isNone()) {
79-
// Replace "is" -> ":"
80-
state.replace(tokIs, ":");
81-
82-
// Get rid of previous token if it is a whitespace
83-
var prev = tokIs.previous();
84-
if (Refactoring.isWhitespace(prev)) {
85-
state.delete(tokIs.previous());
86-
}
87-
}
88-
}
80+
// Get rid of previous token if it is a whitespace
81+
var prev = tokIs.previous();
82+
if (Refactoring.isWhitespace(prev)) {
83+
state.delete(prev);
84+
}
85+
});
86+
});
8987
};
9088
case TO_LKQL_V2 -> new LKQLToLkt();
9189
};

lkql_jit/options/src/main/java/com/adacore/lkql_jit/options/Refactorings/Refactoring.java

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import java.util.HashMap;
1414
import java.util.List;
1515
import java.util.function.Consumer;
16-
import java.util.function.Predicate;
16+
import java.util.stream.Stream;
1717

1818
public interface Refactoring {
1919
class State {
@@ -173,57 +173,30 @@ public static boolean isWhitespace(Liblkqllang.Token token) {
173173
}
174174

175175
/**
176-
* Helper for refactor writers: Returns the first token that satisfies the predicate 'pred',
177-
* iterating on tokens from 'fromTok' (including 'fromTok').
178-
*
179-
* <p>If no token is found, return the null token.
180-
*/
181-
public static Liblkqllang.Token firstWithPred(
182-
Liblkqllang.Token fromTok,
183-
Predicate<Liblkqllang.Token> pred
184-
) {
185-
var curTok = fromTok;
186-
while (!curTok.isNone() && !pred.test(curTok)) {
187-
curTok = curTok.next();
188-
}
189-
return curTok;
190-
}
191-
192-
/**
193-
* Helper for findAll. Visit all children of 'node', calling 'cons' on each of them. TODO: Hoist
194-
* in Java bindings (see eng/libadalang/langkit#859)
176+
* Internal helper for stream method.
177+
* Visit all children of 'node', calling 'cons' on each of them.
178+
* TODO: Hoist in Java bindings (see eng/libadalang/langkit#859)
195179
*/
196180
private static void visitChildren(
197181
Liblkqllang.LkqlNode node,
198182
Consumer<Liblkqllang.LkqlNode> cons
199183
) {
200-
if (node == null || node.isNone()) {
201-
return;
184+
if (node != null && !node.isNone()) {
185+
cons.accept(node);
186+
for (var child : node.children()) visitChildren(child, cons);
202187
}
188+
}
203189

204-
for (var c : node.children()) {
205-
if (c != null && !c.isNone()) {
206-
cons.accept(c);
207-
visitChildren(c, cons);
208-
}
209-
}
190+
/** Creates a node stream visiting all children of the input node. */
191+
public static Stream<Liblkqllang.LkqlNode> stream(Liblkqllang.LkqlNode root) {
192+
final var b = Stream.<Liblkqllang.LkqlNode>builder();
193+
visitChildren(root, b);
194+
return b.build();
210195
}
211196

212-
/**
213-
* Helper for refactor writers: Find all nodes that are children of root and which satisfies the
214-
* predicate 'pred'. TODO: Hoist in Java bindings (see eng/libadalang/langkit#859)
215-
*/
216-
static List<Liblkqllang.LkqlNode> findAll(
217-
Liblkqllang.LkqlNode root,
218-
Predicate<Liblkqllang.LkqlNode> pred
219-
) {
220-
var result = new ArrayList<Liblkqllang.LkqlNode>();
221-
visitChildren(root, c -> {
222-
if (pred.test(c)) {
223-
result.add(c);
224-
}
225-
});
226-
return result;
197+
/** Creates a token stream starting at the input token. */
198+
public static Stream<Liblkqllang.Token> streamFrom(Liblkqllang.Token start) {
199+
return Stream.iterate(start, t -> !t.isNone(), t -> t.next());
227200
}
228201

229202
void applyRefactor(State state);

0 commit comments

Comments
 (0)