From f667d24228455d6dd9b516583d49565c18014359 Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Thu, 19 Oct 2023 09:31:58 -0700 Subject: [PATCH 1/2] Improve query script output --- cmd/zoekt/main.go | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/cmd/zoekt/main.go b/cmd/zoekt/main.go index 7ad2c23ec..e1f8afbbe 100644 --- a/cmd/zoekt/main.go +++ b/cmd/zoekt/main.go @@ -31,21 +31,47 @@ import ( "github.com/sourcegraph/zoekt/shards" ) -func displayMatches(files []zoekt.FileMatch, pat string, withRepo bool, list bool) { +const ( + chunkMatchesPerFile = 3 + fileMatchesPerSearch = 6 +) + +func splitAtIndex[E any](s []E, idx int) ([]E, []E) { + if idx < len(s) { + return s[:idx], s[idx:] + } + return s, nil +} + +func displayMatches(files []zoekt.FileMatch, withRepo bool, list bool) { + files, hiddenFiles := splitAtIndex(files, fileMatchesPerSearch) for _, f := range files { r := "" if withRepo { r = f.Repository + "/" } + + fmt.Printf("%s%s%s\n", r, f.FileName, addTabIfNonEmpty(f.Debug)) if list { - fmt.Printf("%s%s%s\n", r, f.FileName, addTabIfNonEmpty(f.Debug)) continue } - for _, m := range f.LineMatches { - fmt.Printf("%s%s:%d:%s%s\n", r, f.FileName, m.LineNumber, m.Line, addTabIfNonEmpty(f.Debug)) + lines, hidden := splitAtIndex(f.ChunkMatches, chunkMatchesPerFile) + + for _, m := range lines { + fmt.Printf("%d:%s%s\n", m.ContentStart.LineNumber, string(m.Content), addTabIfNonEmpty(m.DebugScore)) + } + + if len(hidden) > 0 { + fmt.Printf("hidden %d more chunk matches\n", len(hidden)) } + fmt.Println() + } + + if len(hiddenFiles) > 0 { + fmt.Printf("hidden %d more file matches\n", len(hiddenFiles)) } + fmt.Println() } func addTabIfNonEmpty(s string) string { @@ -180,6 +206,7 @@ func main() { sOpts := zoekt.SearchOptions{ DebugScore: *debug, + ChunkMatches: true, } sres, err := searcher.Search(context.Background(), query, &sOpts) if err != nil { @@ -195,7 +222,7 @@ func main() { sres, _ = searcher.Search(context.Background(), query, &sOpts) } - displayMatches(sres.Files, pat, *withRepo, *list) + displayMatches(sres.Files, *withRepo, *list) if *verbose { log.Printf("stats: %#v", sres.Stats) } From 66be34f7558229d15e7e2d1fb02fe92bc8c79639 Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Thu, 19 Oct 2023 10:09:58 -0700 Subject: [PATCH 2/2] Rename lines -> chunks --- cmd/zoekt/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/zoekt/main.go b/cmd/zoekt/main.go index e1f8afbbe..c83813716 100644 --- a/cmd/zoekt/main.go +++ b/cmd/zoekt/main.go @@ -56,9 +56,9 @@ func displayMatches(files []zoekt.FileMatch, withRepo bool, list bool) { continue } - lines, hidden := splitAtIndex(f.ChunkMatches, chunkMatchesPerFile) + chunks, hidden := splitAtIndex(f.ChunkMatches, chunkMatchesPerFile) - for _, m := range lines { + for _, m := range chunks { fmt.Printf("%d:%s%s\n", m.ContentStart.LineNumber, string(m.Content), addTabIfNonEmpty(m.DebugScore)) }