From 218b00727a7d451b1fa3c3a8f5638469d40d723e Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 27 Nov 2021 12:31:16 +0100 Subject: Do not assign a new hint to duplicate matches. Reuse the same one for all of them. --- reselect | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/reselect b/reselect index 9e1c4f5..a001640 100644 --- a/reselect +++ b/reselect @@ -80,18 +80,31 @@ sub build_overlays { my @matches = find_matches($pattern, $text, $rowmap); @matches = reverse @matches if ($self->{descending}); - my $results = {}; - my $hints = hint_iterator(); + + my $match_hints = {}; + my $results = {}; + my $hints = hint_iterator(); for my $m (@matches) { my ($col, $row, $match) = @$m; - my $hint = $hints->(); + # create a new hint if this match was not seen before + # reuse an existing one otherwise + if (not exists $match_hints->{$match}) { + $match_hints->{$match} = $hints->(); + } + my $hint = $match_hints->{$match}; + + if (not exists $results->{$hint}) { + $results->{$hint} = { match => $match, labels => [] }; + } + + # create the overlay and add it to the list of overlays for this hint my $overlay = $self->overlay($col, $row, $self->strwidth($hint), 1, $label_rend, 0); $overlay->set(0, 0, $hint); - $results->{$hint} = {label => $overlay, match => $match}; + push(@{$results->{$hint}->{labels}}, $overlay); } return $results; @@ -188,13 +201,16 @@ sub update { foreach (keys %{$results}) { my $result = $results->{$_}; - my $overlay = $result->{label}; if ($len == 0 || index($_, $p->{buffer}) == 0) { - $overlay->show; + for my $overlay (@{$result->{labels}}) { + $overlay->show; + } unshift @matches, $result->{match}; } else { - $overlay->hide; + for my $overlay (@{$result->{labels}}) { + $overlay->hide; + } } } # Auto-launch a single url only if the launchsingle resource is true -- cgit v1.2.3