Skip to content

add vimgrep support for TUI picker output#41

Merged
boyter merged 1 commit intoboyter:masterfrom
oyarsa:master
Feb 27, 2026
Merged

add vimgrep support for TUI picker output#41
boyter merged 1 commit intoboyter:masterfrom
oyarsa:master

Conversation

@oyarsa
Copy link
Contributor

@oyarsa oyarsa commented Feb 26, 2026

I found it useful to have cs support printing vimgrep-style matches when exiting TUI mode, as I can use it inside nvim to populate the quickfix list, but cs seems to just ignore --format from TUI. This is used here: oyarsa/codespelunker.nvim.

The TUI code is mostly copied from the existing one in console.go. If you're interested, I can look into refactoring this.

PS: I hope it's okay to open PRs like this. It's something I found useful for myself, and I thought maybe you would too.

@pr-insights pr-insights bot added S/size Small change VL/complexity Very low complexity labels Feb 26, 2026
@boyter
Copy link
Owner

boyter commented Feb 26, 2026

I don't use vim personally, but if you say its good to use I totally trust you, and YES this is something I want in here.

PR's are always encouraged.

BTW if you rebase and install again you can get a nice performance win ;)

Reviewing this now.

@boyter
Copy link
Owner

boyter commented Feb 26, 2026

Looks good, only thing I ask, is can you add a section to the README.md about how to set this up and use it? Ideally for console and TUI, but im ok with just one.

Then I can try it out too :P

Do that and it will be merged.

@oyarsa
Copy link
Contributor Author

oyarsa commented Feb 26, 2026

Set up what, the nvim plugin?

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for vimgrep-style output formatting in TUI mode by respecting the --format=vimgrep flag. When a user exits the TUI by pressing Enter on a search result, the output is now formatted as vimgrep-style matches that can be consumed by tools like vim's quickfix list, rather than just printing the file location. This brings TUI mode into parity with console mode for output formatting.

Changes:

  • Added vimgrep format support when exiting TUI mode via Enter key
  • Implemented tuiVimGrep function that mirrors formatVimGrep logic for single-file formatting
  • Added format checking in both list view and file viewer modes to conditionally apply vimgrep formatting

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +904 to +960
func tuiVimGrep(cfg *Config, location string, matchLocations map[string][][]int) string {
content, err := readFileContent(location, cfg.MaxReadSizeBytes)
if err != nil {
return location
}
fj := &common.FileJob{
Filename: location,
Location: location,
Content: content,
MatchLocations: matchLocations,
}
fileMode := resolveSnippetMode(cfg.SnippetMode, location)

var vimGrepOutput []string
if fileMode == "grep" {
lineResults := snippet.FindAllMatchingLines(fj, cfg.LineLimit, 0, 0)
for _, lr := range lineResults {
col := 1
if len(lr.Locs) > 0 {
col = lr.Locs[0][0] + 1
}
hint := strings.ReplaceAll(lr.Content, "\n", "\\n")
line := fmt.Sprintf("%v:%v:%v:%v", location, lr.LineNumber, col, hint)
vimGrepOutput = append(vimGrepOutput, line)
}
} else if fileMode == "lines" {
lineResults := snippet.FindMatchingLines(fj, 0)
for _, lr := range lineResults {
col := 1
if len(lr.Locs) > 0 {
col = lr.Locs[0][0] + 1
}
hint := strings.ReplaceAll(lr.Content, "\n", "\\n")
line := fmt.Sprintf("%v:%v:%v:%v", location, lr.LineNumber, col, hint)
vimGrepOutput = append(vimGrepOutput, line)
}
} else {
docFreq := make(map[string]int, len(matchLocations))
for k, v := range matchLocations {
docFreq[k] = len(v)
}
snippets := snippet.ExtractRelevant(fj, docFreq, 50)
if len(snippets) > cfg.SnippetCount {
snippets = snippets[:cfg.SnippetCount]
}
for _, snip := range snippets {
hint := strings.ReplaceAll(snip.Content, "\n", "\\n")
line := fmt.Sprintf("%v:%v:%v:%v", location, snip.LineStart, snip.StartPos, hint)
vimGrepOutput = append(vimGrepOutput, line)
}
}

if len(vimGrepOutput) == 0 {
return location
}
return strings.Join(vimGrepOutput, "\n")
}
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding test coverage for the tuiVimGrep function. While the implementation closely mirrors formatVimGrep from console.go, having tests would help ensure correct behavior across different snippet modes (grep, lines, snippet) and verify proper handling of edge cases like file read errors or empty match locations. The existing tui_test.go file demonstrates that TUI functions are tested in this codebase.

Copilot uses AI. Check for mistakes.
@boyter
Copy link
Owner

boyter commented Feb 27, 2026

Set up what, the nvim plugin?

Just whatever you need to do to add it? Sorry I really only know enough vim to open, edit, search, save and quit. Its really not what I learnt... I went emacs, then got emacs pinkie and moved away.

@boyter
Copy link
Owner

boyter commented Feb 27, 2026

Bah gonna merge anyway. Ill learn some vim.

@boyter boyter merged commit 5900e60 into boyter:master Feb 27, 2026
@boyter
Copy link
Owner

boyter commented Feb 27, 2026

Thank you @oyarsa !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S/size Small change VL/complexity Very low complexity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants