29
29
30
30
import edu .umd .cs .findbugs .annotations .CheckForNull ;
31
31
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
32
+ import hudson .ExtensionComponent ;
33
+ import hudson .ExtensionList ;
32
34
import hudson .Util ;
33
35
import hudson .util .EditDistance ;
34
36
import io .jenkins .servlet .ServletExceptionWrapper ;
37
39
import java .util .AbstractList ;
38
40
import java .util .ArrayList ;
39
41
import java .util .Collections ;
42
+ import java .util .Comparator ;
40
43
import java .util .HashSet ;
41
44
import java .util .List ;
45
+ import java .util .Map ;
42
46
import java .util .Set ;
43
47
import java .util .logging .Level ;
44
48
import java .util .logging .Logger ;
49
+ import java .util .stream .Collectors ;
45
50
import jenkins .model .Jenkins ;
51
+ import jenkins .search .SearchGroup ;
46
52
import jenkins .security .stapler .StaplerNotDispatchable ;
47
53
import jenkins .util .MemoryReductionUtil ;
48
54
import jenkins .util .SystemProperties ;
@@ -171,11 +177,26 @@ public void doSuggest(StaplerRequest2 req, StaplerResponse2 rsp, @QueryParameter
171
177
172
178
if (iconName .startsWith ("symbol" )) {
173
179
r .suggestions .add (new Item (curItem .getPath (), curItem .getUrl (),
174
- Symbol .get (new SymbolRequest .Builder ().withRaw (iconName ).build ())));
180
+ Symbol .get (new SymbolRequest .Builder ().withRaw (iconName ).build ()), "symbol" , curItem . item . getSearchGroup (). getDisplayName () ));
175
181
} else {
176
- r .suggestions .add (new Item (curItem .getPath (), curItem .getUrl (), iconName , "image" ));
182
+ r .suggestions .add (new Item (curItem .getPath (), curItem .getUrl (), iconName , "image" , curItem . item . getSearchGroup (). getDisplayName () ));
177
183
}
178
184
}
185
+
186
+ // Sort results by group
187
+ ExtensionList <SearchGroup > groupsExtensionList = ExtensionList .lookup (SearchGroup .class );
188
+ List <ExtensionComponent <SearchGroup >> components = groupsExtensionList .getComponents ();
189
+ Map <String , Double > searchGroupOrdinal = components .stream ()
190
+ .collect (Collectors .toMap (
191
+ (k ) -> k .getInstance ().getDisplayName (),
192
+ ExtensionComponent ::ordinal
193
+ ));
194
+ r .suggestions .sort (
195
+ Comparator .comparingDouble ((Item item ) -> searchGroupOrdinal .getOrDefault (item .getGroup (), Double .MAX_VALUE ))
196
+ .reversed ()
197
+ .thenComparing (item -> item .name )
198
+ );
199
+
179
200
rsp .serveExposedBean (req , r , new ExportConfig ());
180
201
}
181
202
@@ -270,7 +291,6 @@ public static class Result {
270
291
public static class Item {
271
292
272
293
@ Exported
273
- @ SuppressFBWarnings (value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD" , justification = "read by Stapler" )
274
294
public String name ;
275
295
276
296
private final String url ;
@@ -279,22 +299,19 @@ public static class Item {
279
299
280
300
private final String icon ;
281
301
302
+ private final String group ;
303
+
282
304
public Item (String name ) {
283
- this (name , null , null );
305
+ this (name , null , null , "symbol" , null );
284
306
}
285
307
286
- public Item (String name , String url , String icon ) {
308
+ public Item (String name , String url , String icon , String type , String group ) {
287
309
this .name = name ;
288
310
this .url = url ;
289
311
this .icon = icon ;
290
- this .type = "symbol" ;
291
- }
292
-
293
- public Item (String name , String url , String icon , String type ) {
294
312
this .name = name ;
295
- this .url = url ;
296
- this .icon = icon ;
297
313
this .type = type ;
314
+ this .group = group ;
298
315
}
299
316
300
317
@ Exported
@@ -311,6 +328,11 @@ public String getIcon() {
311
328
public String getType () {
312
329
return type ;
313
330
}
331
+
332
+ @ Exported
333
+ public String getGroup () {
334
+ return group ;
335
+ }
314
336
}
315
337
316
338
private enum Mode {
0 commit comments