Skip to content

Commit acf0adf

Browse files
Add setting to go to item if there is only one result
1 parent 88cd367 commit acf0adf

File tree

2 files changed

+59
-31
lines changed

2 files changed

+59
-31
lines changed

src/librustdoc/html/render.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,8 @@ impl<'a> Settings<'a> {
15941594
settings: vec![
15951595
("item-declarations", "Auto-hide item declarations.", true),
15961596
("item-attributes", "Auto-hide item attributes.", true),
1597+
("go-to-only-result", "Directly go to item in search if there is only one result",
1598+
false),
15971599
],
15981600
root_path,
15991601
suffix,

src/librustdoc/html/static/main.js

+57-31
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,8 @@
10131013
'returned': sortResults(results_returned, true),
10141014
'others': sortResults(results),
10151015
};
1016-
if (ALIASES[window.currentCrate][query.raw]) {
1016+
if (ALIASES && ALIASES[window.currentCrate] &&
1017+
ALIASES[window.currentCrate][query.raw]) {
10171018
var aliases = ALIASES[window.currentCrate][query.raw];
10181019
for (var i = 0; i < aliases.length; ++i) {
10191020
ret['others'].unshift(aliases[i]);
@@ -1188,6 +1189,44 @@
11881189
return '<span>' + path.replace(/::/g, '::</span><span>');
11891190
}
11901191

1192+
function buildHrefAndPath(item) {
1193+
var displayPath;
1194+
var href;
1195+
var type = itemTypes[item.ty];
1196+
var name = item.name;
1197+
1198+
if (type === 'mod') {
1199+
displayPath = item.path + '::';
1200+
href = rootPath + item.path.replace(/::/g, '/') + '/' +
1201+
name + '/index.html';
1202+
} else if (type === "primitive") {
1203+
displayPath = "";
1204+
href = rootPath + item.path.replace(/::/g, '/') +
1205+
'/' + type + '.' + name + '.html';
1206+
} else if (type === "externcrate") {
1207+
displayPath = "";
1208+
href = rootPath + name + '/index.html';
1209+
} else if (item.parent !== undefined) {
1210+
var myparent = item.parent;
1211+
var anchor = '#' + type + '.' + name;
1212+
var parentType = itemTypes[myparent.ty];
1213+
if (parentType === "primitive") {
1214+
displayPath = myparent.name + '::';
1215+
} else {
1216+
displayPath = item.path + '::' + myparent.name + '::';
1217+
}
1218+
href = rootPath + item.path.replace(/::/g, '/') +
1219+
'/' + parentType +
1220+
'.' + myparent.name +
1221+
'.html' + anchor;
1222+
} else {
1223+
displayPath = item.path + '::';
1224+
href = rootPath + item.path.replace(/::/g, '/') +
1225+
'/' + type + '.' + name + '.html';
1226+
}
1227+
return [displayPath, href];
1228+
}
1229+
11911230
function addTab(array, query, display) {
11921231
var extraStyle = '';
11931232
if (display === false) {
@@ -1211,35 +1250,9 @@
12111250
name = item.name;
12121251
type = itemTypes[item.ty];
12131252

1214-
if (type === 'mod') {
1215-
displayPath = item.path + '::';
1216-
href = rootPath + item.path.replace(/::/g, '/') + '/' +
1217-
name + '/index.html';
1218-
} else if (type === "primitive") {
1219-
displayPath = "";
1220-
href = rootPath + item.path.replace(/::/g, '/') +
1221-
'/' + type + '.' + name + '.html';
1222-
} else if (type === "externcrate") {
1223-
displayPath = "";
1224-
href = rootPath + name + '/index.html';
1225-
} else if (item.parent !== undefined) {
1226-
var myparent = item.parent;
1227-
var anchor = '#' + type + '.' + name;
1228-
var parentType = itemTypes[myparent.ty];
1229-
if (parentType === "primitive") {
1230-
displayPath = myparent.name + '::';
1231-
} else {
1232-
displayPath = item.path + '::' + myparent.name + '::';
1233-
}
1234-
href = rootPath + item.path.replace(/::/g, '/') +
1235-
'/' + parentType +
1236-
'.' + myparent.name +
1237-
'.html' + anchor;
1238-
} else {
1239-
displayPath = item.path + '::';
1240-
href = rootPath + item.path.replace(/::/g, '/') +
1241-
'/' + type + '.' + name + '.html';
1242-
}
1253+
var res = buildHrefAndPath(item);
1254+
var href = res[1];
1255+
var displayPath = res[0];
12431256

12441257
output += '<tr class="' + type + ' result"><td>' +
12451258
'<a href="' + href + '">' +
@@ -1268,6 +1281,16 @@
12681281
}
12691282

12701283
function showResults(results) {
1284+
if (results['others'].length === 1 &&
1285+
getCurrentValue('rustdoc-go-to-only-result') === "true") {
1286+
var elem = document.createElement('a');
1287+
var res = buildHrefAndPath(results['others'][0]);
1288+
elem.href = res[1];
1289+
elem.style.display = 'none';
1290+
// For firefox, we need the element to be in the DOM so it can be clicked.
1291+
document.body.appendChild(elem);
1292+
elem.click();
1293+
}
12711294
var output, query = getQuery(search_input.value);
12721295

12731296
currentResults = query.id;
@@ -1721,6 +1744,9 @@
17211744

17221745
function toggleAllDocs(pageId) {
17231746
var toggle = document.getElementById("toggle-all-docs");
1747+
if (!toggle) {
1748+
return;
1749+
}
17241750
if (hasClass(toggle, "will-expand")) {
17251751
updateLocalStorage("rustdoc-collapse", "false");
17261752
removeClass(toggle, "will-expand");
@@ -1977,7 +2003,7 @@
19772003
collapseDocs(e.previousSibling.childNodes[0], "toggle");
19782004
}
19792005
}
1980-
})
2006+
});
19812007

19822008
autoCollapseAllImpls(getPageId());
19832009

0 commit comments

Comments
 (0)