Skip to content

Commit 4536e62

Browse files
committed
ModuleSearcher: put matching titles before menus
1 parent 300911e commit 4536e62

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/main/java/org/scijava/search/module/ModuleSearcher.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
package org.scijava.search.module;
3131

32+
import java.util.LinkedHashSet;
3233
import java.util.List;
3334
import java.util.stream.Collectors;
3435

@@ -69,19 +70,29 @@ public String title() {
6970
public List<SearchResult> search(final String text, final boolean fuzzy) {
7071
final String baseDir = //
7172
appService.getApp().getBaseDirectory().getAbsolutePath();
72-
return moduleService.getModules().stream() //
73-
.filter(info -> matches(info, text)) //
73+
74+
final LinkedHashSet<ModuleInfo> matches = new LinkedHashSet<>();
75+
76+
final List<ModuleInfo> modules = moduleService.getModules();
77+
78+
// Add modules with matching titles first.
79+
modules.stream() //
80+
.filter(info -> matches(info.getTitle(), text)) //
81+
.forEach(matches::add);
82+
83+
// Add modules with matching menu paths after that.
84+
modules.stream() //
85+
.filter(info -> matches(info.getMenuPath().toString(), text)) //
86+
.forEach(matches::add);
87+
88+
// Wrap each matching ModuleInfo in a ModuleSearchResult.
89+
return matches.stream() //
7490
.map(info -> new ModuleSearchResult(info, baseDir)) //
7591
.collect(Collectors.toList());
7692
}
7793

7894
// -- Helper methods --
7995

80-
private boolean matches(final ModuleInfo info, final String text) {
81-
return matches(info.getMenuPath().toString(), text) ||
82-
matches(info.getTitle(), text);
83-
}
84-
8596
private boolean matches(final String actual, final String desired) {
8697
// TODO: Implement fuzzy matching option, and maybe case sensitive option.
8798
// Probably put it in the SearchService itself, and make an API toggle.

0 commit comments

Comments
 (0)