@@ -34,12 +34,10 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Scratc
3434 plugins = (Scratch . Services . Interface ) object ;
3535 plugins. hook_document. connect ((doc) = > {
3636 if (current_source != null ) {
37- current_source. deselected. disconnect (on_deselection);
3837 current_source. selection_changed. disconnect (on_selection_changed);
3938 }
4039
4140 current_source = doc. source_view;
42- current_source. deselected. connect (on_deselection);
4341 current_source. selection_changed. connect (on_selection_changed);
4442 });
4543
@@ -48,16 +46,18 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Scratc
4846 });
4947 }
5048
51- public void on_selection_changed (ref Gtk .TextIter start , ref Gtk .TextIter end ) requires (main_window != null ) {
52- if (! main_window. has_successful_search ()) {
53- // Perform plugin selection when there is no ongoing and successful search
54- current_search_context = new Gtk .SourceSearchContext (
55- (Gtk . SourceBuffer )current_source. buffer,
56- null
57- );
58- current_search_context. settings. search_text = " " ;
49+ // A deselection is now treated as a selection change
50+ private void on_selection_changed () requires (main_window != null ) {
51+ if (current_search_context != null ) {
52+ // Cancel existing search
5953 current_search_context. set_highlight (false );
54+ current_search_context = null ;
55+ }
6056
57+ // Only highlight (extended) selection if non-zero length and search highlighting not happening
58+ Gtk . TextIter start, end;
59+ if (current_source. buffer. get_selection_bounds (out start, out end) &&
60+ ! main_window. has_successful_search ()) {
6161 var original_start = start. copy ();
6262
6363 // Ignore leading space
@@ -114,29 +114,20 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Scratc
114114 // Ensure no leading or trailing space
115115 var selected_text = start. get_text (end). strip ();
116116
117- if (selected_text. char_count () > SELECTION_HIGHLIGHT_MAX_CHARS ) {
118- return ;
117+ // We know the selected text is non-zero length, check not too long
118+ if (selected_text. char_count () <= SELECTION_HIGHLIGHT_MAX_CHARS ) {
119+ current_search_context = new Gtk .SourceSearchContext (
120+ (Gtk . SourceBuffer )current_source. buffer,
121+ null
122+ );
123+ current_search_context. settings. search_text = selected_text;
124+ current_search_context. set_highlight (true );
119125 }
120-
121- current_search_context. settings. search_text = selected_text;
122- current_search_context. set_highlight (true );
123- } else if (current_search_context != null ) {
124- // Cancel existing search
125- current_search_context. set_highlight (false );
126- current_search_context = null ;
127- }
128- }
129-
130- public void on_deselection () {
131- if (current_search_context != null ) {
132- current_search_context. set_highlight (false );
133- current_search_context = null ;
134126 }
135127 }
136128
137129 public void deactivate () {
138130 if (current_source != null ) {
139- current_source. deselected. disconnect (on_deselection);
140131 current_source. selection_changed. disconnect (on_selection_changed);
141132 }
142133 }
0 commit comments