From 6794ec1de749da04450deeeb816e35036947a42b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 12 Mar 2023 21:34:54 +0530 Subject: [PATCH] Wire up the new subseq match code --- kittens/themes/collection.py | 2 -- setup.py | 5 ----- tools/themes/collection.go | 29 ++++++++++++++--------------- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/kittens/themes/collection.py b/kittens/themes/collection.py index 0dbcf41a4..7b0f2b4fa 100644 --- a/kittens/themes/collection.py +++ b/kittens/themes/collection.py @@ -22,8 +22,6 @@ from kitty.fast_data_types import Color from kitty.options.types import Options as KittyOptions from kitty.utils import reload_conf_in_all_kitties -from ..choose.match import match - MARK_BEFORE = '\033[33m' MARK_AFTER = '\033[39m' diff --git a/setup.py b/setup.py index 2b9d240e1..415ddeeea 100755 --- a/setup.py +++ b/setup.py @@ -819,11 +819,6 @@ def compile_kittens(compilation_database: CompilationDatabase) -> None: files('unicode_input', 'unicode_names'), files('diff', 'diff_speedup'), files('transfer', 'rsync', libraries=('rsync',)), - files( - 'choose', 'subseq_matcher', - extra_headers=('kitty/charsets.h',), - extra_sources=('kitty/charsets.c',), - filter_sources=lambda x: 'windows_compat.c' not in x), ): final_env = kenv.copy() final_env.cflags.extend(f'-I{x}' for x in includes) diff --git a/tools/themes/collection.go b/tools/themes/collection.go index 64de38d41..36a173a50 100644 --- a/tools/themes/collection.go +++ b/tools/themes/collection.go @@ -20,6 +20,7 @@ import ( "kitty/tools/cli" "kitty/tools/config" + "kitty/tools/tui/subseq" "kitty/tools/utils" "kitty/tools/utils/style" @@ -819,8 +820,10 @@ func (self *Themes) ThemeByName(name string) *Theme { return ans } -func match(expression string, items []string) []string { - return nil +func match(expression string, items []string) []*subseq.Match { + matches := subseq.ScoreItems(expression, items, subseq.Options{Level1: " "}) + matches = utils.StableSort(matches, func(a, b *subseq.Match) bool { return a.Score > b.Score }) + return matches } func (self *Themes) ApplySearch(expression string, marks ...string) []string { @@ -831,20 +834,16 @@ func (self *Themes) ApplySearch(expression string, marks ...string) []string { results := match(expression, maps.Keys(self.name_map)) name_map := make(map[string]*Theme, len(results)) ans := make([]string, 0, len(results)) - for _, r := range results { - pos, k, _ := strings.Cut(r, ":") - positions := []int{} - for _, q := range strings.Split(pos, ",") { - i, _ := strconv.Atoi(q) - positions = append(positions, i) - text := k - for i := len(positions) - 1; i >= 0; i-- { - p := positions[i] - text = text[:p] + mark_before + text[p:p+1] + mark_after + text[p+1:] - } - name_map[k] = self.name_map[k] - ans = append(ans, text) + for _, m := range results { + k := m.Text + text := m.Text + positions := m.Positions + for i := len(positions) - 1; i >= 0; i-- { + p := positions[i] + text = text[:p] + mark_before + text[p:p+1] + mark_after + text[p+1:] } + name_map[k] = self.name_map[k] + ans = append(ans, text) } self.name_map = name_map self.index_map = maps.Keys(name_map)