Skip to content

Commit a72ed8d

Browse files
author
Oliver Schneider
committed
Rollup merge of rust-lang#25702 - killercup:rustdoc/search-primitives, r=Gankro
This minimally changes rustdoc's search as described in rust-lang#25167. Additionally, I also cleaned up some parts of the JS code. There is one more change I made: After each result for a primitive type, I added "(Overview of primitive type)". This further differentiates the result from the module (previously, the only difference was that the module's link was blue). I'm not this is the way to go (this seems to be the only place where we do this) and it's no problem for me to remove that commit. ![std__str_-_rust](https://cloud.githubusercontent.com/assets/20063/7770589/67e8cb26-0090-11e5-8f99-c2a3af9fa37f.png) cc @steveklabnik (it concerns docs) and @alexcrichton (who made changes to rustdoc previously)
2 parents 1b030dd + ec60d9f commit a72ed8d

File tree

2 files changed

+66
-60
lines changed

2 files changed

+66
-60
lines changed

src/librustdoc/html/static/main.css

+2
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ a {
443443
.content .search-results td:first-child { padding-right: 0; }
444444
.content .search-results td:first-child a { padding-right: 10px; }
445445

446+
tr.result span.primitive::after { content: ' (primitive type)'; font-style: italic; }
447+
446448
#help {
447449
background: #e9e9e9;
448450
border-radius: 4px;

src/librustdoc/html/static/main.js

+64-60
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
(function() {
1515
"use strict";
16-
var resizeTimeout, interval;
1716

1817
// This mapping table should match the discriminants of
1918
// `rustdoc::html::item_type::ItemType` type in Rust.
@@ -37,6 +36,9 @@
3736
"constant",
3837
"associatedconstant"];
3938

39+
// used for special search precedence
40+
var TY_PRIMITIVE = itemTypes.indexOf("primitive");
41+
4042
$('.js-only').removeClass('js-only');
4143

4244
function getQueryStringParams() {
@@ -64,7 +66,7 @@
6466
if ($('#' + from).length === 0) {
6567
return;
6668
}
67-
if (ev === null) $('#' + from)[0].scrollIntoView();
69+
if (ev === null) { $('#' + from)[0].scrollIntoView(); };
6870
$('.line-numbers span').removeClass('line-highlighted');
6971
for (i = from; i <= to; ++i) {
7072
$('#' + i).addClass('line-highlighted');
@@ -74,7 +76,7 @@
7476
highlightSourceLines(null);
7577
$(window).on('hashchange', highlightSourceLines);
7678

77-
$(document).on('keyup', function(e) {
79+
$(document).on('keyup', function handleKeyboardShortcut(e) {
7880
if (document.activeElement.tagName === 'INPUT') {
7981
return;
8082
}
@@ -133,29 +135,28 @@
133135
return function(s1, s2) {
134136
if (s1 === s2) {
135137
return 0;
136-
} else {
137-
var s1_len = s1.length, s2_len = s2.length;
138-
if (s1_len && s2_len) {
139-
var i1 = 0, i2 = 0, a, b, c, c2, row = row2;
140-
while (i1 < s1_len)
141-
row[i1] = ++i1;
142-
while (i2 < s2_len) {
143-
c2 = s2.charCodeAt(i2);
144-
a = i2;
145-
++i2;
146-
b = i2;
147-
for (i1 = 0; i1 < s1_len; ++i1) {
148-
c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
149-
a = row[i1];
150-
b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
151-
row[i1] = b;
152-
}
138+
}
139+
var s1_len = s1.length, s2_len = s2.length;
140+
if (s1_len && s2_len) {
141+
var i1 = 0, i2 = 0, a, b, c, c2, row = row2;
142+
while (i1 < s1_len) {
143+
row[i1] = ++i1;
144+
}
145+
while (i2 < s2_len) {
146+
c2 = s2.charCodeAt(i2);
147+
a = i2;
148+
++i2;
149+
b = i2;
150+
for (i1 = 0; i1 < s1_len; ++i1) {
151+
c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
152+
a = row[i1];
153+
b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
154+
row[i1] = b;
153155
}
154-
return b;
155-
} else {
156-
return s1_len + s2_len;
157156
}
157+
return b;
158158
}
159+
return s1_len + s2_len;
159160
};
160161
})();
161162

@@ -187,7 +188,7 @@
187188
results = [],
188189
split = valLower.split("::");
189190

190-
//remove empty keywords
191+
// remove empty keywords
191192
for (var j = 0; j < split.length; ++j) {
192193
split[j].toLowerCase();
193194
if (split[j] === "") {
@@ -286,58 +287,63 @@
286287
return [];
287288
}
288289

289-
results.sort(function(aaa, bbb) {
290+
results.sort(function sortResults(aaa, bbb) {
290291
var a, b;
291292

292293
// Sort by non levenshtein results and then levenshtein results by the distance
293294
// (less changes required to match means higher rankings)
294295
a = (aaa.lev);
295296
b = (bbb.lev);
296-
if (a !== b) return a - b;
297+
if (a !== b) { return a - b; }
297298

298299
// sort by crate (non-current crate goes later)
299300
a = (aaa.item.crate !== window.currentCrate);
300301
b = (bbb.item.crate !== window.currentCrate);
301-
if (a !== b) return a - b;
302+
if (a !== b) { return a - b; }
302303

303304
// sort by exact match (mismatch goes later)
304305
a = (aaa.word !== valLower);
305306
b = (bbb.word !== valLower);
306-
if (a !== b) return a - b;
307+
if (a !== b) { return a - b; }
307308

308309
// sort by item name length (longer goes later)
309310
a = aaa.word.length;
310311
b = bbb.word.length;
311-
if (a !== b) return a - b;
312+
if (a !== b) { return a - b; }
312313

313314
// sort by item name (lexicographically larger goes later)
314315
a = aaa.word;
315316
b = bbb.word;
316-
if (a !== b) return (a > b ? +1 : -1);
317+
if (a !== b) { return (a > b ? +1 : -1); }
317318

318319
// sort by index of keyword in item name (no literal occurrence goes later)
319320
a = (aaa.index < 0);
320321
b = (bbb.index < 0);
321-
if (a !== b) return a - b;
322+
if (a !== b) { return a - b; }
322323
// (later literal occurrence, if any, goes later)
323324
a = aaa.index;
324325
b = bbb.index;
325-
if (a !== b) return a - b;
326+
if (a !== b) { return a - b; }
327+
328+
// special precedence for primitive pages
329+
if ((aaa.item.ty === TY_PRIMITIVE) && (bbb.item.ty !== TY_PRIMITIVE)) {
330+
return -1;
331+
}
326332

327333
// sort by description (no description goes later)
328334
a = (aaa.item.desc === '');
329335
b = (bbb.item.desc === '');
330-
if (a !== b) return a - b;
336+
if (a !== b) { return a - b; }
331337

332338
// sort by type (later occurrence in `itemTypes` goes later)
333339
a = aaa.item.ty;
334340
b = bbb.item.ty;
335-
if (a !== b) return a - b;
341+
if (a !== b) { return a - b; }
336342

337343
// sort by path (lexicographically larger goes later)
338344
a = aaa.item.path;
339345
b = bbb.item.path;
340-
if (a !== b) return (a > b ? +1 : -1);
346+
if (a !== b) { return (a > b ? +1 : -1); }
341347

342348
// que sera, sera
343349
return 0;
@@ -388,7 +394,7 @@
388394
* @return {[boolean]} [Whether the result is valid or not]
389395
*/
390396
function validateResult(name, path, keys, parent) {
391-
for (var i=0; i < keys.length; ++i) {
397+
for (var i = 0; i < keys.length; ++i) {
392398
// each check is for validation so we negate the conditions and invalidate
393399
if (!(
394400
// check for an exact name match
@@ -423,7 +429,7 @@
423429
raw: raw,
424430
query: query,
425431
type: type,
426-
id: query + type,
432+
id: query + type
427433
};
428434
}
429435

@@ -432,7 +438,7 @@
432438

433439
$results.on('click', function() {
434440
var dst = $(this).find('a')[0];
435-
if (window.location.pathname == dst.pathname) {
441+
if (window.location.pathname === dst.pathname) {
436442
$('#search').addClass('hidden');
437443
$('#main').removeClass('hidden');
438444
document.location.href = dst.href;
@@ -595,7 +601,7 @@
595601

596602
function itemTypeFromName(typename) {
597603
for (var i = 0; i < itemTypes.length; ++i) {
598-
if (itemTypes[i] === typename) return i;
604+
if (itemTypes[i] === typename) { return i; }
599605
}
600606
return -1;
601607
}
@@ -604,7 +610,7 @@
604610
searchIndex = [];
605611
var searchWords = [];
606612
for (var crate in rawSearchIndex) {
607-
if (!rawSearchIndex.hasOwnProperty(crate)) { continue }
613+
if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
608614

609615
// an array of [(Number) item type,
610616
// (String) name,
@@ -690,32 +696,31 @@
690696
}
691697

692698
function plainSummaryLine(markdown) {
693-
var str = markdown.replace(/\n/g, ' ')
694-
str = str.replace(/'/g, "\'")
695-
str = str.replace(/^#+? (.+?)/, "$1")
696-
str = str.replace(/\[(.*?)\]\(.*?\)/g, "$1")
697-
str = str.replace(/\[(.*?)\]\[.*?\]/g, "$1")
698-
return str;
699+
markdown.replace(/\n/g, ' ')
700+
.replace(/'/g, "\'")
701+
.replace(/^#+? (.+?)/, "$1")
702+
.replace(/\[(.*?)\]\(.*?\)/g, "$1")
703+
.replace(/\[(.*?)\]\[.*?\]/g, "$1");
699704
}
700705

701706
index = buildIndex(rawSearchIndex);
702707
startSearch();
703708

704709
// Draw a convenient sidebar of known crates if we have a listing
705-
if (rootPath == '../') {
710+
if (rootPath === '../') {
706711
var sidebar = $('.sidebar');
707712
var div = $('<div>').attr('class', 'block crate');
708713
div.append($('<h2>').text('Crates'));
709714

710715
var crates = [];
711716
for (var crate in rawSearchIndex) {
712-
if (!rawSearchIndex.hasOwnProperty(crate)) { continue }
717+
if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
713718
crates.push(crate);
714719
}
715720
crates.sort();
716721
for (var i = 0; i < crates.length; ++i) {
717722
var klass = 'crate';
718-
if (crates[i] == window.currentCrate) {
723+
if (crates[i] === window.currentCrate) {
719724
klass += ' current';
720725
}
721726
if (rawSearchIndex[crates[i]].items[0]) {
@@ -738,7 +743,7 @@
738743

739744
function block(shortty, longty) {
740745
var filtered = items[shortty];
741-
if (!filtered) return;
746+
if (!filtered) { return; }
742747

743748
var div = $('<div>').attr('class', 'block ' + shortty);
744749
div.append($('<h2>').text(longty));
@@ -749,7 +754,7 @@
749754
var desc = item[1]; // can be null
750755

751756
var klass = shortty;
752-
if (name === current.name && shortty == current.ty) {
757+
if (name === current.name && shortty === current.ty) {
753758
klass += ' current';
754759
}
755760
var path;
@@ -779,7 +784,7 @@
779784
var list = $('#implementors-list');
780785
var libs = Object.getOwnPropertyNames(imp);
781786
for (var i = 0; i < libs.length; ++i) {
782-
if (libs[i] == currentCrate) continue;
787+
if (libs[i] === currentCrate) { continue; }
783788
var structs = imp[libs[i]];
784789
for (var j = 0; j < structs.length; ++j) {
785790
var code = $('<code>').append(structs[j]);
@@ -811,11 +816,10 @@
811816
if (sectionIsCollapsed) {
812817
// button will expand the section
813818
return "+";
814-
} else {
815-
// button will collapse the section
816-
// note that this text is also set in the HTML template in render.rs
817-
return "\u2212"; // "\u2212" is '−' minus sign
818819
}
820+
// button will collapse the section
821+
// note that this text is also set in the HTML template in render.rs
822+
return "\u2212"; // "\u2212" is '−' minus sign
819823
}
820824

821825
$("#toggle-all-docs").on("click", function() {
@@ -847,12 +851,12 @@
847851
}
848852
if (relatedDoc.is(".docblock")) {
849853
if (relatedDoc.is(":visible")) {
850-
relatedDoc.slideUp({duration:'fast', easing:'linear'});
854+
relatedDoc.slideUp({duration: 'fast', easing: 'linear'});
851855
toggle.parent(".toggle-wrapper").addClass("collapsed");
852856
toggle.children(".inner").text(labelForToggleButton(true));
853857
toggle.children(".toggle-label").fadeIn();
854858
} else {
855-
relatedDoc.slideDown({duration:'fast', easing:'linear'});
859+
relatedDoc.slideDown({duration: 'fast', easing: 'linear'});
856860
toggle.parent(".toggle-wrapper").removeClass("collapsed");
857861
toggle.children(".inner").text(labelForToggleButton(false));
858862
toggle.children(".toggle-label").hide();
@@ -877,7 +881,7 @@
877881
$('<span/>', {'class': 'toggle-label'})
878882
.css('display', 'none')
879883
.html('&nbsp;Expand&nbsp;description'));
880-
var wrapper = $("<div class='toggle-wrapper'>").append(mainToggle);
884+
var wrapper = $("<div class='toggle-wrapper'>").append(mainToggle);
881885
$("#main > .docblock").before(wrapper);
882886
});
883887

@@ -894,7 +898,7 @@
894898
}
895899

896900
return function(ev) {
897-
var cur_id = parseInt(ev.target.id);
901+
var cur_id = parseInt(ev.target.id, 10);
898902

899903
if (ev.shiftKey && prev_id) {
900904
if (prev_id > cur_id) {

0 commit comments

Comments
 (0)