8141636: Javadoc search should support camelCase search
authorbpatel
Thu, 13 Oct 2016 10:48:09 -0700
changeset 41523 da4f500b886d
parent 41522 2fb1e2da56ca
child 41524 4c65c407ee8a
8141636: Javadoc search should support camelCase search Reviewed-by: jjg
langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/resources/search.js
langtools/test/jdk/javadoc/doclet/testSearch/TestSearch.java
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/resources/search.js	Wed Oct 12 17:27:54 2016 -0700
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/resources/search.js	Thu Oct 13 10:48:09 2016 -0700
@@ -30,6 +30,9 @@
 var catTypes = "Types";
 var catMembers = "Members";
 var catSearchTags = "SearchTags";
+var highlight = "<span class=\"resultHighlight\">$&</span>";
+var camelCaseRegexp = "";
+var secondaryMatcher = "";
 function getName(name) {
     var anchor = "";
     var ch = '';
@@ -65,27 +68,35 @@
     }
     return anchor;
 }
+function getHighlightedText(item) {
+    var ccMatcher = new RegExp(camelCaseRegexp);
+    var label = item.replace(ccMatcher, highlight);
+    if (label === item) {
+        label = item.replace(secondaryMatcher, highlight);
+    }
+    return label;
+}
 var watermark = 'Search';
 $(function() {
     $("#search").prop("disabled", false);
     $("#reset").prop("disabled", false);
     $("#search").val(watermark).addClass('watermark');
-    $("#search").blur(function(){
+    $("#search").blur(function() {
         if ($(this).val().length == 0) {
             $(this).val(watermark).addClass('watermark');
         }
     });
-    $("#search").keydown(function(){
-       if ($(this).val() == watermark) {
+    $("#search").keydown(function() {
+        if ($(this).val() == watermark) {
             $(this).val('').removeClass('watermark');
         }
     });
-    $("#reset").click(function(){
-       $("#search").val('');
-       $("#search").focus();
+    $("#reset").click(function() {
+        $("#search").val('');
+        $("#search").focus();
     });
     $("#search").focus();
-    $("#search")[0].setSelectionRange(0,0);
+    $("#search")[0].setSelectionRange(0, 0);
 });
 $.widget("custom.catcomplete", $.ui.autocomplete, {
     _create: function() {
@@ -112,22 +123,19 @@
         });
     },
     _renderItem: function(ul, item) {
-        var result = this.element.val();
-        var regexp = new RegExp($.ui.autocomplete.escapeRegex(result), "i");
-        highlight = "<span class=\"resultHighlight\">$&</span>";
         var label = "";
         if (item.category === catModules) {
-            label = item.l.replace(regexp, highlight);
+            label = getHighlightedText(item.l);
         } else if (item.category === catPackages) {
             label = (item.m)
-                    ? (item.m + "/" + item.l).replace(regexp, highlight)
-                    : item.l.replace(regexp, highlight);
+                    ? getHighlightedText(item.m + "/" + item.l)
+                    : getHighlightedText(item.l);
         } else if (item.category === catTypes) {
-            label += (item.p + "." + item.l).replace(regexp, highlight);
+            label = getHighlightedText(item.p + "." + item.l);
         } else if (item.category === catMembers) {
-            label += item.p + "." + (item.c + "." + item.l).replace(regexp, highlight);
+            label = getHighlightedText(item.p + "." + (item.c + "." + item.l));
         } else if (item.category === catSearchTags) {
-            label = item.l.replace(regexp, highlight);
+            label = getHighlightedText(item.l);
         } else {
             label = item.l;
         }
@@ -163,7 +171,9 @@
             var tgresult = new Array();
             var displayCount = 0;
             var exactMatcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term) + "$", "i");
-            var secondaryMatcher = 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");
             if (moduleSearchIndex) {
                 var mdleCount = 0;
                 $.each(moduleSearchIndex, function(index, item) {
@@ -171,6 +181,8 @@
                     if (exactMatcher.test(item.l)) {
                         result.unshift(item);
                         mdleCount++;
+                    } else if (camelCaseMatcher.test(item.l)) {
+                        result.unshift(item);
                     } else if (secondaryMatcher.test(item.l)) {
                         result.push(item);
                     }
@@ -188,6 +200,8 @@
                     if (exactMatcher.test(item.l)) {
                         presult.unshift(item);
                         pCount++;
+                    } else if (camelCaseMatcher.test(pkg)) {
+                        presult.unshift(item);
                     } else if (secondaryMatcher.test(pkg)) {
                         presult.push(item);
                     }
@@ -202,6 +216,8 @@
                     if (exactMatcher.test(item.l)) {
                         tresult.unshift(item);
                         tCount++;
+                    } else if (camelCaseMatcher.test(item.l)) {
+                        tresult.unshift(item);
                     } else if (secondaryMatcher.test(item.p + "." + item.l)) {
                         tresult.push(item);
                     }
@@ -216,6 +232,8 @@
                     if (exactMatcher.test(item.l)) {
                         mresult.unshift(item);
                         mCount++;
+                    } else if (camelCaseMatcher.test(item.l)) {
+                        mresult.unshift(item);
                     } else if (secondaryMatcher.test(item.c + "." + item.l)) {
                         mresult.push(item);
                     }
@@ -294,4 +312,4 @@
             }
         }
     });
-});
+});
\ No newline at end of file
--- a/langtools/test/jdk/javadoc/doclet/testSearch/TestSearch.java	Wed Oct 12 17:27:54 2016 -0700
+++ b/langtools/test/jdk/javadoc/doclet/testSearch/TestSearch.java	Thu Oct 13 10:48:09 2016 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8141492 8071982
+ * @bug 8141492 8071982 8141636
  * @summary Test the search feature of javadoc.
  * @author bpatel
  * @library ../lib
@@ -45,6 +45,7 @@
         checkExit(Exit.OK);
         checkSearchOutput("UnnamedPkgClass.html", true);
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(false,
                 "package-search-index.zip",
                 "tag-search-index.zip");
@@ -62,6 +63,7 @@
         checkSearchOutput(true);
         checkSingleIndex(true);
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(true,
                 "member-search-index.zip",
                 "package-search-index.zip",
@@ -78,6 +80,7 @@
         checkSearchOutput(true);
         checkSingleIndex(true);
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(true,
                 "member-search-index.zip",
                 "package-search-index.zip",
@@ -110,6 +113,7 @@
         checkSearchOutput(true);
         checkSingleIndex(true);
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(true,
                 "member-search-index.zip",
                 "package-search-index.zip",
@@ -142,6 +146,7 @@
         checkSearchOutput(true);
         checkIndexNoComment();
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(true,
                 "member-search-index.zip",
                 "package-search-index.zip",
@@ -158,6 +163,7 @@
         checkSearchOutput(true);
         checkIndexNoDeprecated();
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(true,
                 "member-search-index.zip",
                 "package-search-index.zip",
@@ -174,6 +180,7 @@
         checkSearchOutput(true);
         checkSplitIndex();
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(true,
                 "member-search-index.zip",
                 "package-search-index.zip",
@@ -189,6 +196,7 @@
         checkSearchOutput(true);
         checkJavaFXOutput();
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(false,
                 "tag-search-index.zip");
         checkFiles(true,
@@ -420,4 +428,11 @@
                 "resources/x.png",
                 "resources/glass.png");
     }
+
+    void checkSearchJS() {
+        checkOutput("search.js", true,
+                "camelCaseRegexp = ($.ui.autocomplete.escapeRegex(request.term)).split(/(?=[A-Z])/).join(\"([a-z0-9_$]*?)\");",
+                "var camelCaseMatcher = new RegExp(\"^\" + camelCaseRegexp);",
+                "camelCaseMatcher.test(item.l)");
+    }
 }