Skip to content

Commit

Permalink
Add Vimgrep format
Browse files Browse the repository at this point in the history
Provide option for integration with vim's grep command. This allows the
results to return to the Quickfix list and be cycled through after a
search.
  • Loading branch information
tlelson committed Jul 27, 2020
1 parent 02c3f08 commit afbe5f9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func main() {
"format",
"f",
"text",
"set output format [text, json]",
"set output format [text, json, vimgrep]",
)
flags.StringVar(
&processor.Ranker,
Expand Down
31 changes: 31 additions & 0 deletions processor/worker_summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/mattn/go-isatty"
"io/ioutil"
"os"
"strings"
)

type ResultSummarizer struct {
Expand Down Expand Up @@ -53,11 +54,41 @@ func (f *ResultSummarizer) Start() {
switch f.Format {
case "json":
f.formatJson(results)
case "vimgrep":
f.formatVimGrep(results)
default:
f.formatDefault(results)
}
}

func (f *ResultSummarizer) formatVimGrep(results []*FileJob) {
var vimGrepOutput []string
SnippetLength = 50 // vim quickfix puts each hit on its own line.
documentFrequency := calculateDocumentTermFrequency(results)

// Cycle through files with matches and process each snippets inside it.
for _, res := range results {
snippets := extractRelevantV3(res, documentFrequency, int(SnippetLength), "…")
if int64(len(snippets)) > f.SnippetCount {
snippets = snippets[:f.SnippetCount]
}

for _, snip := range snippets {
hint := strings.ReplaceAll(snip.Content, "\n", "\\n")
line := fmt.Sprintf("%v:%v:%v:%v", res.Location, snip.LineStart, snip.StartPos, hint)
vimGrepOutput = append(vimGrepOutput, line)
}
}

printable := strings.Join(vimGrepOutput, "\n")
if f.FileOutput == "" {
fmt.Println(printable)
} else {
_ = ioutil.WriteFile(FileOutput, []byte(printable), 0600)
fmt.Println("results written to " + FileOutput)
}
}

func (f *ResultSummarizer) formatJson(results []*FileJob) {
var jsonResults []jsonResult

Expand Down

0 comments on commit afbe5f9

Please sign in to comment.