Skip to content

Commit d21c023

Browse files
committed
Auto merge of #45055 - GuillaumeGomez:search-tabs, r=QuietMisdreavus
Add tabs for search for better information access A few screenshots: <img width="1440" alt="screen shot 2017-10-06 at 00 54 51" src="https://user-images.githubusercontent.com/3050060/31256148-032c1a06-aa31-11e7-8e4c-fec59786b8e6.png"> <img width="1440" alt="screen shot 2017-10-06 at 00 54 58" src="https://user-images.githubusercontent.com/3050060/31256150-03312cb2-aa31-11e7-86f7-8c9f0d8d6d4f.png"> <img width="1440" alt="screen shot 2017-10-06 at 00 55 00" src="https://user-images.githubusercontent.com/3050060/31256149-0330d456-aa31-11e7-8f89-3b3c824e30b4.png"> r? @rust-lang/docs cc @killercup @QuietMisdreavus
2 parents 43d95e2 + 3a65d12 commit d21c023

File tree

3 files changed

+175
-22
lines changed

3 files changed

+175
-22
lines changed

src/librustdoc/html/static/main.js

+145-22
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,17 @@
342342
}
343343
}
344344

345+
function findArg(obj, val) {
346+
if (obj && obj.type && obj.type.inputs.length > 0) {
347+
for (var i = 0; i < obj.type.inputs.length; i++) {
348+
if (obj.type.inputs[i].name === val) {
349+
return true;
350+
}
351+
}
352+
}
353+
return false;
354+
}
355+
345356
function typePassesFilter(filter, type) {
346357
// No filter
347358
if (filter < 0) return true;
@@ -376,18 +387,28 @@
376387
if (typePassesFilter(typeFilter, searchIndex[i].ty)) {
377388
results.push({id: i, index: -1});
378389
}
390+
} else if (findArg(searchIndex[i], val.toLowerCase()) ||
391+
(searchIndex[i].type &&
392+
searchIndex[i].type.output &&
393+
searchIndex[i].type.output.name === val.toLowerCase())) {
394+
if (typePassesFilter(typeFilter, searchIndex[i].ty)) {
395+
results.push({id: i, index: -1, dontValidate: true});
396+
}
379397
}
380398
if (results.length === max) {
381399
break;
382400
}
383401
}
402+
query.inputs = [val];
403+
query.output = val;
404+
query.search = val;
384405
// searching by type
385406
} else if (val.search("->") > -1) {
386407
var trimmer = function (s) { return s.trim(); };
387408
var parts = val.split("->").map(trimmer);
388409
var input = parts[0];
389410
// sort inputs so that order does not matter
390-
var inputs = input.split(",").map(trimmer).sort().toString();
411+
var inputs = input.split(",").map(trimmer).sort();
391412
var output = parts[1];
392413

393414
for (var i = 0; i < nSearchWords; ++i) {
@@ -403,12 +424,30 @@
403424

404425
// allow searching for void (no output) functions as well
405426
var typeOutput = type.output ? type.output.name : "";
406-
if ((inputs === "*" || inputs === typeInputs.toString()) &&
407-
(output === "*" || output == typeOutput)) {
408-
results.push({id: i, index: -1, dontValidate: true});
427+
if (output === "*" || output == typeOutput) {
428+
if (input === "*") {
429+
results.push({id: i, index: -1, dontValidate: true});
430+
} else {
431+
var allFound = true;
432+
for (var it = 0; allFound === true && it < inputs.length; it++) {
433+
var found = false;
434+
for (var y = 0; found === false && y < typeInputs.length; y++) {
435+
found = typeInputs[y] === inputs[it];
436+
}
437+
allFound = found;
438+
}
439+
if (allFound === true) {
440+
results.push({id: i, index: -1, dontValidate: true});
441+
}
442+
}
409443
}
410444
}
445+
query.inputs = inputs;
446+
query.output = output;
411447
} else {
448+
query.inputs = [val];
449+
query.output = val;
450+
query.search = val;
412451
// gather matching search results up to a certain maximum
413452
val = val.replace(/\_/g, "");
414453
for (var i = 0; i < split.length; ++i) {
@@ -437,6 +476,15 @@
437476
lev: lev_distance,
438477
});
439478
}
479+
} else if (findArg(searchIndex[j], val)) {
480+
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
481+
results.push({
482+
id: j,
483+
index: 0,
484+
// we want lev results to go lower than others
485+
lev: lev_distance,
486+
});
487+
}
440488
}
441489
if (results.length === max) {
442490
break;
@@ -576,8 +624,7 @@
576624
(parent !== undefined &&
577625
parent.name.toLowerCase().indexOf(keys[i]) > -1) ||
578626
// lastly check to see if the name was a levenshtein match
579-
levenshtein(name.toLowerCase(), keys[i]) <=
580-
MAX_LEV_DISTANCE)) {
627+
levenshtein(name.toLowerCase(), keys[i]) <= MAX_LEV_DISTANCE)) {
581628
return false;
582629
}
583630
}
@@ -692,18 +739,18 @@
692739
return h1.innerHTML;
693740
}
694741

