Skip to content

Commit

Permalink
Make word selection plugin searchbar aware (#1290)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Kornheisl <[email protected]>
  • Loading branch information
Jeremy Wootten and zeebok authored Aug 2, 2023
1 parent cdb62ff commit 9babc15
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
56 changes: 37 additions & 19 deletions plugins/highlight-word-selection/highlight-word-selection.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/Widgets/SearchBar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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 ();

Expand Down Expand Up @@ -227,7 +227,6 @@ namespace Scratch.Widgets {

cancel_update_search_widgets ();
this.text_view = text_view;

if (text_view == null) {
warning ("No SourceView is associated with SearchManager!");
search_context = null;
Expand Down

0 comments on commit 9babc15

Please sign in to comment.