Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Vimgrep format #14

Merged
merged 1 commit into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 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,36 @@ 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")
fmt.Println(printable)
}

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

Expand Down