Skip to content

Commit acf9d67

Browse files
author
Eljay
committed
Improve rustdoc search type filtering.
1 parent 4b79add commit acf9d67

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/librustdoc/html/layout.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ r##"<!DOCTYPE html>
113113
<p>
114114
Accepted types are: <code>fn</code>, <code>mod</code>,
115115
<code>struct</code>, <code>enum</code>,
116-
<code>trait</code>, <code>typedef</code> (or
117-
<code>tdef</code>).
116+
<code>trait</code>, <code>type</code>, <code>macro</code>,
117+
and <code>const</code>.
118118
</p>
119119
120120
<p>

src/librustdoc/html/static/main.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,28 @@
230230
}
231231
}
232232

233+
function typePassesFilter(filter, type) {
234+
// No filter
235+
if (filter < 0) return true;
236+
237+
// Exact match
238+
if (filter === type) return true;
239+
240+
// Match related items
241+
var name = itemTypes[type];
242+
switch (itemTypes[filter]) {
243+
case "constant":
244+
return (name == "associatedconstant");
245+
case "fn":
246+
return (name == "method" || name == "tymethod");
247+
case "type":
248+
return (name == "primitive");
249+
}
250+
251+
// No match
252+
return false;
253+
}
254+
233255
// quoted values mean literal search
234256
var nSearchWords = searchWords.length;
235257
if ((val.charAt(0) === "\"" || val.charAt(0) === "'") &&
@@ -239,7 +261,7 @@
239261
for (var i = 0; i < nSearchWords; ++i) {
240262
if (searchWords[i] === val) {
241263
// filter type: ... queries
242-
if (typeFilter < 0 || typeFilter === searchIndex[i].ty) {
264+
if (typePassesFilter(typeFilter, searchIndex[i].ty)) {
243265
results.push({id: i, index: -1});
244266
}
245267
}
@@ -285,7 +307,7 @@
285307
searchWords[j].replace(/_/g, "").indexOf(val) > -1)
286308
{
287309
// filter type: ... queries
288-
if (typeFilter < 0 || typeFilter === searchIndex[j].ty) {
310+
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
289311
results.push({
290312
id: j,
291313
index: searchWords[j].replace(/_/g, "").indexOf(val),
@@ -295,7 +317,7 @@
295317
} else if (
296318
(lev_distance = levenshtein(searchWords[j], val)) <=
297319
MAX_LEV_DISTANCE) {
298-
if (typeFilter < 0 || typeFilter === searchIndex[j].ty) {
320+
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
299321
results.push({
300322
id: j,
301323
index: 0,
@@ -451,11 +473,9 @@
451473
var matches, type, query, raw = $('.search-input').val();
452474
query = raw;
453475

454-
matches = query.match(/^(fn|mod|struct|enum|trait|t(ype)?d(ef)?)\s*:\s*/i);
476+
matches = query.match(/^(fn|mod|struct|enum|trait|type|const|macro)\s*:\s*/i);
455477
if (matches) {
456-
type = matches[1].replace(/^td$/, 'typedef')
457-
.replace(/^tdef$/, 'typedef')
458-
.replace(/^typed$/, 'typedef');
478+
type = matches[1].replace(/^const$/, 'constant');
459479
query = query.substring(matches[0].length);
460480
}
461481

0 commit comments

Comments
 (0)