Skip to content

Commit

Permalink
Allow search feedback to filter results from the final output
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Mar 15, 2024
1 parent 08c17bc commit 5246f53
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import jakarta.annotation.Nonnull;
import software.coley.recaf.info.ClassInfo;
import software.coley.recaf.info.FileInfo;
import software.coley.recaf.services.search.result.Result;
import software.coley.recaf.services.search.result.Results;

/**
* Outline of search feedback capabilities. Allows for:
Expand Down Expand Up @@ -49,4 +51,15 @@ default boolean doVisitClass(@Nonnull ClassInfo cls) {
default boolean doVisitFile(@Nonnull FileInfo file) {
return true;
}

/**
* @param result
* Result to consider.
*
* @return {@code true} to accept the result into the final {@link Results} collection.
* {@code false} to drop it.
*/
default boolean doAcceptResult(@Nonnull Result<?> result) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package software.coley.recaf.services.search;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import software.coley.recaf.info.AndroidClassInfo;
Expand Down Expand Up @@ -131,7 +132,7 @@ public Results search(@Nonnull Workspace workspace, @Nonnull List<Query> queries
service.submit(() -> {
if (feedback.hasRequestedStop())
return;
androidClassVisitor.visit((path, value) -> results.add(createResult(path, value)), classPath, classInfo);
androidClassVisitor.visit(getResultSink(results, feedback), classPath, classInfo);
});
}
}
Expand All @@ -150,7 +151,7 @@ public Results search(@Nonnull Workspace workspace, @Nonnull List<Query> queries
service.submit(() -> {
if (feedback.hasRequestedStop())
return;
jvmClassVisitor.visit((path, value) -> results.add(createResult(path, value)), classPath, classInfo);
jvmClassVisitor.visit(getResultSink(results, feedback), classPath, classInfo);
});
}
});
Expand All @@ -169,7 +170,7 @@ public Results search(@Nonnull Workspace workspace, @Nonnull List<Query> queries
service.submit(() -> {
if (feedback.hasRequestedStop())
return;
fileVisitor.visit((path, value) -> results.add(createResult(path, value)), filePath, fileInfo);
fileVisitor.visit(getResultSink(results, feedback), filePath, fileInfo);
});
}
}
Expand All @@ -179,6 +180,15 @@ public Results search(@Nonnull Workspace workspace, @Nonnull List<Query> queries
return results;
}

@Nonnull
private static ResultSink getResultSink(@Nonnull Results results, @Nullable SearchFeedback feedback) {
return (path, value) -> {
Result<?> result = createResult(path, value);
if (feedback == null || feedback.doAcceptResult(result))
results.add(result);
};
}

@Nonnull
private static Result<?> createResult(@Nonnull PathNode<?> path, @Nonnull Object value) {
if (value instanceof Number)
Expand Down

0 comments on commit 5246f53

Please sign in to comment.