695-
function showResults(results) {
696-
var output, shown, query = getQuery();
697-
698-
currentResults = query.id;
699-
output = '<h1>Results for ' + escape(query.query) +
700-
(query.type ? ' (type: ' + escape(query.type) + ')' : '') + '</h1>';
701-
output += '<table class="search-results">';
742+
function addTab(array, query, display) {
743+
var extraStyle = '';
744+
if (display === false) {
745+
extraStyle = ' style="display: none;"';
746+
}
702747

703-
if (results.length > 0) {
704-
shown = [];
748+
var output = '';
749+
if (array.length > 0) {
750+
output = `<table class="search-results"${extraStyle}>`;
751+
var shown = [];
705752

706-
results.forEach(function(item) {
753+
array.forEach(function(item) {
707754
var name, type, href, displayPath;
708755

709756
if (shown.indexOf(item) !== -1) {
@@ -752,13 +799,32 @@
752799
'<span class="desc">' + escape(item.desc) +
753800
'&nbsp;</span></a></td></tr>';
754801
});
802+
output += '</table>';
755803
} else {
756-
output += 'No results :( <a href="https://duckduckgo.com/?q=' +
804+
output = `<div class="search-failed"${extraStyle}>No results :(<br/>` +
805+
'Try on <a href="https://duckduckgo.com/?q=' +
757806
encodeURIComponent('rust ' + query.query) +
758-
'">Try on DuckDuckGo?</a>';
807+
'">DuckDuckGo</a>?</div>';
759808
}
809+
return output;
810+
}
811+
812+
function showResults(results) {
813+
var output, query = getQuery();
814+
815+
currentResults = query.id;
816+
output = '<h1>Results for ' + escape(query.query) +
817+
(query.type ? ' (type: ' + escape(query.type) + ')' : '') + '</h1>' +
818+
'<div id="titles">' +
819+
'<div class="selected">Types/modules</div>' +
820+
'<div>As parameters</div>' +
821+
'<div>As return value</div></div><div id="results">';
822+
823+
output += addTab(results['others'], query);
824+
output += addTab(results['in_args'], query, false);
825+
output += addTab(results['returned'], query, false);
826+
output += '</div>';
760827

761-
output += "</p>";
762828
addClass(document.getElementById('main'), 'hidden');
763829
var search = document.getElementById('search');
764830
removeClass(search, 'hidden');
@@ -773,13 +839,17 @@
773839
e.style.width = width + 'px';
774840
});
775841
initSearchNav();
842+
var elems = document.getElementById('titles').childNodes;
843+
elems[0].onclick = function() { printTab(0); };
844+
elems[1].onclick = function() { printTab(1); };
845+
elems[2].onclick = function() { printTab(2); };
776846
}
777847

778848
function search(e) {
779849
var query,
780850
filterdata = [],
781851
obj, i, len,
782-
results = [],
852+
results = {"in_args": [], "returned": [], "others": []},
783853
maxResults = 200,
784854
resultIndex;
785855
var params = getQueryStringParams();
@@ -810,11 +880,43 @@
810880
len = resultIndex.length;
811881
for (i = 0; i < len; ++i) {
812882
if (resultIndex[i].id > -1) {
883+
var added = false;
813884
obj = searchIndex[resultIndex[i].id];
814885
filterdata.push([obj.name, obj.ty, obj.path, obj.desc]);
815-
results.push(obj);
886+
if (obj.type) {
887+
if (results['returned'].length < maxResults &&
888+
obj.type.output &&
889+
obj.type.output.name.toLowerCase() === query.output) {
890+
results['returned'].push(obj);
891+
added = true;
892+
}
893+
if (results['in_args'].length < maxResults && obj.type.inputs.length > 0) {
894+
var all_founds = true;
895+
for (var it = 0;
896+
all_founds === true && it < query.inputs.length;
897+
it++) {
898+
var found = false;
899+
for (var y = 0;
900+
found === false && y < obj.type.inputs.length;
901+
y++) {
902+
found = query.inputs[it] === obj.type.inputs[y].name;
903+
}
904+
all_founds = found;
905+
}
906+
if (all_founds === true) {
907+
results['in_args'].push(obj);
908+
added = true;
909+
}
910+
}
911+
}
912+
if (results['others'].length < maxResults &&
913+
((query.search && obj.name.indexOf(query.search)) || added === false)) {
914+
results['others'].push(obj);
915+
}
816916
}
817-
if (results.length >= maxResults) {
917+
if (results['others'].length >= maxResults &&
918+
results['in_args'].length >= maxResults &&
919+
results['returned'].length >= maxResults) {
818920
break;
819921
}
820922
}
@@ -1290,6 +1392,27 @@
12901392
return wrapper;
12911393
}
12921394

1395+
// In the search display, allows to switch between tabs.
1396+
function printTab(nb) {
1397+
var nb_copy = nb;
1398+
onEach(document.getElementById('titles').childNodes, function(elem) {
1399+
if (nb_copy === 0) {
1400+
addClass(elem, 'selected');
1401+
} else {
1402+
removeClass(elem, 'selected');
1403+
}
1404+
nb_copy -= 1;
1405+
});
1406+
onEach(document.getElementById('results').childNodes, function(elem) {
1407+
if (nb === 0) {
1408+
elem.style.display = '';
1409+
} else {
1410+
elem.style.display = 'none';
1411+
}
1412+
nb -= 1;
1413+
});
1414+
}
1415+
12931416
onEach(document.getElementById('main').getElementsByTagName('pre'), function(e) {
12941417
onEach(e.getElementsByClassName('attributes'), function(i_e) {
12951418
i_e.parentNode.insertBefore(createToggleWrapper(), i_e);

src/librustdoc/html/static/rustdoc.css

+26
Original file line numberDiff line numberDiff line change
@@ -882,3 +882,29 @@ span.since {
882882
pre.rust {
883883
position: relative;
884884
}
885+
886+
.search-failed {
887+
text-align: center;
888+
margin-top: 20px;
889+
}
890+
891+
#titles {
892+
height: 35px;
893+
}
894+
895+
#titles > div {
896+
float: left;
897+
width: 33.3%;
898+
text-align: center;
899+
border-bottom: 1px solid #ccc;
900+
font-size: 18px;
901+
cursor: pointer;
902+
}
903+
904+
#titles > div.selected {
905+
border-bottom: 3px solid #0078ee;
906+
}
907+
908+
#titles > div:hover {
909+
border-bottom: 3px solid #0089ff;
910+
}

src/librustdoc/html/static/styles/main.css

+4
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,7 @@ pre.ignore:hover, .information:hover + pre.ignore {
235235
.information > .ignore:hover {
236236
color: rgba(255,142,0,1);
237237
}
238+
239+
.search-failed > a {
240+
color: #0089ff;
241+
}

0 commit comments

Comments
 (0)