Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 18 additions & 27 deletions plugins/highlight-word-selection/highlight-word-selection.vala
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Scratc
plugins = (Scratch.Services.Interface) object;
plugins.hook_document.connect ((doc) => {
if (current_source != null) {
current_source.deselected.disconnect (on_deselection);
current_source.selection_changed.disconnect (on_selection_changed);
}

current_source = doc.source_view;
current_source.deselected.connect (on_deselection);
current_source.selection_changed.connect (on_selection_changed);
});

Expand All @@ -48,16 +46,18 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Scratc
});
}

public void on_selection_changed (ref Gtk.TextIter start, ref Gtk.TextIter end) requires (main_window != null) {
if (!main_window.has_successful_search ()) {
// 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 = "";
// A deselection is now treated as a selection change
private void on_selection_changed () requires (main_window != null) {
if (current_search_context != null) {
// Cancel existing search
current_search_context.set_highlight (false);
current_search_context = null;
}

// Only highlight (extended) selection if non-zero length and search highlighting not happening
Gtk.TextIter start, end;
if (current_source.buffer.get_selection_bounds (out start, out end) &&
!main_window.has_successful_search ()) {
var original_start = start.copy ();

// Ignore leading space
Expand Down Expand Up @@ -114,29 +114,20 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Scratc
// Ensure no leading or trailing space
var selected_text = start.get_text (end).strip ();

if (selected_text.char_count () > SELECTION_HIGHLIGHT_MAX_CHARS) {
return;
// We know the selected text is non-zero length, check not too long
if (selected_text.char_count () <= SELECTION_HIGHLIGHT_MAX_CHARS) {
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);
}

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;
}
}

public void on_deselection () {
if (current_search_context != null) {
current_search_context.set_highlight (false);
current_search_context = null;
}
}

public void deactivate () {
if (current_source != null) {
current_source.deselected.disconnect (on_deselection);
current_source.selection_changed.disconnect (on_selection_changed);
}
}
Expand Down
27 changes: 9 additions & 18 deletions src/Widgets/SourceView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ namespace Scratch.Widgets {
private const double SCROLL_THRESHOLD = 1.0;

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

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

// Fire deselected immediately
if (start.equal (end)) {
deselected ();
// Don't fire signal till we think select movement is done
} else {
selection_changed_timer = Timeout.add (THROTTLE_MS, selection_changed_event);
}
selection_changed_timer = Timeout.add (THROTTLE_MS, selection_changed_event);
}

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

private bool selection_changed_event () {
Gtk.TextIter start, end;
bool selected = buffer.get_selection_bounds (out start, out end);
if (selected) {
var prev_selected_text = selected_text;
selected_text = buffer.get_text (start, end, true);
if (selected_text != prev_selected_text) {
selection_changed (start, end);
}
} else {
deselected ();
buffer.get_selection_bounds (out start, out end);
// No selection now treated as a potential selection change
var prev_selected_text = selected_text;
selected_text = buffer.get_text (start, end, true);
if (selected_text != prev_selected_text) {
selection_changed (start, end);
}

selection_changed_timer = 0;
Expand Down