|
29 | 29 |
|
30 | 30 | package org.scijava.search.module; |
31 | 31 |
|
| 32 | +import java.util.LinkedHashSet; |
32 | 33 | import java.util.List; |
33 | 34 | import java.util.stream.Collectors; |
34 | 35 |
|
@@ -69,19 +70,29 @@ public String title() { |
69 | 70 | public List<SearchResult> search(final String text, final boolean fuzzy) { |
70 | 71 | final String baseDir = // |
71 | 72 | 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() // |
74 | 90 | .map(info -> new ModuleSearchResult(info, baseDir)) // |
75 | 91 | .collect(Collectors.toList()); |
76 | 92 | } |
77 | 93 |
|
78 | 94 | // -- Helper methods -- |
79 | 95 |
|
80 | | - private boolean matches(final ModuleInfo info, final String text) { |
81 | | - return matches(info.getMenuPath().toString(), text) || |
82 | | - matches(info.getTitle(), text); |
83 | | - } |
84 | | - |
85 | 96 | private boolean matches(final String actual, final String desired) { |
86 | 97 | // TODO: Implement fuzzy matching option, and maybe case sensitive option. |
87 | 98 | // Probably put it in the SearchService itself, and make an API toggle. |
|
0 commit comments