Skip to content

Commit ead7eec

Browse files
authored
Merge branch 'master' into prepare-release-8.1.0
2 parents f7261bb + 5e578f6 commit ead7eec

File tree

2 files changed

+27
-45
lines changed

2 files changed

+27
-45
lines changed

plugins/highlight-word-selection/highlight-word-selection.vala

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Widgets/SourceView.vala

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ namespace Scratch.Widgets {
4444
private const double SCROLL_THRESHOLD = 1.0;
4545

4646
public signal void style_changed (Gtk.SourceStyleScheme style);
47-
// "selection_changed" signal now only emitted when the selected text changes (position ignored). Listened to by searchbar and highlight word selection plugin
47+
// "selection_changed" signal now only emitted when the selected text changes (position ignored).
48+
// Listened to by searchbar and highlight word selection plugin
4849
public signal void selection_changed (Gtk.TextIter start_iter, Gtk.TextIter end_iter);
49-
public signal void deselected ();
5050

5151
//lang can be null, in the case of *No highlight style* aka Normal text
5252
public Gtk.SourceLanguage? language {
@@ -679,13 +679,7 @@ namespace Scratch.Widgets {
679679
selection_changed_timer = 0;
680680
}
681681

682-
// Fire deselected immediately
683-
if (start.equal (end)) {
684-
deselected ();
685-
// Don't fire signal till we think select movement is done
686-
} else {
687-
selection_changed_timer = Timeout.add (THROTTLE_MS, selection_changed_event);
688-
}
682+
selection_changed_timer = Timeout.add (THROTTLE_MS, selection_changed_event);
689683
}
690684

691685
private void on_mark_deleted (Gtk.TextMark mark) {
@@ -698,15 +692,12 @@ namespace Scratch.Widgets {
698692

699693
private bool selection_changed_event () {
700694
Gtk.TextIter start, end;
701-
bool selected = buffer.get_selection_bounds (out start, out end);
702-
if (selected) {
703-
var prev_selected_text = selected_text;
704-
selected_text = buffer.get_text (start, end, true);
705-
if (selected_text != prev_selected_text) {
706-
selection_changed (start, end);
707-
}
708-
} else {
709-
deselected ();
695+
buffer.get_selection_bounds (out start, out end);
696+
// No selection now treated as a potential selection change
697+
var prev_selected_text = selected_text;
698+
selected_text = buffer.get_text (start, end, true);
699+
if (selected_text != prev_selected_text) {
700+
selection_changed (start, end);
710701
}
711702

712703
selection_changed_timer = 0;

0 commit comments

Comments
 (0)