Display search result fixes (#1901)

Fix syntax highlighting on non highlighted fields. Fix ellipsis on new lines in code syntax highlighting. Fix ellipsis on content start or end in non code fields.

Co-authored-by: Sebastian Sdorra <sebastian.sdorra@cloudogu.com>
This commit is contained in:
Matthias Thieroff
2021-12-21 15:10:08 +01:00
committed by GitHub
parent 5b700dc0c7
commit bc86ed4474
8 changed files with 455 additions and 82 deletions

View File

@@ -123,6 +123,24 @@ class LuceneHighlighterTest {
assertThat(contentFragments[0].isMatchesContentEnd()).isTrue();
}
@Test
void shouldMatchContentStartWithDefaultAnalyzer() throws InvalidTokenOffsetsException, IOException {
ContentFragment[] contentFragments = highlight("GameOfLife.java", "gameoflife");
assertThat(contentFragments).hasSize(1);
assertThat(contentFragments[0].isMatchesContentStart()).isTrue();
assertThat(contentFragments[0].isMatchesContentEnd()).isFalse();
}
@Test
void shouldMatchContentEndWithDefaultAnalyzer() throws InvalidTokenOffsetsException, IOException {
ContentFragment[] contentFragments = highlight("Button.tsx", "default");
assertThat(contentFragments).hasSize(1);
assertThat(contentFragments[0].isMatchesContentStart()).isFalse();
assertThat(contentFragments[0].isMatchesContentEnd()).isTrue();
}
@Nested
class IsHighlightableTests {
@@ -162,6 +180,16 @@ class LuceneHighlighterTest {
}
private ContentFragment[] highlight(String resource, String search) throws IOException, InvalidTokenOffsetsException {
StandardAnalyzer analyzer = new StandardAnalyzer();
Query query = new TermQuery(new Term("content", search));
String content = content(resource);
LuceneHighlighter highlighter = new LuceneHighlighter(analyzer, query);
return highlighter.highlight("content", Indexed.Analyzer.DEFAULT, content);
}
private ContentFragment[] highlightCode(String resource, String search) throws IOException, InvalidTokenOffsetsException {
NonNaturalLanguageAnalyzer analyzer = new NonNaturalLanguageAnalyzer();
Query query = new TermQuery(new Term("content", search));