Fix highlighting in suggestions with full text search

If full text search for reviewer suggestion is enabled, the matched
substrings were not highlighted in the suggestions when the query
consisted of several words. E.g. the query 'jo do' would find
'John Doe <john.doe@example.com>', but 'Jo'/'jo' and 'Do'/'do' were
not highlighted in the suggestion.

For the highlighting of the query terms in the suggestions query terms
which are substrings of other query terms are filtered out, so that
decoration is not done several times on the same substring.

Change-Id: Iffae33bd52a24593589722f4cbb2dee8139ff73e
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2016-07-07 08:28:47 +02:00
parent c69c6eecc3
commit cdfd4d3f44

View File

@@ -17,6 +17,10 @@ package com.google.gwtexpui.safehtml.client;
import com.google.gwt.user.client.ui.SuggestOracle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* A suggestion oracle that tries to highlight the matched text.
@@ -56,7 +60,7 @@ public abstract class HighlightSuggestOracle extends SuggestOracle {
}
protected String getQueryPattern(final String query) {
return "(" + escape(query) + ")";
return query;
}
/**
@@ -84,19 +88,51 @@ public abstract class HighlightSuggestOracle extends SuggestOracle {
ds = escape(ds);
}
// We now surround qstr by <strong>. But the chosen approach is not too
// smooth, if qstr is small (e.g.: "t") and this small qstr may occur in
// escapes (e.g.: "Tim &lt;email@example.org&gt;"). Those escapes will
// get <strong>-ed as well (e.g.: "&lt;" -> "&<strong>l</strong>t;"). But
// as repairing those mangled escapes is easier than not mangling them in
// the first place, we repair them afterwards.
ds = sgi(ds, qstr, "<strong>$1</strong>");
for (String qterm : splitQuery(qstr)) {
qterm = "(" + escape(qterm) + ")";
// We now surround qstr by <strong>. But the chosen approach is not too
// smooth, if qstr is small (e.g.: "t") and this small qstr may occur in
// escapes (e.g.: "Tim &lt;email@example.org&gt;"). Those escapes will
// get <strong>-ed as well (e.g.: "&lt;" -> "&<strong>l</strong>t;"). But
// as repairing those mangled escapes is easier than not mangling them in
// the first place, we repair them afterwards.
ds = sgi(ds, qterm, "<strong>$1</strong>");
}
// Repairing <strong>-ed escapes.
ds = sgi(ds, "(&[a-z]*)<strong>([a-z]*)</strong>([a-z]*;)", "$1$2$3");
displayString = ds;
}
/**
* Split the query by whitespace and filter out query terms which are
* substrings of other query terms.
*/
private static List<String> splitQuery(String query) {
List<String> queryTerms = Arrays.asList(query.split("\\s+"));
Collections.sort(queryTerms, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return Integer.compare(s2.length(), s1.length());
}});
List<String> result = new ArrayList<>();
for (String s : queryTerms) {
boolean add = true;
for (String queryTerm : result) {
if (queryTerm.toLowerCase().contains(s.toLowerCase())) {
add = false;
break;
}
}
if (add) {
result.add(s);
}
}
return result;
}
private static native String sgi(String inString, String pat, String newHtml)
/*-{ return inString.replace(RegExp(pat, 'gi'), newHtml); }-*/;