-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
main.js
2966 lines (2710 loc) · 117 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Local js definitions:
/* global addClass, getSettingValue, hasClass */
/* global onEach, onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */
/* global hideThemeButtonState, showThemeButtonState */
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(suffix, length) {
var l = length || this.length;
return this.indexOf(suffix, l - suffix.length) !== -1;
};
}
if (!DOMTokenList.prototype.add) {
DOMTokenList.prototype.add = function(className) {
if (className && !hasClass(this, className)) {
if (this.className && this.className.length > 0) {
this.className += " " + className;
} else {
this.className = className;
}
}
};
}
if (!DOMTokenList.prototype.remove) {
DOMTokenList.prototype.remove = function(className) {
if (className && this.className) {
this.className = (" " + this.className + " ").replace(" " + className + " ", " ")
.trim();
}
};
}
(function () {
var rustdocVars = document.getElementById("rustdoc-vars");
if (rustdocVars) {
window.rootPath = rustdocVars.attributes["data-root-path"].value;
window.currentCrate = rustdocVars.attributes["data-current-crate"].value;
}
var sidebarVars = document.getElementById("sidebar-vars");
if (sidebarVars) {
window.sidebarCurrent = {
name: sidebarVars.attributes["data-name"].value,
ty: sidebarVars.attributes["data-ty"].value,
relpath: sidebarVars.attributes["data-relpath"].value,
};
}
}());
// Gets the human-readable string for the virtual-key code of the
// given KeyboardEvent, ev.
//
// This function is meant as a polyfill for KeyboardEvent#key,
// since it is not supported in IE 11 or Chrome for Android. We also test for
// KeyboardEvent#keyCode because the handleShortcut handler is
// also registered for the keydown event, because Blink doesn't fire
// keypress on hitting the Escape key.
//
// So I guess you could say things are getting pretty interoperable.
function getVirtualKey(ev) {
if ("key" in ev && typeof ev.key != "undefined") {
return ev.key;
}
var c = ev.charCode || ev.keyCode;
if (c == 27) {
return "Escape";
}
return String.fromCharCode(c);
}
function getSearchInput() {
return document.getElementsByClassName("search-input")[0];
}
function getSearchElement() {
return document.getElementById("search");
}
function getThemesElement() {
return document.getElementById("theme-choices");
}
function getThemePickerElement() {
return document.getElementById("theme-picker");
}
// Returns the current URL without any query parameter or hash.
function getNakedUrl() {
return window.location.href.split("?")[0].split("#")[0];
}
// Sets the focus on the search bar at the top of the page
function focusSearchBar() {
getSearchInput().focus();
}
// Removes the focus from the search bar.
function defocusSearchBar() {
getSearchInput().blur();
}
(function() {
"use strict";
// This mapping table should match the discriminants of
// `rustdoc::html::item_type::ItemType` type in Rust.
var itemTypes = ["mod",
"externcrate",
"import",
"struct",
"enum",
"fn",
"type",
"static",
"trait",
"impl",
"tymethod",
"method",
"structfield",
"variant",
"macro",
"primitive",
"associatedtype",
"constant",
"associatedconstant",
"union",
"foreigntype",
"keyword",
"existential",
"attr",
"derive",
"traitalias"];
var disableShortcuts = getSettingValue("disable-shortcuts") === "true";
var search_input = getSearchInput();
var searchTimeout = null;
var toggleAllDocsId = "toggle-all-docs";
// On the search screen, so you remain on the last tab you opened.
//
// 0 for "In Names"
// 1 for "In Parameters"
// 2 for "In Return Types"
var currentTab = 0;
var mouseMovedAfterSearch = true;
var titleBeforeSearch = document.title;
var searchTitle = null;
function clearInputTimeout() {
if (searchTimeout !== null) {
clearTimeout(searchTimeout);
searchTimeout = null;
}
}
function getPageId() {
if (window.location.hash) {
var tmp = window.location.hash.replace(/^#/, "");
if (tmp.length > 0) {
return tmp;
}
}
return null;
}
function showSidebar() {
var elems = document.getElementsByClassName("sidebar-elems")[0];
if (elems) {
addClass(elems, "show-it");
}
var sidebar = document.getElementsByClassName("sidebar")[0];
if (sidebar) {
addClass(sidebar, "mobile");
var filler = document.getElementById("sidebar-filler");
if (!filler) {
var div = document.createElement("div");
div.id = "sidebar-filler";
sidebar.appendChild(div);
}
}
}
function hideSidebar() {
var elems = document.getElementsByClassName("sidebar-elems")[0];
if (elems) {
removeClass(elems, "show-it");
}
var sidebar = document.getElementsByClassName("sidebar")[0];
removeClass(sidebar, "mobile");
var filler = document.getElementById("sidebar-filler");
if (filler) {
filler.remove();
}
document.getElementsByTagName("body")[0].style.marginTop = "";
}
function showSearchResults(search) {
if (search === null || typeof search === 'undefined') {
search = getSearchElement();
}
addClass(main, "hidden");
removeClass(search, "hidden");
mouseMovedAfterSearch = false;
document.title = searchTitle;
}
function hideSearchResults(search) {
if (search === null || typeof search === 'undefined') {
search = getSearchElement();
}
addClass(search, "hidden");
removeClass(main, "hidden");
document.title = titleBeforeSearch;
// We also remove the query parameter from the URL.
if (browserSupportsHistoryApi()) {
history.replaceState("", window.currentCrate + " - Rust",
getNakedUrl() + window.location.hash);
}
}
// used for special search precedence
var TY_PRIMITIVE = itemTypes.indexOf("primitive");
var TY_KEYWORD = itemTypes.indexOf("keyword");
function getQueryStringParams() {
var params = {};
window.location.search.substring(1).split("&").
map(function(s) {
var pair = s.split("=");
params[decodeURIComponent(pair[0])] =
typeof pair[1] === "undefined" ? null : decodeURIComponent(pair[1]);
});
return params;
}
function browserSupportsHistoryApi() {
return window.history && typeof window.history.pushState === "function";
}
function isHidden(elem) {
return elem.offsetHeight === 0;
}
var main = document.getElementById("main");
var savedHash = "";
function handleHashes(ev) {
var elem;
var search = getSearchElement();
if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
// This block occurs when clicking on an element in the navbar while
// in a search.
hideSearchResults(search);
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
if (browserSupportsHistoryApi()) {
// `window.location.search`` contains all the query parameters, not just `search`.
history.replaceState(hash, "",
getNakedUrl() + window.location.search + "#" + hash);
}
elem = document.getElementById(hash);
if (elem) {
elem.scrollIntoView();
}
}
// This part is used in case an element is not visible.
if (savedHash !== window.location.hash) {
savedHash = window.location.hash;
if (savedHash.length === 0) {
return;
}
elem = document.getElementById(savedHash.slice(1)); // we remove the '#'
if (!elem || !isHidden(elem)) {
return;
}
var parent = elem.parentNode;
if (parent && hasClass(parent, "impl-items")) {
// In case this is a trait implementation item, we first need to toggle
// the "Show hidden undocumented items".
onEachLazy(parent.getElementsByClassName("collapsed"), function(e) {
if (e.parentNode === parent) {
// Only click on the toggle we're looking for.
e.click();
return true;
}
});
if (isHidden(elem)) {
// The whole parent is collapsed. We need to click on its toggle as well!
if (hasClass(parent.lastElementChild, "collapse-toggle")) {
parent.lastElementChild.click();
}
}
}
}
}
function highlightSourceLines(match, ev) {
if (typeof match === "undefined") {
// If we're in mobile mode, we should hide the sidebar in any case.
hideSidebar();
match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
}
if (!match) {
return;
}
var from = parseInt(match[1], 10);
var to = from;
if (typeof match[2] !== "undefined") {
to = parseInt(match[2], 10);
}
if (to < from) {
var tmp = to;
to = from;
from = tmp;
}
var elem = document.getElementById(from);
if (!elem) {
return;
}
if (!ev) {
var x = document.getElementById(from);
if (x) {
x.scrollIntoView();
}
}
onEachLazy(document.getElementsByClassName("line-numbers"), function(e) {
onEachLazy(e.getElementsByTagName("span"), function(i_e) {
removeClass(i_e, "line-highlighted");
});
});
for (var i = from; i <= to; ++i) {
elem = document.getElementById(i);
if (!elem) {
break;
}
addClass(elem, "line-highlighted");
}
}
function onHashChange(ev) {
// If we're in mobile mode, we should hide the sidebar in any case.
hideSidebar();
var match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
if (match) {
return highlightSourceLines(match, ev);
}
handleHashes(ev);
}
function expandSection(id) {
var elem = document.getElementById(id);
if (elem && isHidden(elem)) {
var h3 = elem.parentNode.previousElementSibling;
if (h3 && h3.tagName !== "H3") {
h3 = h3.previousElementSibling; // skip div.docblock
}
if (h3) {
var collapses = h3.getElementsByClassName("collapse-toggle");
if (collapses.length > 0) {
// The element is not visible, we need to make it appear!
collapseDocs(collapses[0], "show");
}
}
}
}
function getHelpElement() {
buildHelperPopup();
return document.getElementById("help");
}
function displayHelp(display, ev, help) {
help = help ? help : getHelpElement();
if (display === true) {
if (hasClass(help, "hidden")) {
ev.preventDefault();
removeClass(help, "hidden");
addClass(document.body, "blur");
}
} else if (hasClass(help, "hidden") === false) {
ev.preventDefault();
addClass(help, "hidden");
removeClass(document.body, "blur");
}
}
function handleEscape(ev) {
var help = getHelpElement();
var search = getSearchElement();
if (hasClass(help, "hidden") === false) {
displayHelp(false, ev, help);
} else if (hasClass(search, "hidden") === false) {
clearInputTimeout();
ev.preventDefault();
hideSearchResults(search);
}
defocusSearchBar();
hideThemeButtonState();
}
function handleShortcut(ev) {
// Don't interfere with browser shortcuts
if (ev.ctrlKey || ev.altKey || ev.metaKey || disableShortcuts === true) {
return;
}
if (document.activeElement.tagName === "INPUT") {
switch (getVirtualKey(ev)) {
case "Escape":
handleEscape(ev);
break;
}
} else {
switch (getVirtualKey(ev)) {
case "Escape":
handleEscape(ev);
break;
case "s":
case "S":
displayHelp(false, ev);
ev.preventDefault();
focusSearchBar();
break;
case "+":
case "-":
ev.preventDefault();
toggleAllDocs();
break;
case "?":
displayHelp(true, ev);
break;
case "t":
case "T":
displayHelp(false, ev);
ev.preventDefault();
var themePicker = getThemePickerElement();
themePicker.click();
themePicker.focus();
break;
default:
var themePicker = getThemePickerElement();
if (themePicker.parentNode.contains(ev.target)) {
handleThemeKeyDown(ev);
}
}
}
}
function handleThemeKeyDown(ev) {
var active = document.activeElement;
var themes = getThemesElement();
switch (getVirtualKey(ev)) {
case "ArrowUp":
ev.preventDefault();
if (active.previousElementSibling && ev.target.id !== "theme-picker") {
active.previousElementSibling.focus();
} else {
showThemeButtonState();
themes.lastElementChild.focus();
}
break;
case "ArrowDown":
ev.preventDefault();
if (active.nextElementSibling && ev.target.id !== "theme-picker") {
active.nextElementSibling.focus();
} else {
showThemeButtonState();
themes.firstElementChild.focus();
}
break;
case "Enter":
case "Return":
case "Space":
if (ev.target.id === "theme-picker" && themes.style.display === "none") {
ev.preventDefault();
showThemeButtonState();
themes.firstElementChild.focus();
}
break;
case "Home":
ev.preventDefault();
themes.firstElementChild.focus();
break;
case "End":
ev.preventDefault();
themes.lastElementChild.focus();
break;
// The escape key is handled in handleEscape, not here,
// so that pressing escape will close the menu even if it isn't focused
}
}
function findParentElement(elem, tagName) {
do {
if (elem && elem.tagName === tagName) {
return elem;
}
elem = elem.parentNode;
} while (elem);
return null;
}
document.addEventListener("keypress", handleShortcut);
document.addEventListener("keydown", handleShortcut);
document.addEventListener("mousemove", function() { mouseMovedAfterSearch = true; });
var handleSourceHighlight = (function() {
var prev_line_id = 0;
var set_fragment = function(name) {
var x = window.scrollX,
y = window.scrollY;
if (browserSupportsHistoryApi()) {
history.replaceState(null, null, "#" + name);
highlightSourceLines();
} else {
location.replace("#" + name);
}
// Prevent jumps when selecting one or many lines
window.scrollTo(x, y);
};
return function(ev) {
var cur_line_id = parseInt(ev.target.id, 10);
ev.preventDefault();
if (ev.shiftKey && prev_line_id) {
// Swap selection if needed
if (prev_line_id > cur_line_id) {
var tmp = prev_line_id;
prev_line_id = cur_line_id;
cur_line_id = tmp;
}
set_fragment(prev_line_id + "-" + cur_line_id);
} else {
prev_line_id = cur_line_id;
set_fragment(cur_line_id);
}
};
}());
document.addEventListener("click", function(ev) {
if (hasClass(ev.target, "help-button")) {
displayHelp(true, ev);
} else if (hasClass(ev.target, "collapse-toggle")) {
collapseDocs(ev.target, "toggle");
} else if (hasClass(ev.target.parentNode, "collapse-toggle")) {
collapseDocs(ev.target.parentNode, "toggle");
} else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) {
handleSourceHighlight(ev);
} else if (hasClass(getHelpElement(), "hidden") === false) {
var help = getHelpElement();
var is_inside_help_popup = ev.target !== help && help.contains(ev.target);
if (is_inside_help_popup === false) {
addClass(help, "hidden");
removeClass(document.body, "blur");
}
} else {
// Making a collapsed element visible on onhashchange seems
// too late
var a = findParentElement(ev.target, "A");
if (a && a.hash) {
expandSection(a.hash.replace(/^#/, ""));
}
}
});
(function() {
var x = document.getElementsByClassName("version-selector");
if (x.length > 0) {
x[0].onchange = function() {
var i, match,
url = document.location.href,
stripped = "",
len = window.rootPath.match(/\.\.\//g).length + 1;
for (i = 0; i < len; ++i) {
match = url.match(/\/[^\/]*$/);
if (i < len - 1) {
stripped = match[0] + stripped;
}
url = url.substring(0, url.length - match[0].length);
}
var selectedVersion = document.getElementsByClassName("version-selector")[0].value;
url += "/" + selectedVersion + stripped;
document.location.href = url;
};
}
}());
/**
* A function to compute the Levenshtein distance between two strings
* Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported
* Full License can be found at http://creativecommons.org/licenses/by-sa/3.0/legalcode
* This code is an unmodified version of the code written by Marco de Wit
* and was found at http://stackoverflow.com/a/18514751/745719
*/
var levenshtein_row2 = [];
function levenshtein(s1, s2) {
if (s1 === s2) {
return 0;
}
var s1_len = s1.length, s2_len = s2.length;
if (s1_len && s2_len) {
var i1 = 0, i2 = 0, a, b, c, c2, row = levenshtein_row2;
while (i1 < s1_len) {
row[i1] = ++i1;
}
while (i2 < s2_len) {
c2 = s2.charCodeAt(i2);
a = i2;
++i2;
b = i2;
for (i1 = 0; i1 < s1_len; ++i1) {
c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
a = row[i1];
b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
row[i1] = b;
}
}
return b;
}
return s1_len + s2_len;
}
window.initSearch = function(rawSearchIndex) {
var MAX_LEV_DISTANCE = 3;
var MAX_RESULTS = 200;
var GENERICS_DATA = 1;
var NAME = 0;
var INPUTS_DATA = 0;
var OUTPUT_DATA = 1;
var NO_TYPE_FILTER = -1;
var currentResults, index, searchIndex;
var ALIASES = {};
var params = getQueryStringParams();
// Populate search bar with query string search term when provided,
// but only if the input bar is empty. This avoid the obnoxious issue
// where you start trying to do a search, and the index loads, and
// suddenly your search is gone!
if (search_input.value === "") {
search_input.value = params.search || "";
}
/**
* Executes the query and builds an index of results
* @param {[Object]} query [The user query]
* @param {[type]} searchWords [The list of search words to query
* against]
* @param {[type]} filterCrates [Crate to search in if defined]
* @return {[type]} [A search index of results]
*/
function execQuery(query, searchWords, filterCrates) {
function itemTypeFromName(typename) {
for (var i = 0, len = itemTypes.length; i < len; ++i) {
if (itemTypes[i] === typename) {
return i;
}
}
return NO_TYPE_FILTER;
}
var valLower = query.query.toLowerCase(),
val = valLower,
typeFilter = itemTypeFromName(query.type),
results = {}, results_in_args = {}, results_returned = {},
split = valLower.split("::");
split = split.filter(function(segment) { return segment !== ""; });
function transformResults(results, isType) {
var out = [];
for (var i = 0, len = results.length; i < len; ++i) {
if (results[i].id > -1) {
var obj = searchIndex[results[i].id];
obj.lev = results[i].lev;
if (isType !== true || obj.type) {
var res = buildHrefAndPath(obj);
obj.displayPath = pathSplitter(res[0]);
obj.fullPath = obj.displayPath + obj.name;
// To be sure than it some items aren't considered as duplicate.
obj.fullPath += "|" + obj.ty;
obj.href = res[1];
out.push(obj);
if (out.length >= MAX_RESULTS) {
break;
}
}
}
}
return out;
}
function sortResults(results, isType) {
var ar = [];
for (var entry in results) {
if (hasOwnProperty(results, entry)) {
ar.push(results[entry]);
}
}
results = ar;
var i, len, result;
for (i = 0, len = results.length; i < len; ++i) {
result = results[i];
result.word = searchWords[result.id];
result.item = searchIndex[result.id] || {};
}
// if there are no results then return to default and fail
if (results.length === 0) {
return [];
}
results.sort(function(aaa, bbb) {
var a, b;
// sort by exact match with regard to the last word (mismatch goes later)
a = (aaa.word !== val);
b = (bbb.word !== val);
if (a !== b) { return a - b; }
// Sort by non levenshtein results and then levenshtein results by the distance
// (less changes required to match means higher rankings)
a = (aaa.lev);
b = (bbb.lev);
if (a !== b) { return a - b; }
// sort by crate (non-current crate goes later)
a = (aaa.item.crate !== window.currentCrate);
b = (bbb.item.crate !== window.currentCrate);
if (a !== b) { return a - b; }
// sort by item name length (longer goes later)
a = aaa.word.length;
b = bbb.word.length;
if (a !== b) { return a - b; }
// sort by item name (lexicographically larger goes later)
a = aaa.word;
b = bbb.word;
if (a !== b) { return (a > b ? +1 : -1); }
// sort by index of keyword in item name (no literal occurrence goes later)
a = (aaa.index < 0);
b = (bbb.index < 0);
if (a !== b) { return a - b; }
// (later literal occurrence, if any, goes later)
a = aaa.index;
b = bbb.index;
if (a !== b) { return a - b; }
// special precedence for primitive and keyword pages
if ((aaa.item.ty === TY_PRIMITIVE && bbb.item.ty !== TY_KEYWORD) ||
(aaa.item.ty === TY_KEYWORD && bbb.item.ty !== TY_PRIMITIVE)) {
return -1;
}
if ((bbb.item.ty === TY_PRIMITIVE && aaa.item.ty !== TY_PRIMITIVE) ||
(bbb.item.ty === TY_KEYWORD && aaa.item.ty !== TY_KEYWORD)) {
return 1;
}
// sort by description (no description goes later)
a = (aaa.item.desc === "");
b = (bbb.item.desc === "");
if (a !== b) { return a - b; }
// sort by type (later occurrence in `itemTypes` goes later)
a = aaa.item.ty;
b = bbb.item.ty;
if (a !== b) { return a - b; }
// sort by path (lexicographically larger goes later)
a = aaa.item.path;
b = bbb.item.path;
if (a !== b) { return (a > b ? +1 : -1); }
// que sera, sera
return 0;
});
for (i = 0, len = results.length; i < len; ++i) {
var result = results[i];
// this validation does not make sense when searching by types
if (result.dontValidate) {
continue;
}
var name = result.item.name.toLowerCase(),
path = result.item.path.toLowerCase(),
parent = result.item.parent;
if (isType !== true &&
validateResult(name, path, split, parent) === false)
{
result.id = -1;
}
}
return transformResults(results);
}
function extractGenerics(val) {
val = val.toLowerCase();
if (val.indexOf("<") !== -1) {
var values = val.substring(val.indexOf("<") + 1, val.lastIndexOf(">"));
return {
name: val.substring(0, val.indexOf("<")),
generics: values.split(/\s*,\s*/),
};
}
return {
name: val,
generics: [],
};
}
function getObjectFromId(id) {
if (typeof id === "number") {
return searchIndex[id];
}
return {'name': id};
}
function checkGenerics(obj, val) {
// The names match, but we need to be sure that all generics kinda
// match as well.
var lev_distance = MAX_LEV_DISTANCE + 1;
if (val.generics.length > 0) {
if (obj.length > GENERICS_DATA &&
obj[GENERICS_DATA].length >= val.generics.length) {
var elems = obj[GENERICS_DATA].slice(0);
var total = 0;
var done = 0;
// We need to find the type that matches the most to remove it in order
// to move forward.
var vlength = val.generics.length;
for (var y = 0; y < vlength; ++y) {
var lev = { pos: -1, lev: MAX_LEV_DISTANCE + 1};
var firstGeneric = getObjectFromId(val.generics[y]).name;
for (var x = 0, elength = elems.length; x < elength; ++x) {
var tmp_lev = levenshtein(getObjectFromId(elems[x]).name,
firstGeneric);
if (tmp_lev < lev.lev) {
lev.lev = tmp_lev;
lev.pos = x;
}
}
if (lev.pos !== -1) {
elems.splice(lev.pos, 1);
lev_distance = Math.min(lev.lev, lev_distance);
total += lev.lev;
done += 1;
} else {
return MAX_LEV_DISTANCE + 1;
}
}
return Math.ceil(total / done);
}
}
return MAX_LEV_DISTANCE + 1;
}
// Check for type name and type generics (if any).
function checkType(obj, val, literalSearch) {
var lev_distance = MAX_LEV_DISTANCE + 1;
var len, x, y, e_len, firstGeneric;
if (obj[NAME] === val.name) {
if (literalSearch === true) {
if (val.generics && val.generics.length !== 0) {
if (obj.length > GENERICS_DATA &&
obj[GENERICS_DATA].length >= val.generics.length) {
var elems = obj[GENERICS_DATA].slice(0);
var allFound = true;
len = val.generics.length;
for (y = 0; allFound === true && y < len; ++y) {
allFound = false;
firstGeneric = getObjectFromId(val.generics[y]).name;
e_len = elems.length;
for (x = 0; allFound === false && x < e_len; ++x) {
allFound = getObjectFromId(elems[x]).name === firstGeneric;
}
if (allFound === true) {
elems.splice(x - 1, 1);
}
}
if (allFound === true) {
return true;
}
} else {
return false;
}
}
return true;
}
// If the type has generics but don't match, then it won't return at this point.
// Otherwise, `checkGenerics` will return 0 and it'll return.
if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length !== 0) {
var tmp_lev = checkGenerics(obj, val);
if (tmp_lev <= MAX_LEV_DISTANCE) {
return tmp_lev;
}
} else {
return 0;
}
}
// Names didn't match so let's check if one of the generic types could.
if (literalSearch === true) {
if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
return obj[GENERICS_DATA].some(
function(name) {
return name === val.name;
});
}
return false;
}
lev_distance = Math.min(levenshtein(obj[NAME], val.name), lev_distance);
if (lev_distance <= MAX_LEV_DISTANCE) {
// The generics didn't match but the name kinda did so we give it
// a levenshtein distance value that isn't *this* good so it goes
// into the search results but not too high.
lev_distance = Math.ceil((checkGenerics(obj, val) + lev_distance) / 2);
} else if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
// We can check if the type we're looking for is inside the generics!
var olength = obj[GENERICS_DATA].length;
for (x = 0; x < olength; ++x) {
lev_distance = Math.min(levenshtein(obj[GENERICS_DATA][x], val.name),
lev_distance);
}
}
// Now whatever happens, the returned distance is "less good" so we should mark it
// as such, and so we add 1 to the distance to make it "less good".
return lev_distance + 1;
}
function findArg(obj, val, literalSearch, typeFilter) {
var lev_distance = MAX_LEV_DISTANCE + 1;
if (obj && obj.type && obj.type[INPUTS_DATA] && obj.type[INPUTS_DATA].length > 0) {
var length = obj.type[INPUTS_DATA].length;
for (var i = 0; i < length; i++) {
var tmp = obj.type[INPUTS_DATA][i];
if (typePassesFilter(typeFilter, tmp[1]) === false) {
continue;
}
tmp = checkType(tmp, val, literalSearch);
if (literalSearch === true) {
if (tmp === true) {
return true;
}
continue;
}
lev_distance = Math.min(tmp, lev_distance);
if (lev_distance === 0) {
return 0;
}
}
}
return literalSearch === true ? false : lev_distance;
}
function checkReturned(obj, val, literalSearch, typeFilter) {
var lev_distance = MAX_LEV_DISTANCE + 1;
if (obj && obj.type && obj.type.length > OUTPUT_DATA) {
var ret = obj.type[OUTPUT_DATA];
if (typeof ret[0] === "string") {
ret = [ret];
}
for (var x = 0, len = ret.length; x < len; ++x) {
var tmp = ret[x];
if (typePassesFilter(typeFilter, tmp[1]) === false) {
continue;
}
tmp = checkType(tmp, val, literalSearch);
if (literalSearch === true) {
if (tmp === true) {
return true;
}
continue;
}
lev_distance = Math.min(tmp, lev_distance);