mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 23:34:52 +02:00
8220497: Improve Javadoc search feature and add test coverage
Reviewed-by: jjg
This commit is contained in:
parent
80240eb399
commit
8322ce2e6b
12 changed files with 938 additions and 104 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -30,20 +30,16 @@ var catTypes = "Types";
|
|||
var catMembers = "Members";
|
||||
var catSearchTags = "SearchTags";
|
||||
var highlight = "<span class=\"resultHighlight\">$&</span>";
|
||||
var camelCaseRegexp = "";
|
||||
var secondaryMatcher = "";
|
||||
var searchPattern = "";
|
||||
var RANKING_THRESHOLD = 2;
|
||||
var NO_MATCH = 0xffff;
|
||||
var MAX_RESULTS_PER_CATEGORY = 500;
|
||||
function escapeHtml(str) {
|
||||
return str.replace(/</g, "<").replace(/>/g, ">");
|
||||
}
|
||||
function getHighlightedText(item) {
|
||||
var ccMatcher = new RegExp(escapeHtml(camelCaseRegexp));
|
||||
function getHighlightedText(item, matcher) {
|
||||
var escapedItem = escapeHtml(item);
|
||||
var label = escapedItem.replace(ccMatcher, highlight);
|
||||
if (label === escapedItem) {
|
||||
var secMatcher = new RegExp(escapeHtml(secondaryMatcher.source), "i");
|
||||
label = escapedItem.replace(secMatcher, highlight);
|
||||
}
|
||||
return label;
|
||||
return escapedItem.replace(matcher, highlight);
|
||||
}
|
||||
function getURLPrefix(ui) {
|
||||
var urlPrefix="";
|
||||
|
@ -64,6 +60,33 @@ function getURLPrefix(ui) {
|
|||
}
|
||||
return urlPrefix;
|
||||
}
|
||||
function makeCamelCaseRegex(term) {
|
||||
var pattern = "";
|
||||
var isWordToken = false;
|
||||
term.replace(/,\s*/g, ", ").trim().split(/\s+/).forEach(function(w, index) {
|
||||
if (index > 0) {
|
||||
// whitespace between identifiers is significant
|
||||
pattern += (isWordToken && /^\w/.test(w)) ? "\\s+" : "\\s*";
|
||||
}
|
||||
var tokens = w.split(/(?=[A-Z,.()<>[\/])/);
|
||||
for (var i = 0; i < tokens.length; i++) {
|
||||
var s = tokens[i];
|
||||
if (s === "") {
|
||||
continue;
|
||||
}
|
||||
pattern += $.ui.autocomplete.escapeRegex(s);
|
||||
isWordToken = /\w$/.test(s);
|
||||
if (isWordToken) {
|
||||
pattern += "([a-z0-9_$<>\\[\\]]*?)";
|
||||
}
|
||||
}
|
||||
});
|
||||
return pattern;
|
||||
}
|
||||
function createMatcher(pattern, flags) {
|
||||
var isCamelCase = /[A-Z]/.test(pattern);
|
||||
return new RegExp(pattern, flags + (isCamelCase ? "" : "i"));
|
||||
}
|
||||
var watermark = 'Search';
|
||||
$(function() {
|
||||
$("#search").val('');
|
||||
|
@ -93,8 +116,8 @@ $.widget("custom.catcomplete", $.ui.autocomplete, {
|
|||
this.widget().menu("option", "items", "> :not(.ui-autocomplete-category)");
|
||||
},
|
||||
_renderMenu: function(ul, items) {
|
||||
var rMenu = this,
|
||||
currentCategory = "";
|
||||
var rMenu = this;
|
||||
var currentCategory = "";
|
||||
rMenu.menu.bindings = $();
|
||||
$.each(items, function(index, item) {
|
||||
var li;
|
||||
|
@ -114,20 +137,21 @@ $.widget("custom.catcomplete", $.ui.autocomplete, {
|
|||
},
|
||||
_renderItem: function(ul, item) {
|
||||
var label = "";
|
||||
var matcher = createMatcher(escapeHtml(searchPattern), "g");
|
||||
if (item.category === catModules) {
|
||||
label = getHighlightedText(item.l);
|
||||
label = getHighlightedText(item.l, matcher);
|
||||
} else if (item.category === catPackages) {
|
||||
label = (item.m)
|
||||
? getHighlightedText(item.m + "/" + item.l)
|
||||
: getHighlightedText(item.l);
|
||||
? getHighlightedText(item.m + "/" + item.l, matcher)
|
||||
: getHighlightedText(item.l, matcher);
|
||||
} else if (item.category === catTypes) {
|
||||
label = (item.p)
|
||||
? getHighlightedText(item.p + "." + item.l)
|
||||
: getHighlightedText(item.l);
|
||||
? getHighlightedText(item.p + "." + item.l, matcher)
|
||||
: getHighlightedText(item.l, matcher);
|
||||
} else if (item.category === catMembers) {
|
||||
label = getHighlightedText(item.p + "." + (item.c + "." + item.l));
|
||||
label = getHighlightedText(item.p + "." + (item.c + "." + item.l), matcher);
|
||||
} else if (item.category === catSearchTags) {
|
||||
label = getHighlightedText(item.l);
|
||||
label = getHighlightedText(item.l, matcher);
|
||||
} else {
|
||||
label = item.l;
|
||||
}
|
||||
|
@ -146,128 +170,137 @@ $.widget("custom.catcomplete", $.ui.autocomplete, {
|
|||
return li;
|
||||
}
|
||||
});
|
||||
function rankMatch(match, category) {
|
||||
if (!match) {
|
||||
return NO_MATCH;
|
||||
}
|
||||
var index = match.index;
|
||||
var input = match.input;
|
||||
var leftBoundaryMatch = 2;
|
||||
var periferalMatch = 0;
|
||||
var delta = 0;
|
||||
// make sure match is anchored on a left word boundary
|
||||
if (index === 0 || /\W/.test(input[index - 1]) || "_" === input[index - 1] || "_" === input[index]) {
|
||||
leftBoundaryMatch = 0;
|
||||
} else if (input[index] === input[index].toUpperCase() && !/^[A-Z0-9_$]+$/.test(input)) {
|
||||
leftBoundaryMatch = 1;
|
||||
}
|
||||
var matchEnd = index + match[0].length;
|
||||
var leftParen = input.indexOf("(");
|
||||
// exclude peripheral matches
|
||||
if (category !== catModules && category !== catSearchTags) {
|
||||
var endOfName = leftParen > -1 ? leftParen : input.length;
|
||||
var delim = category === catPackages ? "/" : ".";
|
||||
if (leftParen > -1 && leftParen < index) {
|
||||
periferalMatch += 2;
|
||||
} else if (input.lastIndexOf(delim, endOfName) >= matchEnd) {
|
||||
periferalMatch += 2;
|
||||
}
|
||||
}
|
||||
for (var i = 1; i < match.length; i++) {
|
||||
// lower ranking if parts of the name are missing
|
||||
if (match[i])
|
||||
delta += match[i].length;
|
||||
}
|
||||
if (category === catTypes) {
|
||||
// lower ranking if a type name contains unmatched camel-case parts
|
||||
if (/[A-Z]/.test(input.substring(matchEnd)))
|
||||
delta += 5;
|
||||
if (/[A-Z]/.test(input.substring(0, index)))
|
||||
delta += 5;
|
||||
}
|
||||
return leftBoundaryMatch + periferalMatch + (delta / 200);
|
||||
|
||||
}
|
||||
$(function() {
|
||||
$("#search").catcomplete({
|
||||
minLength: 1,
|
||||
delay: 300,
|
||||
source: function(request, response) {
|
||||
var result = new Array();
|
||||
var presult = new Array();
|
||||
var tresult = new Array();
|
||||
var mresult = new Array();
|
||||
var tgresult = new Array();
|
||||
var secondaryresult = new Array();
|
||||
var displayCount = 0;
|
||||
var exactMatcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term) + "$", "i");
|
||||
camelCaseRegexp = ($.ui.autocomplete.escapeRegex(request.term)).split(/(?=[A-Z])/).join("([a-z0-9_$]*?)");
|
||||
var camelCaseMatcher = new RegExp("^" + camelCaseRegexp);
|
||||
secondaryMatcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
|
||||
var result = [];
|
||||
var newResults = [];
|
||||
|
||||
// Return the nested innermost name from the specified object
|
||||
function nestedName(e) {
|
||||
return e.l.substring(e.l.lastIndexOf(".") + 1);
|
||||
searchPattern = makeCamelCaseRegex(request.term);
|
||||
if (searchPattern === "") {
|
||||
return this.close();
|
||||
}
|
||||
var camelCaseMatcher = createMatcher(searchPattern, "");
|
||||
var boundaryMatcher = createMatcher("\\b" + searchPattern, "");
|
||||
|
||||
function concatResults(a1, a2) {
|
||||
a1 = a1.concat(a2);
|
||||
a2.sort(function(e1, e2) {
|
||||
return e1.ranking - e2.ranking;
|
||||
});
|
||||
a1 = a1.concat(a2.map(function(e) { return e.item; }));
|
||||
a2.length = 0;
|
||||
return a1;
|
||||
}
|
||||
|
||||
if (moduleSearchIndex) {
|
||||
var mdleCount = 0;
|
||||
$.each(moduleSearchIndex, function(index, item) {
|
||||
item.category = catModules;
|
||||
if (exactMatcher.test(item.l)) {
|
||||
result.push(item);
|
||||
mdleCount++;
|
||||
} else if (camelCaseMatcher.test(item.l)) {
|
||||
result.push(item);
|
||||
} else if (secondaryMatcher.test(item.l)) {
|
||||
secondaryresult.push(item);
|
||||
var ranking = rankMatch(boundaryMatcher.exec(item.l), catModules);
|
||||
if (ranking < RANKING_THRESHOLD) {
|
||||
newResults.push({ ranking: ranking, item: item });
|
||||
}
|
||||
return newResults.length < MAX_RESULTS_PER_CATEGORY;
|
||||
});
|
||||
displayCount = mdleCount;
|
||||
result = concatResults(result, secondaryresult);
|
||||
result = concatResults(result, newResults);
|
||||
}
|
||||
if (packageSearchIndex) {
|
||||
var pCount = 0;
|
||||
var pkg = "";
|
||||
$.each(packageSearchIndex, function(index, item) {
|
||||
item.category = catPackages;
|
||||
pkg = (item.m)
|
||||
var name = (item.m && request.term.indexOf("/") > -1)
|
||||
? (item.m + "/" + item.l)
|
||||
: item.l;
|
||||
if (exactMatcher.test(item.l)) {
|
||||
presult.push(item);
|
||||
pCount++;
|
||||
} else if (camelCaseMatcher.test(pkg)) {
|
||||
presult.push(item);
|
||||
} else if (secondaryMatcher.test(pkg)) {
|
||||
secondaryresult.push(item);
|
||||
var ranking = rankMatch(boundaryMatcher.exec(name), catPackages);
|
||||
if (ranking < RANKING_THRESHOLD) {
|
||||
newResults.push({ ranking: ranking, item: item });
|
||||
}
|
||||
return newResults.length < MAX_RESULTS_PER_CATEGORY;
|
||||
});
|
||||
result = result.concat(concatResults(presult, secondaryresult));
|
||||
displayCount = (pCount > displayCount) ? pCount : displayCount;
|
||||
result = concatResults(result, newResults);
|
||||
}
|
||||
if (typeSearchIndex) {
|
||||
var tCount = 0;
|
||||
$.each(typeSearchIndex, function(index, item) {
|
||||
item.category = catTypes;
|
||||
var s = nestedName(item);
|
||||
if (exactMatcher.test(s)) {
|
||||
tresult.push(item);
|
||||
tCount++;
|
||||
} else if (camelCaseMatcher.test(s)) {
|
||||
tresult.push(item);
|
||||
} else if (secondaryMatcher.test(item.p + "." + item.l)) {
|
||||
secondaryresult.push(item);
|
||||
var name = request.term.indexOf(".") > -1
|
||||
? item.p + "." + item.l
|
||||
: item.l;
|
||||
var ranking = rankMatch(camelCaseMatcher.exec(name), catTypes);
|
||||
if (ranking < RANKING_THRESHOLD) {
|
||||
newResults.push({ ranking: ranking, item: item });
|
||||
}
|
||||
return newResults.length < MAX_RESULTS_PER_CATEGORY;
|
||||
});
|
||||
result = result.concat(concatResults(tresult, secondaryresult));
|
||||
displayCount = (tCount > displayCount) ? tCount : displayCount;
|
||||
result = concatResults(result, newResults);
|
||||
}
|
||||
if (memberSearchIndex) {
|
||||
var mCount = 0;
|
||||
$.each(memberSearchIndex, function(index, item) {
|
||||
item.category = catMembers;
|
||||
var s = nestedName(item);
|
||||
if (exactMatcher.test(s)) {
|
||||
mresult.push(item);
|
||||
mCount++;
|
||||
} else if (camelCaseMatcher.test(s)) {
|
||||
mresult.push(item);
|
||||
} else if (secondaryMatcher.test(item.c + "." + item.l)) {
|
||||
secondaryresult.push(item);
|
||||
var name = request.term.indexOf(".") > -1
|
||||
? item.p + "." + item.c + "." + item.l
|
||||
: item.l;
|
||||
var ranking = rankMatch(camelCaseMatcher.exec(name), catMembers);
|
||||
if (ranking < RANKING_THRESHOLD) {
|
||||
newResults.push({ ranking: ranking, item: item });
|
||||
}
|
||||
return newResults.length < MAX_RESULTS_PER_CATEGORY;
|
||||
});
|
||||
result = result.concat(concatResults(mresult, secondaryresult));
|
||||
displayCount = (mCount > displayCount) ? mCount : displayCount;
|
||||
result = concatResults(result, newResults);
|
||||
}
|
||||
if (tagSearchIndex) {
|
||||
var tgCount = 0;
|
||||
$.each(tagSearchIndex, function(index, item) {
|
||||
item.category = catSearchTags;
|
||||
if (exactMatcher.test(item.l)) {
|
||||
tgresult.push(item);
|
||||
tgCount++;
|
||||
} else if (secondaryMatcher.test(item.l)) {
|
||||
secondaryresult.push(item);
|
||||
var ranking = rankMatch(boundaryMatcher.exec(item.l), catSearchTags);
|
||||
if (ranking < RANKING_THRESHOLD) {
|
||||
newResults.push({ ranking: ranking, item: item });
|
||||
}
|
||||
return newResults.length < MAX_RESULTS_PER_CATEGORY;
|
||||
});
|
||||
result = result.concat(concatResults(tgresult, secondaryresult));
|
||||
displayCount = (tgCount > displayCount) ? tgCount : displayCount;
|
||||
result = concatResults(result, newResults);
|
||||
}
|
||||
displayCount = (displayCount > 500) ? displayCount : 500;
|
||||
var counter = function() {
|
||||
var count = {Modules: 0, Packages: 0, Types: 0, Members: 0, SearchTags: 0};
|
||||
var f = function(item) {
|
||||
count[item.category] += 1;
|
||||
return (count[item.category] <= displayCount);
|
||||
};
|
||||
return f;
|
||||
}();
|
||||
response(result.filter(counter));
|
||||
response(result);
|
||||
},
|
||||
response: function(event, ui) {
|
||||
if (!ui.content.length) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue