Skip to content

Instantly share code, notes, and snippets.

@Altai-man
Created March 7, 2021 22:08
Show Gist options
  • Save Altai-man/7a723dded0c9a758e9c1bb5acf8f6095 to your computer and use it in GitHub Desktop.
Save Altai-man/7a723dded0c9a758e9c1bb5acf8f6095 to your computer and use it in GitHub Desktop.
Index: templates/search_template.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- templates/search_template.js (revision cb5268de29e8692d71fb7ef3b720fd41bfe28b7d)
+++ templates/search_template.js (date 1615154815552)
@@ -1,6 +1,44 @@
-//WARNING
var current_search = "";
-
+var category_search = (function() {
+ var method_sign = new RegExp(/^(\.)(\w[\w\-]+)/);
+ var routine_sign = new RegExp(/^(\&)(\w[\w-]+.*)/);
+ var routineMethod_sign = new RegExp(/([^\(]+)(\(\)?)$/);
+ var classPackageRole_sign = new RegExp(/^(\:\:)([A-Z][\w\:]*)/);
+ return {
+ filter_by_category: function(search_term, items) {
+ var filteredItems = [];
+ if (search_term.match(method_sign)) {
+ filteredItems = items.filter(function(item) { return item.category.toLowerCase() === 'methods' || item.category.toLowerCase() === 'routines' });
+ } else if (search_term.match(routine_sign)){
+ filteredItems = items.filter(function(item) { return item.category.toLowerCase() === 'subroutines' || item.category.toLowerCase() === 'routines' });
+ } else if (search_term.match(routineMethod_sign)) {
+ filteredItems = items.filter(function(item) { return item.category.toLowerCase() === 'methods' || item.category.toLowerCase() === 'subroutines' || item.category.toLowerCase() === 'routines' });
+ } else if (search_term.match(classPackageRole_sign)) {
+ filteredItems = items.filter(function(item) { return item.category.toLowerCase() === 'types' });
+ } else {
+ filteredItems = items;
+ }
+ return filteredItems;
+ },
+ strip_sign: function(search_term) {
+ var match;
+ if (search_term.match(method_sign)) {
+ // We matched `.`, strip it off
+ search_term = search_term.substring(1);
+ } else if (search_term.match(routine_sign)) {
+ // We matched a &, strip it off
+ search_term = search_term.replace('&', '');
+ } else if (search_term.match(routineMethod_sign)) {
+ // We matched (), strip it off
+ search_term = search_term.replace(/[()]/g, '');
+ } else if (search_term.match(classPackageRole_sign)) {
+ // We matched ::, strip it off
+ search_term = search_term.replace('::', '');
+ }
+ return search_term;
+ }
+ }
+})();
$(function(){
$.widget( "custom.catcomplete", $.ui.autocomplete, {
_create: function() {
@@ -8,17 +46,43 @@
this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
},
_renderItem: function( ul, item) {
+ var enter_text = $('<span>')
+ .attr('class', 'enter-prompt')
+ .css('display', 'none')
+ .html('Enter to select');
var regex = new RegExp('('
+ current_search.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
+ ')', 'ig');
var text = item.label.replace(regex, '<b>$1</b>');
+ var boldMatch = text.match(/^<b>.*?<\/b>$/);
+ if (boldMatch && boldMatch[0].match(/<b>/g).length === 1) {
+ $('#navbar-search').attr('data-default-url', item.url);
+ enter_text.css('display', 'inline');
+ enter_text.addClass('default-selection');
+ } else if (item.category === 'Site Search') {
+ $('#navbar-search').attr('data-search-url', item.url);
+ enter_text.css('display', 'inline');
+ enter_text.addClass('default-selection');
+ }
return $( "<li>" )
- .append( $( "<div>" ).html(text) )
- .appendTo( ul );
+ .append( $( "<div>" ).html(text).append(enter_text) )
+ .appendTo( ul )
+ .hover(
+ function() {
+ $('#navbar-search .enter-prompt:visible').hide();
+ $(this).find('.enter-prompt').show()
+ },
+ function() {
+ $(this).find('.enter-prompt').hide();
+ $('#navbar-search .default-selection').show();
+ }
+ )
},
_renderMenu: function( ul, items ) {
var that = this,
currentCategory = "";
+ $('#navbar-search').attr('data-default-url', '');
+ $('#navbar-search').attr('data-search-url', '');
function sortBy(a, b) {
// We want to place 5to6 docs to the end of the list.
// See if either a or b are in 5to6 category.
@@ -62,7 +126,9 @@
return 0;
}
var sortedItems = items.sort(sortBy);
- var keywords = $("#query").val();
+ console.log($("#query").val());
+ var keywords = category_search.strip_sign($("#query").val());
+ console.log(keywords);
sortedItems.push({
category: 'Site Search',
label: "Search the entire site for " + keywords,
@@ -80,6 +146,11 @@
li.attr( "aria-label", item.category + " : " + item.label );
}
});
+ if ($(ul).find('.default-selection').length > 1) {
+ $(ul).find('.default-selection').not(":first")
+ .removeClass('default-selection')
+ .css({'display': 'none'});
+ };
}
});
var searchResults;
@@ -119,7 +190,8 @@
value: ";; (long name)",
url: "/type/Signature#index-entry-Long_Names"
}, ITEMS ];
- var results = $.ui.autocomplete.filter(items, request.term);
+ items = category_search.filter_by_category(request.term, items);
+ var results = $.ui.autocomplete.filter(items, category_search.strip_sign(request.term));
function trim_results(results, term) {
var cutoff = 50;
if (results.length < cutoff) {
@@ -161,27 +233,34 @@
};
response(trim_results(results, request.term));
},
- select: function (event, ui) { window.location.href = ui.item.url; },
+ select: function (event, ui) {
+ $('#navbar-search').attr('data-default-url', ui.item.url);
+ followLink();
+ },
});
- $("#query").keydown(function(event) {
+ $("#query").keydown(function(event, ui) {
if (event.keyCode == 13) {
- let searchText = $('#query').val();
- let matchedItems = [];
- for (var i = 0; i < searchResults.length; i++) {
- if (searchResults[i].category === "Site Search")
- continue;
- if (searchResults[i].value === searchText || searchResults[i].value.toLowerCase() === searchText.toLowerCase())
- matchedItems.push(searchResults[i]);
- }
- if (matchedItems.length === 1) {
- window.location.href = matchedItems[0].url;
- } else {
- window.location.href = "/search#q=" + searchText;
- }
+ followLink();
}
});
});
+
+var followLink = function() {
+ /* When using return key to select, the select event
+ and keydown event are both activated and the second
+ event should do nothing */
+ var url;
+ if ($('#navbar-search').attr('data-default-url')) {
+ url = $('#navbar-search').attr('data-default-url');
+ $('#navbar-search').attr('data-default-url', '');
+ $('#navbar-search').attr('data-search-url', '');
+ window.location.href = url;
+ } else if ($('#navbar-search').attr('data-search-url')) {
+ url = $('#navbar-search').attr('data-search-url');
+ window.location.href = url;
+ }
+}
/*
* allow for inexact searching via sift4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment