diff --git a/docs/changelog.rst b/docs/changelog.rst index 9b8f31c05..992a2039f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -213,6 +213,9 @@ Detailed list of changes - Vertical tabs: Improve rendering of tabs when using the ``powerline`` :opt:`tab_bar_style` +- themes and choose fonts kittens: Fix search freezing for many seconds when + typing a long query, such as the full name of a theme + 0.49.1 [2026-09-24] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tools/tui/subseq/score.go b/tools/tui/subseq/score.go index 83b7a59b9..0a69055f7 100644 --- a/tools/tui/subseq/score.go +++ b/tools/tui/subseq/score.go @@ -53,7 +53,8 @@ func level_factor_for(current_lcase, last_lcase, current_cased, last_cased rune, type workspace_type struct { positions [][]int // positions of each needle char in haystack level_factors []int - address []int + best []float64 // highest score of a match ending at each position of each needle char, flattened + prev []int // index into the positions of the previous needle char for that match max_score_per_char float64 } @@ -75,62 +76,13 @@ func (w *workspace_type) initialize(haystack_sz, needle_sz int) { w.positions[i] = w.positions[i][:0] } } - if cap(w.address) < needle_sz { - w.address = make([]int, needle_sz) - } - w.address = utils.Memset(w.address) } -func (w *workspace_type) position(x int) int { // the position of xth needle char in the haystack for the current address - return w.positions[x][w.address[x]] -} - -func (w *workspace_type) increment_address() bool { - pos := len(w.positions) - 1 // the last needle char - for { - w.address[pos]++ - if w.address[pos] < len(w.positions[pos]) { - return true - } - if pos == 0 { - break - } - w.address[pos] = 0 - pos-- +func (w *workspace_type) char_score(pos, distance int) float64 { + if w.level_factors[pos] > 0 { + return (100.0 * w.max_score_per_char) / float64(w.level_factors[pos]) // at a special location } - return false -} - -func (w *workspace_type) address_is_monotonic() bool { - // Check if the character positions pointed to by the current address are monotonic - for i := 1; i < len(w.positions); i++ { - if w.position(i) <= w.position(i-1) { - return false - } - } - return true -} - -func (w *workspace_type) calc_score() (ans float64) { - distance, pos := 0, 0 - for i := range len(w.positions) { - pos = w.position(i) - if i == 0 { - distance = pos + 1 - } else { - distance = pos - w.position(i-1) - if distance < 2 { - ans += w.max_score_per_char // consecutive chars - continue - } - } - if w.level_factors[pos] > 0 { - ans += (100.0 * w.max_score_per_char) / float64(w.level_factors[pos]) // at a special location - } else { - ans += (0.75 * w.max_score_per_char) / float64(distance) - } - } - return + return (0.75 * w.max_score_per_char) / float64(distance) } func has_atleast_one_match(w *workspace_type) (found bool) { @@ -177,21 +129,52 @@ func score_item(item string, idx int, needle []rune, opts *resolved_options_type if !has_atleast_one_match(w) { return ans } - var score float64 - for { - if w.address_is_monotonic() { - score = w.calc_score() - if score > ans.Score { - ans.Score = score - for i := range ans.Positions { - ans.Positions[i] = w.position(i) + // Find the highest scoring monotonic positions one needle char at a time + n := 0 + for _, positions := range w.positions { + n += len(positions) + } + if cap(w.best) < n { + w.best, w.prev = make([]float64, 2*n), make([]int, 2*n) + } + for k, pos := range w.positions[0] { + w.best[k] = w.char_score(pos, pos+1) + } + off := 0 // offset of the entries for the previous needle char in best + for j := 1; j < len(w.positions); j++ { + prev_positions, next_off := w.positions[j-1], off+len(w.positions[j-1]) + for k, pos := range w.positions[j] { + score, from := 0.0, 0 + for i := 0; i < len(prev_positions) && prev_positions[i] < pos; i++ { + s := w.best[off+i] + if s == 0 { // no match ends here, all scores are > 0 + continue + } + if distance := pos - prev_positions[i]; distance < 2 { + s += w.max_score_per_char // consecutive chars + } else { + s += w.char_score(pos, distance) + } + if s > score { + score, from = s, i } } + w.best[next_off+k], w.prev[next_off+k] = score, from } - if !w.increment_address() { - break + off = next_off + } + last, k := len(w.positions)-1, 0 + for i := range len(w.positions[last]) { + if w.best[off+i] > ans.Score { + ans.Score, k = w.best[off+i], i } } + for j := last; j > 0; j-- { + ans.Positions[j] = w.positions[j][k] + k = w.prev[off+k] + off -= len(w.positions[j-1]) + } + ans.Positions[0] = w.positions[0][k] if ans.Score > 0 { adjust := utils.RuneOffsetsToByteOffsets(item) for i := range ans.Positions { diff --git a/tools/tui/subseq/score_test.go b/tools/tui/subseq/score_test.go index c2938e991..c53c7854b 100644 --- a/tools/tui/subseq/score_test.go +++ b/tools/tui/subseq/score_test.go @@ -5,6 +5,8 @@ package subseq import ( "fmt" "github.com/kovidgoyal/kitty/tools/utils" + "math/rand" + "slices" "strconv" "strings" "testing" @@ -97,3 +99,105 @@ func TestSubseq(t *testing.T) { } simple(strings.Join(items, "\n"), "2", expected...) } + +var theme_names = []string{ + "1984 Dark", "Adwaita darker", "Aquarium Light", "Atelier Plateau Dark", "Ayu Mirage", "Base2Tone Heath Dark", + "Base2Tone Sea Dark", "Base4Tone_Modern_W Dark", "Bluloco Dark", "C64", "Catppuccin-Latte", "Chalkboard", + "Copland OS", "Darkside", "Doom Vibrant", "Earthsong", "Everforest Light Hard", "Flexoki (Dark)", + "GitHub Dark Colorblind", "GitHub Light High Contrast", "Gnome-ish gray-on-black", "Gruvbox Light Soft", + "Gruvbox Material Light Medium", "HardHacker", "IC Green PPL", "Kanagawa_dragon", "Kibble", "Linh", "Mayukai", + "Modus Vivendi", "N0tch2k", "Nord", "One Half Dark", "Pastel EGA", "Red Alert", "Ryoccino", "Selenized White", + "Serendipity Sunset", "Soft Server", "Solarized Dark Higher Contrast", "Sonokai Sushia", "Spring", "Teerb", + "Tokyo Night", "Tomorrow", "Tomorrow Night Eighties", "Ubuntu", "Wez", "Yorumi Kraken", "ferra", "pink lavender", + "zenbones_light", +} + +func score_positions(w *workspace_type, positions []int) (ans float64) { + for i, pos := range positions { + distance := pos + 1 + if i > 0 { + distance = pos - positions[i-1] + if distance < 2 { + ans += w.max_score_per_char + continue + } + } + if w.level_factors[pos] > 0 { + ans += (100.0 * w.max_score_per_char) / float64(w.level_factors[pos]) + } else { + ans += (0.75 * w.max_score_per_char) / float64(distance) + } + } + return +} + +// Try every monotonic set of positions for the item last scored with w +func brute_force(w *workspace_type) (best float64) { + positions := make([]int, len(w.positions)) + var recurse func(j int) + recurse = func(j int) { + if j == len(positions) { + best = max(best, score_positions(w, positions)) + return + } + for _, pos := range w.positions[j] { + if j == 0 || pos > positions[j-1] { + positions[j] = pos + recurse(j + 1) + } + } + } + recurse(0) + return +} + +func TestSubseqBruteForce(t *testing.T) { + opts := resolved_options_type{level1: []rune(" "), level2: []rune(LEVEL2), level3: []rune(LEVEL3)} + w := workspace_type{} + r := rand.New(rand.NewSource(1)) + for range 200 { + name := []rune(strings.ToLower(theme_names[r.Intn(len(theme_names))])) + idx := r.Perm(len(name))[:1+r.Intn(min(6, len(name)))] + slices.Sort(idx) + needle := make([]rune, len(idx)) + for i, x := range idx { + needle[i] = name[x] + } + for _, item := range theme_names { + m := score_item(item, 0, needle, &opts, &w) + if expected := brute_force(&w); m.Score != expected { + t.Fatalf("Score for %#v in %#v: %v != %v", string(needle), item, m.Score, expected) + } + if m.Score > 0 { + ok := score_positions(&w, m.Positions) == m.Score + for i, pos := range m.Positions { + ok = ok && strings.ToLower(item)[pos] == byte(needle[i]) && (i == 0 || pos > m.Positions[i-1]) + } + if !ok { + t.Fatalf("Positions for %#v in %#v: %v do not give the score %v", string(needle), item, m.Positions, m.Score) + } + } + } + } + // the old code tried every combination of positions here, which never finishes + m := ScoreItems(strings.Repeat("a", 100), []string{strings.Repeat("a", 200)}, Options{})[0] + expected := make([]int, 100) + for i := range expected { + expected[i] = i + } + if diff := cmp.Diff(expected, m.Positions); diff != "" { + t.Fatalf("Failed positions for long needle:\n%s", diff) + } +} + +func BenchmarkScoreItems(b *testing.B) { + for _, query := range []string{"dark", "tokyo night", "solarized dark higher contrast"} { + b.Run(query, func(b *testing.B) { + for b.Loop() { + for i := range len(query) { // as if typed one char at a time + ScoreItems(query[:i+1], theme_names, Options{Level1: " "}) + } + } + }) + } +}