Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ gem 'rubocop', '>= 1.31.0'
gem 'gettext'
gem 'prism', '>= 0.30.0'
gem 'webrick'

platforms :ruby do
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.2')
gem 'mini_racer' # For testing the searcher.js file
end
end
43 changes: 38 additions & 5 deletions lib/rdoc/generator/template/json_index/js/searcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Searcher.prototype = new function() {

var results =
performSearch(_this.data, regexps, queries, highlighters, state);
var hasMore = (state.limit > 0 && state.pass < 4);
var hasMore = (state.limit > 0 && state.pass < 6);

triggerResults.call(_this, results, !hasMore);
if (hasMore) {
Expand Down Expand Up @@ -85,6 +85,30 @@ Searcher.prototype = new function() {

/* ----- Mathchers ------ */

/*
* This record matches if both the index and longIndex exactly equal queries[0]
* and the record matches all of the regexps. This ensures top-level exact matches
* like "String" are prioritized over nested classes like "Gem::Module::String".
*/
function matchPassExact(index, longIndex, queries) {
return index == queries[0] && longIndex == queries[0];
}

/*
* This record matches if the index without "()" exactly equals queries[0].
* This prioritizes methods like "attribute()" when searching for "attribute".
*/
function matchPassExactMethod(index, longIndex, queries, regexps) {
var indexWithoutParens = index.replace(/\(\)$/, '');
if (indexWithoutParens != queries[0]) return false;
if (index === indexWithoutParens) return false; // Not a method (no parens to remove)
for (var i=1, l = regexps.length; i < l; i++) {
if (!index.match(regexps[i]) && !longIndex.match(regexps[i]))
return false;
};
return true;
}

/*
* This record matches if the index starts with queries[0] and the record
* matches all of the regexps
Expand Down Expand Up @@ -192,17 +216,26 @@ Searcher.prototype = new function() {
var togo = CHUNK_SIZE;
var matchFunc, hltFunc;

while (state.pass < 4 && state.limit > 0 && togo > 0) {
var isLowercaseQuery = queries[0] === queries[0].toLowerCase();

while (state.pass < 6 && state.limit > 0 && togo > 0) {
// When query is lowercase, prioritize methods over classes
if (state.pass == 0) {
matchFunc = matchPassBeginning;
matchFunc = isLowercaseQuery ? matchPassExactMethod : matchPassExact;
hltFunc = highlightQuery;
} else if (state.pass == 1) {
matchFunc = matchPassLongIndex;
matchFunc = isLowercaseQuery ? matchPassExact : matchPassExactMethod;
hltFunc = highlightQuery;
} else if (state.pass == 2) {
matchFunc = matchPassContains;
matchFunc = matchPassBeginning;
hltFunc = highlightQuery;
} else if (state.pass == 3) {
matchFunc = matchPassLongIndex;
hltFunc = highlightQuery;
} else if (state.pass == 4) {
matchFunc = matchPassContains;
hltFunc = highlightQuery;
} else if (state.pass == 5) {
matchFunc = matchPassRegexp;
hltFunc = highlightRegexp;
}
Expand Down
Loading