Skip to content

Commit b3dcacc

Browse files
committed
Make searching mor case sensitive
1 parent c74e998 commit b3dcacc

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

lib/rdoc/generator/template/json_index/js/searcher.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Searcher.prototype = new function() {
101101
function matchPassExactMethod(index, longIndex, queries, regexps) {
102102
var indexWithoutParens = index.replace(/\(\)$/, '');
103103
if (indexWithoutParens != queries[0]) return false;
104+
if (index === indexWithoutParens) return false; // Not a method (no parens to remove)
104105
for (var i=1, l = regexps.length; i < l; i++) {
105106
if (!index.match(regexps[i]) && !longIndex.match(regexps[i]))
106107
return false;
@@ -215,12 +216,15 @@ Searcher.prototype = new function() {
215216
var togo = CHUNK_SIZE;
216217
var matchFunc, hltFunc;
217218

219+
var isLowercaseQuery = queries[0] === queries[0].toLowerCase();
220+
218221
while (state.pass < 6 && state.limit > 0 && togo > 0) {
222+
// When query is lowercase, prioritize methods over classes
219223
if (state.pass == 0) {
220-
matchFunc = matchPassExact;
224+
matchFunc = isLowercaseQuery ? matchPassExactMethod : matchPassExact;
221225
hltFunc = highlightQuery;
222226
} else if (state.pass == 1) {
223-
matchFunc = matchPassExactMethod;
227+
matchFunc = isLowercaseQuery ? matchPassExact : matchPassExactMethod;
224228
hltFunc = highlightQuery;
225229
} else if (state.pass == 2) {
226230
matchFunc = matchPassBeginning;

test/rdoc/rdoc_generator_json_index_searcher_test.rb

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,52 @@ def test_exact_method_match
8080
assert_equal 'RDoc::Markup::AttributeManager', strip_highlights(results[0]['namespace'])
8181
end
8282

83-
def test_exact_class_beats_exact_method
83+
def test_capitalized_query_prioritizes_exact_class
8484
results = run_search(
85-
query: 'attribute',
85+
query: 'String',
8686
data: {
87-
searchIndex: ['attribute()', 'attribute'],
88-
longSearchIndex: ['rdoc::markup#attribute()', 'attribute'],
87+
searchIndex: ['string', 'string()'],
88+
longSearchIndex: ['string', 'object#string()'],
8989
info: [
90-
['attribute', 'RDoc::Markup', 'RDoc/Markup.html#method-i-attribute', '()', 'Attribute method', '', 'method'],
91-
['Attribute', '', 'Attribute.html', '', 'Attribute class (hypothetical)', '', 'class']
90+
['String', '', 'String.html', '', 'String class', '', 'class'],
91+
['string', 'Object', 'Object.html#method-i-string', '()', 'String method', '', 'method']
9292
]
9393
}
9494
)
9595

9696
assert_equal 2, results.length
97-
# Exact class match (Pass 0) should beat exact method match (Pass 1)
98-
assert_equal 'Attribute', strip_highlights(results[0]['title'])
97+
# Capitalized query: exact class (Pass 0) beats exact method (Pass 1)
98+
assert_equal 'String', strip_highlights(results[0]['title'])
9999
assert_equal '', results[0]['namespace']
100-
assert_equal 'Attribute.html', results[0]['path']
100+
assert_equal 'String.html', results[0]['path']
101101

102102
# Method comes second
103-
assert_equal 'attribute', strip_highlights(results[1]['title'])
104-
assert_equal 'RDoc::Markup', strip_highlights(results[1]['namespace'])
103+
assert_equal 'string', strip_highlights(results[1]['title'])
104+
assert_equal 'Object', strip_highlights(results[1]['namespace'])
105+
end
106+
107+
def test_lowercase_query_prioritizes_method
108+
results = run_search(
109+
query: 'options',
110+
data: {
111+
searchIndex: ['options', 'options()'],
112+
longSearchIndex: ['rdoc::options', 'rdoc::codeobject#options()'],
113+
info: [
114+
['Options', 'RDoc', 'RDoc/Options.html', '', 'Options class', '', 'class'],
115+
['options', 'RDoc::CodeObject', 'RDoc/CodeObject.html#method-i-options', '()', 'Options method', '', 'method']
116+
]
117+
}
118+
)
119+
120+
assert_equal 2, results.length
121+
# Lowercase query should prioritize method over class
122+
assert_equal 'options', strip_highlights(results[0]['title'])
123+
assert_equal 'RDoc::CodeObject', strip_highlights(results[0]['namespace'])
124+
assert_equal 'RDoc/CodeObject.html#method-i-options', results[0]['path']
125+
126+
# Class comes second
127+
assert_equal 'Options', strip_highlights(results[1]['title'])
128+
assert_equal 'RDoc', strip_highlights(results[1]['namespace'])
105129
end
106130

107131
def test_beginning_match

0 commit comments

Comments
 (0)