From f6e276c1e7ee064e86e0a4225f194472b3401269 Mon Sep 17 00:00:00 2001 From: Artem Lytkin Date: Sun, 27 Sep 2026 12:39:13 +0300 Subject: [PATCH] diff kitten: Fix search missing matches at the end of a line find_matches_in_lines was passing the exclusive end offset of a match to find_pos, so a match ending at the end of the joined screen lines mapped to no line and was dropped. It was not highlighted, not counted and n/N skipped it. Since screen lines are not padded, this hit any match on the last character of a line, for example searching for foo in "x := foo". Look up the line holding the last matched byte instead. --- docs/changelog.rst | 2 ++ kittens/diff/search.go | 2 +- kittens/diff/search_test.go | 39 +++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 kittens/diff/search_test.go diff --git a/docs/changelog.rst b/docs/changelog.rst index 8d3a5e01f..766f43510 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -237,6 +237,8 @@ Detailed list of changes - macOS: Fix a custom dock icon reverting to the stock icon after a progress bar is cleared (:iss:`10545`) +- diff kitten: Fix search not finding matches at the end of a line + 0.49.1 [2026-09-24] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kittens/diff/search.go b/kittens/diff/search.go index 8275b44a0..2a36cc88a 100644 --- a/kittens/diff/search.go +++ b/kittens/diff/search.go @@ -57,7 +57,7 @@ func (self *Search) find_matches_in_lines(clean_lines []string, origin int, send } start_line := find_pos(start) if start_line > -1 { - end_line := find_pos(end) + end_line := find_pos(end - 1) if end_line > -1 { for i := start_line; i <= end_line; i++ { cell_start := 0 diff --git a/kittens/diff/search_test.go b/kittens/diff/search_test.go new file mode 100644 index 000000000..6318b1b6f --- /dev/null +++ b/kittens/diff/search_test.go @@ -0,0 +1,39 @@ +// License: GPLv3 Copyright: 2026, Kovid Goyal, + +package diff + +import ( + "regexp" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestDiffSearchFindMatches(t *testing.T) { + wrapped := []string{"abc def ", "ghi rows"} + for _, tc := range []struct { + pat string + lines []string + origin int + expected [][3]int + }{ + {"foo", []string{"x := foo"}, 0, [][3]int{{0, 5, 3}}}, + {"o", []string{"foo"}, 0, [][3]int{{0, 1, 1}, {0, 2, 1}}}, + {"rows", wrapped, 0, [][3]int{{1, 4, 4}}}, + {"def ghi", wrapped, 0, [][3]int{{0, 4, 4}, {1, 0, 3}}}, + {"def ", wrapped, 0, [][3]int{{0, 4, 4}}}, + {"foo", []string{"日本 foo"}, 0, [][3]int{{0, 5, 3}}}, + {"foo", []string{"x := foo"}, 7, [][3]int{{0, 12, 3}}}, + } { + s := &Search{pat: regexp.MustCompile(tc.pat)} + var actual [][3]int + s.find_matches_in_lines(tc.lines, tc.origin, func(screen_line, offset, size int) { + if size > 0 { + actual = append(actual, [3]int{screen_line, offset, size}) + } + }) + if diff := cmp.Diff(tc.expected, actual); diff != "" { + t.Errorf("Unexpected matches for %#v in %#v:\n%s", tc.pat, tc.lines, diff) + } + } +}