From d63f475f3abb66e59ed7702883f865e1d13febbd Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 26 Mar 2023 13:23:45 +0100 Subject: [PATCH] Make word selection plugin searchbar aware --- .../highlight-word-selection.vala | 56 ++++++++++++------- src/Widgets/SearchBar.vala | 9 ++- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/plugins/highlight-word-selection/highlight-word-selection.vala b/plugins/highlight-word-selection/highlight-word-selection.vala index 0d0ba9ec52..c14fe243b9 100644 --- a/plugins/highlight-word-selection/highlight-word-selection.vala +++ b/plugins/highlight-word-selection/highlight-word-selection.vala @@ -20,7 +20,8 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Peas.Activatable { Scratch.Widgets.SourceView current_source; - Gtk.SourceSearchContext current_search_context; + Scratch.MainWindow? main_window = null; + Gtk.SourceSearchContext? current_search_context = null; // Consts // Pneumonoultramicroscopicsilicovolcanoconiosis longest word in a major dictionary @ 45 @@ -43,33 +44,50 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Peas.A current_source.deselected.connect (on_deselection); current_source.selection_changed.connect (on_selection_changed); }); + + plugins.hook_window.connect ((w) => { + main_window = w; + }); } public void on_selection_changed (ref Gtk.TextIter start, ref Gtk.TextIter end) { - if (!start.equal (end)) { - // Expand highlight to current word - if (!start.starts_word ()) { - start.backward_word_start (); - } - - if (!end.ends_word ()) { - end.forward_word_end (); + var window_search_context = main_window != null ? main_window.search_bar.search_context : null; + if (window_search_context == null || + window_search_context.settings.search_text == "" || + window_search_context.get_occurrences_count () == 0) { + + if (!start.equal (end)) { + // Expand highlight to current word + if (!start.starts_word ()) { + start.backward_word_start (); + } + + if (!end.ends_word ()) { + end.forward_word_end (); + } + + string selected_text = start.get_buffer ().get_text (start, end, false); + if (selected_text.char_count () > SELECTION_HIGHLIGHT_MAX_CHARS) { + return; + } + + current_search_context = new Gtk.SourceSearchContext ((Gtk.SourceBuffer)current_source.buffer, null); + current_search_context.settings.search_text = selected_text; + // Honor current search settings (to be confirmed) + if (window_search_context != null ) { + current_search_context.settings.case_sensitive = window_search_context.settings.case_sensitive; + } } - - string selected_text = start.get_buffer ().get_text (start, end, false); - if (selected_text.char_count () > SELECTION_HIGHLIGHT_MAX_CHARS) { - return; - } - - current_search_context = new Gtk.SourceSearchContext ((Gtk.SourceBuffer)current_source.buffer, null); - current_search_context.settings.search_text = selected_text; - current_search_context.set_highlight (true); + } else if (current_search_context != null) { + current_search_context.set_highlight (false); + current_search_context = null; } } public void on_deselection () { if (current_search_context != null) { - current_search_context.settings.search_text = null; + current_search_context.set_highlight (false); + current_search_context = null; } } diff --git a/src/Widgets/SearchBar.vala b/src/Widgets/SearchBar.vala index 50520b9c04..a9dc29ccb6 100644 --- a/src/Widgets/SearchBar.vala +++ b/src/Widgets/SearchBar.vala @@ -51,7 +51,7 @@ namespace Scratch.Widgets { private Scratch.Widgets.SourceView? text_view = null; private Gtk.TextBuffer? text_buffer = null; - private Gtk.SourceSearchContext search_context = null; + public Gtk.SourceSearchContext? search_context { get; private set; default = null; } public signal void search_empty (); @@ -217,9 +217,14 @@ namespace Scratch.Widgets { } public void set_text_view (Scratch.Widgets.SourceView? text_view) { + // Do not needlessly recreate search context as this has + // unexpected effects + if (this.text_view == text_view) { + return; + } + cancel_update_search_widgets (); this.text_view = text_view; - if (text_view == null) { warning ("No SourceView is associated with SearchManager!"); search_context = null;