Skip to content
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

Limit selection highlighting to within a single line and avoid crash #1447

Closed
wants to merge 8 commits into from
30 changes: 19 additions & 11 deletions plugins/highlight-word-selection/highlight-word-selection.vala
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,24 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Peas.A
public void on_selection_changed (ref Gtk.TextIter start, ref Gtk.TextIter end) {
var window_search_context = main_window != null ? main_window.search_bar.search_context : null;

if (current_search_context != null) {
// Cancel existing search
current_search_context.set_highlight (false);
current_search_context = null;
}


// Perform plugin selection highlighting only when there is no ongoing and successful search in the window search bar
if (window_search_context == null ||
window_search_context.settings.search_text == "" ||
window_search_context.get_occurrences_count () == 0) {
// Perform plugin selection when there is no ongoing and successful search
current_search_context = new Gtk.SourceSearchContext (
(Gtk.SourceBuffer)current_source.buffer,
null
);
current_search_context.settings.search_text = "";
current_search_context.set_highlight (false);

// Only highlight if selection is over a single line
if (start.get_line () != end.get_line ()) {
return;
}

// Determine word(s) to be highlighted
var original_start = start.copy ();

// Ignore leading space
Expand Down Expand Up @@ -124,12 +131,13 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Peas.A
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) {
// Cancel existing search
current_search_context.set_highlight (false);
current_search_context = null;
}
}

Expand Down