Repository navigation
[Port to stable] Restore full-text results in the entry editor #17370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
33c3b01
BACKPORT-CONFLICT
Siedlerchr 9ca1dd5
Merge remote-tracking branch 'upstream/stable' into port-17367-to-stable
github-actions[bot] 69d8d81
Resolve requirement markers in full-text port
19bf79e
Merge branch 'stable' into pr-17370
e1d7669
Adapt full-text result test to stable highlighter API
Siedlerchr 5f61419
Declare Lucene highlighter for GUI tests
Siedlerchr 9702444
Merge remote-tracking branch 'upstream/stable' into pr-17370
Siedlerchr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,10 @@ | |
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import javafx.beans.binding.Bindings; | ||
| import javafx.beans.value.ChangeListener; | ||
| import javafx.beans.value.ObservableValue; | ||
| import javafx.geometry.Insets; | ||
| import javafx.geometry.Orientation; | ||
|
|
@@ -18,6 +20,7 @@ | |
| import javafx.scene.text.TextFlow; | ||
|
|
||
| import org.jabref.gui.DialogService; | ||
| import org.jabref.gui.LibraryTab; | ||
| import org.jabref.gui.StateManager; | ||
| import org.jabref.gui.actions.ActionFactory; | ||
| import org.jabref.gui.actions.StandardActions; | ||
|
|
@@ -36,16 +39,22 @@ | |
| import org.jabref.model.entry.BibEntry; | ||
| import org.jabref.model.entry.LinkedFile; | ||
| import org.jabref.model.search.SearchFlags; | ||
| import org.jabref.model.search.query.SearchQuery; | ||
| import org.jabref.model.search.query.SearchResult; | ||
| import org.jabref.model.search.query.SearchResults; | ||
|
|
||
| import org.jspecify.annotations.NullMarked; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class FulltextSearchResultsTab extends EntryEditorTab { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(FulltextSearchResultsTab.class); | ||
|
|
||
| @NullMarked | ||
| private record SearchDisplay(BibEntry entry, SearchQuery query, SearchResults results) { | ||
| } | ||
|
|
||
| private final StateManager stateManager; | ||
| private final GuiPreferences preferences; | ||
| private final DialogService dialogService; | ||
|
|
@@ -58,6 +67,13 @@ public class FulltextSearchResultsTab extends EntryEditorTab { | |
|
|
||
| /// Content available only while an active, valid fulltext search query exists. | ||
| private final ObservableValue<Boolean> contentVisibility; | ||
| private final ChangeListener<Optional<SearchResults>> searchResultsListener = (_, _, _) -> updateSearch(); | ||
| private final ChangeListener<Optional<SearchQuery>> searchQueryListener; | ||
| private final ChangeListener<Optional<LibraryTab>> activeTabListener = (_, previous, current) -> { | ||
| previous.ifPresent(tab -> tab.searchResultsProperty().removeListener(searchResultsListener)); | ||
| current.ifPresent(tab -> tab.searchResultsProperty().addListener(searchResultsListener)); | ||
| updateSearch(); | ||
| }; | ||
|
|
||
| public FulltextSearchResultsTab(StateManager stateManager, | ||
| GuiPreferences preferences, | ||
|
|
@@ -77,14 +93,25 @@ public FulltextSearchResultsTab(StateManager stateManager, | |
| setContentDrivenVisibility(contentVisibility); | ||
|
|
||
| content = new TextFlow(); | ||
| searchQueryListener = (_, _, _) -> content.getChildren().clear(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Library switches erase full-text hits searchQueryListener clears the editor content on every active-query change without restoring results already cached for the newly active library. When switching to a library whose own query and results are already set, the active-tab change can render those hits first, then query synchronization clears them without triggering another search-results update. Agent Prompt
|
||
| ScrollPane scrollPane = new ScrollPane(content); | ||
| scrollPane.setFitToWidth(true); | ||
| content.setPadding(new Insets(10)); | ||
| setContent(scrollPane); | ||
| setText(EntryEditorTabModel.BuiltIn.FULLTEXT_SEARCH_RESULTS.displayName()); | ||
|
|
||
| // Rebinding is necessary because of re-rendering of highlighting of matched text | ||
| stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH).addListener((_, _, _) -> updateSearch()); | ||
| stateManager.activeTabProperty().get() | ||
| .ifPresent(tab -> tab.searchResultsProperty().addListener(searchResultsListener)); | ||
| stateManager.activeTabProperty().addListener(activeTabListener); | ||
| stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH).addListener(searchQueryListener); | ||
| } | ||
|
|
||
| @Override | ||
| protected void dispose() { | ||
| stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH).removeListener(searchQueryListener); | ||
| stateManager.activeTabProperty().removeListener(activeTabListener); | ||
| stateManager.activeTabProperty().get() | ||
| .ifPresent(tab -> tab.searchResultsProperty().removeListener(searchResultsListener)); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -96,41 +123,46 @@ protected void bindToEntry(BibEntry entry) { | |
| updateSearch(); | ||
| } | ||
|
|
||
| // [impl->req~jabgui.search.fulltext.entry-editor-results~1] | ||
| private void updateSearch() { | ||
| stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH).get().ifPresent(searchQuery -> { | ||
| SearchResults searchResults = searchQuery.getSearchResults(); | ||
| if (searchResults != null && entry != null) { | ||
| Map<String, List<SearchResult>> searchResultsForEntry = searchResults.getFileSearchResultsForEntry(entry); | ||
| content.getChildren().clear(); | ||
| if (searchResultsForEntry.isEmpty()) { | ||
| content.getChildren().add(new Text(Localization.lang("No search matches."))); | ||
| } else { | ||
| // Iterate through files with search hits | ||
| for (Map.Entry<String, List<SearchResult>> iterator : searchResultsForEntry.entrySet()) { | ||
| entry.getFiles().stream().filter(file -> file.getLink().equals(iterator.getKey())).findFirst().ifPresent(linkedFile -> { | ||
| content.getChildren().addAll(createFileLink(linkedFile), lineSeparator()); | ||
| // Iterate through pages (within file) with search hits | ||
| for (SearchResult searchResult : iterator.getValue()) { | ||
| for (String resultTextHtml : searchResult.getContentResultStringsHtml()) { | ||
| content.getChildren().addAll(TooltipTextUtil.createTextsFromHtml(resultTextHtml.replace("</b> <b>", " "))); | ||
| content.getChildren().addAll(new Text(System.lineSeparator()), lineSeparator(0.8), createPageLink(linkedFile, searchResult.getPageNumber(), searchQuery.getSearchExpression())); | ||
| } | ||
| if (!searchResult.getAnnotationsResultStringsHtml().isEmpty()) { | ||
| Text annotationsText = new Text(System.lineSeparator() + Localization.lang("Found matches in annotations:") + System.lineSeparator() + System.lineSeparator()); | ||
| annotationsText.getStyleClass().add("italic"); | ||
| content.getChildren().add(annotationsText); | ||
|
|
||
| for (String resultTextHtml : searchResult.getAnnotationsResultStringsHtml()) { | ||
| content.getChildren().addAll(TooltipTextUtil.createTextsFromHtml(resultTextHtml.replace("</b> <b>", " "))); | ||
| content.getChildren().addAll(new Text(System.lineSeparator()), lineSeparator(0.8), createPageLink(linkedFile, searchResult.getPageNumber(), searchQuery.getSearchExpression())); | ||
| } | ||
| } | ||
| content.getChildren().clear(); | ||
| Optional.ofNullable(entry) | ||
| .flatMap(selectedEntry -> stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH).get() | ||
| .flatMap(searchQuery -> stateManager.activeTabProperty().get() | ||
| .flatMap(tab -> tab.searchResultsProperty().get()) | ||
| .map(results -> new SearchDisplay(selectedEntry, searchQuery, results)))) | ||
| .ifPresent(display -> renderResults(display.entry(), display.query(), display.results())); | ||
| } | ||
|
|
||
| private void renderResults(BibEntry selectedEntry, SearchQuery searchQuery, SearchResults searchResults) { | ||
| Map<String, List<SearchResult>> searchResultsForEntry = searchResults.getFileSearchResultsForEntry(selectedEntry); | ||
| if (searchResultsForEntry.isEmpty()) { | ||
| content.getChildren().add(new Text(Localization.lang("No search matches."))); | ||
| } else { | ||
| // Iterate through files with search hits | ||
| for (Map.Entry<String, List<SearchResult>> iterator : searchResultsForEntry.entrySet()) { | ||
| selectedEntry.getFiles().stream().filter(file -> file.getLink().equals(iterator.getKey())).findFirst().ifPresent(linkedFile -> { | ||
| content.getChildren().addAll(createFileLink(linkedFile), lineSeparator()); | ||
| // Iterate through pages (within file) with search hits | ||
| for (SearchResult searchResult : iterator.getValue()) { | ||
| for (String resultTextHtml : searchResult.getContentResultStringsHtml()) { | ||
| content.getChildren().addAll(TooltipTextUtil.createTextsFromHtml(resultTextHtml.replace("</b> <b>", " "))); | ||
| content.getChildren().addAll(new Text(System.lineSeparator()), lineSeparator(0.8), createPageLink(linkedFile, searchResult.getPageNumber(), searchQuery.getSearchExpression())); | ||
| } | ||
| if (!searchResult.getAnnotationsResultStringsHtml().isEmpty()) { | ||
| Text annotationsText = new Text(System.lineSeparator() + Localization.lang("Found matches in annotations:") + System.lineSeparator() + System.lineSeparator()); | ||
| annotationsText.getStyleClass().add("italic"); | ||
| content.getChildren().add(annotationsText); | ||
|
|
||
| for (String resultTextHtml : searchResult.getAnnotationsResultStringsHtml()) { | ||
| content.getChildren().addAll(TooltipTextUtil.createTextsFromHtml(resultTextHtml.replace("</b> <b>", " "))); | ||
| content.getChildren().addAll(new Text(System.lineSeparator()), lineSeparator(0.8), createPageLink(linkedFile, searchResult.getPageNumber(), searchQuery.getSearchExpression())); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| private Text createFileLink(LinkedFile linkedFile) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.