Skip to content

Commit 47a9ffa

Browse files
Add multi-query search
1 parent fb730d7 commit 47a9ffa

File tree

3 files changed

+93
-9
lines changed

3 files changed

+93
-9
lines changed

src/librustdoc/html/static/main.js

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,14 +1299,78 @@
12991299
printTab(currentTab);
13001300
}
13011301

1302+
function execSearch(query, searchWords) {
1303+
var queries = query.raw.split(",");
1304+
var results = {
1305+
'in_args': [],
1306+
'returned': [],
1307+
'others': [],
1308+
};
1309+
1310+
for (var i = 0; i < queries.length; ++i) {
1311+
var query = queries[i].trim();
1312+
if (query.length !== 0) {
1313+
var tmp = execQuery(getQuery(query), searchWords);
1314+
1315+
results['in_args'].push(tmp['in_args']);
1316+
results['returned'].push(tmp['returned']);
1317+
results['others'].push(tmp['others']);
1318+
}
1319+
}
1320+
if (queries.length > 1) {
1321+
function getSmallest(arrays, positions) {
1322+
var start = null;
1323+
1324+
for (var it = 0; it < positions.length; ++it) {
1325+
if (arrays[it].length > positions[it] &&
1326+
(start === null || start > arrays[it][positions[it]].lev)) {
1327+
start = arrays[it][positions[it]].lev;
1328+
}
1329+
}
1330+
return start;
1331+
}
1332+
1333+
function mergeArrays(arrays) {
1334+
var ret = [];
1335+
var positions = [];
1336+
1337+
for (var x = 0; x < arrays.length; ++x) {
1338+
positions.push(0);
1339+
}
1340+
while (ret.length < MAX_RESULTS) {
1341+
var smallest = getSmallest(arrays, positions);
1342+
if (smallest === null) {
1343+
break;
1344+
}
1345+
for (x = 0; x < arrays.length && ret.length < MAX_RESULTS; ++x) {
1346+
if (arrays[x].length > positions[x] &&
1347+
arrays[x][positions[x]].lev === smallest) {
1348+
ret.push(arrays[x][positions[x]]);
1349+
positions[x] += 1;
1350+
}
1351+
}
1352+
}
1353+
return ret;
1354+
}
1355+
1356+
return {
1357+
'in_args': mergeArrays(results['in_args']),
1358+
'returned': mergeArrays(results['returned']),
1359+
'others': mergeArrays(results['others']),
1360+
};
1361+
} else {
1362+
return {
1363+
'in_args': results['in_args'][0],
1364+
'returned': results['returned'][0],
1365+
'others': results['others'][0],
1366+
};
1367+
}
1368+
}
1369+
13021370
function search(e) {
1303-
var query,
1304-
obj, i, len,
1305-
results = {"in_args": [], "returned": [], "others": []},
1306-
resultIndex;
13071371
var params = getQueryStringParams();
1372+
var query = getQuery(document.getElementsByClassName('search-input')[0].value);
13081373

1309-
query = getQuery(document.getElementsByClassName('search-input')[0].value);
13101374
if (e) {
13111375
e.preventDefault();
13121376
}
@@ -1328,8 +1392,7 @@
13281392
}
13291393
}
13301394

1331-
results = execQuery(query, index);
1332-
showResults(results);
1395+
showResults(execSearch(query, index));
13331396
}
13341397

13351398
function buildIndex(rawSearchIndex) {

src/test/rustdoc-js/multi-query.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const QUERY = 'str,u8';
12+
13+
const EXPECTED = {
14+
'others': [
15+
{ 'path': 'std', 'name': 'str' },
16+
{ 'path': 'std', 'name': 'u8' },
17+
{ 'path': 'std::ffi', 'name': 'CStr' },
18+
{ 'path': 'std::simd', 'name': 'u8x2' },
19+
],
20+
};

src/tools/rustdoc-js/tester.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ function main(argv) {
157157
// execQuery first parameter is built in getQuery (which takes in the search input).
158158
// execQuery last parameter is built in buildIndex.
159159
// buildIndex requires the hashmap from search-index.
160-
var functionsToLoad = ["levenshtein", "validateResult", "getQuery", "buildIndex", "execQuery"];
160+
var functionsToLoad = ["levenshtein", "validateResult", "getQuery", "buildIndex", "execQuery",
161+
"execSearch"];
161162

162163
finalJS += 'window = { "currentCrate": "std" };\n';
163164
finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
@@ -174,7 +175,7 @@ function main(argv) {
174175
'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;');
175176
const expected = loadedFile.EXPECTED;
176177
const query = loadedFile.QUERY;
177-
var results = loaded.execQuery(loaded.getQuery(query), index);
178+
var results = loaded.execSearch(loaded.getQuery(query), index);
178179
process.stdout.write('Checking "' + file + '" ... ');
179180
var error_text = [];
180181
for (var key in expected) {

0 commit comments

Comments
 (0)