diff --git a/CHANGELOG.md b/CHANGELOG.md index 787c90d091..11f47c84e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to `src-cli` are documented in this file. ### Changed ### Fixed +- `src search -stream` displayed the number of lines that contain matches instead of the number of matches. ### Removed diff --git a/cmd/src/format.go b/cmd/src/format.go index 8ad63edc1f..af36ee157b 100644 --- a/cmd/src/format.go +++ b/cmd/src/format.go @@ -100,6 +100,7 @@ func parseTemplate(text string) (*template.Template, error) { "streamSearchHighlightCommit": streamSearchTemplateFuncs["streamSearchHighlightCommit"], "streamSearchRenderCommitLabel": streamSearchTemplateFuncs["streamSearchRenderCommitLabel"], "matchOrMatches": streamSearchTemplateFuncs["matchOrMatches"], + "countMatches": streamSearchTemplateFuncs["countMatches"], // Alert rendering "searchAlertRender": func(alert searchResultsAlert) string { diff --git a/cmd/src/search_stream.go b/cmd/src/search_stream.go index 1a06b46372..825c225854 100644 --- a/cmd/src/search_stream.go +++ b/cmd/src/search_stream.go @@ -230,7 +230,7 @@ const streamingTemplate = ` {{- color "search-repository"}}{{.Repository}}{{color "nc" -}} {{- " › " -}} {{- color "search-filename"}}{{.Path}}{{color "nc" -}} - {{- color "success"}}{{matchOrMatches (len .LineMatches)}}{{color "nc" -}} + {{- color "success"}}{{matchOrMatches (countMatches .LineMatches)}}{{color "nc" -}} {{- "\n" -}} {{- color "search-border"}}{{"--------------------------------------------------------------------------------\n"}}{{color "nc"}} @@ -375,6 +375,14 @@ var streamSearchTemplateFuncs = map[string]interface{}{ } return fmt.Sprintf(" (%d matches)", i) }, + + "countMatches": func(lineMatches []streaming.EventLineMatch) int { + count := 0 + for _, l := range lineMatches { + count += len(l.OffsetAndLengths) + } + return count + }, } func streamSearchHighlightDiffPreview(diffPreview string, highlights []highlight) string { diff --git a/cmd/src/search_stream_test.go b/cmd/src/search_stream_test.go index be1887d5c8..e053dd8db4 100644 --- a/cmd/src/search_stream_test.go +++ b/cmd/src/search_stream_test.go @@ -45,9 +45,9 @@ var event = []streaming.EventMatch{ Version: "", LineMatches: []streaming.EventLineMatch{ { - Line: "foo bar", + Line: "foo bar foo", LineNumber: 4, - OffsetAndLengths: [][2]int32{{4, 3}}, + OffsetAndLengths: [][2]int32{{0, 3}, {8, 3}}, }, }, }, diff --git a/cmd/src/testdata/streaming_search_want.json b/cmd/src/testdata/streaming_search_want.json index 8404bb99b8..86936f2909 100755 --- a/cmd/src/testdata/streaming_search_want.json +++ b/cmd/src/testdata/streaming_search_want.json @@ -1,4 +1,4 @@ -{"type":"content","name":"path/to/file","repository":"org/repo","lineMatches":[{"line":"foo bar","lineNumber":4,"offsetAndLengths":[[4,3]]}]} +{"type":"content","name":"path/to/file","repository":"org/repo","lineMatches":[{"line":"foo bar foo","lineNumber":4,"offsetAndLengths":[[0,3],[8,3]]}]} {"type":"repo","repository":"sourcegraph/sourcegraph"} {"type":"symbol","name":"path/to/file","repository":"org/repo","symbols":[{"url":"github.com/sourcegraph/sourcegraph/-/blob/cmd/frontend/graphqlbackend/search_results.go#L1591:26-1591:35","name":"doResults","containerName":"","kind":"FUNCTION"},{"url":"github.com/sourcegraph/sourcegraph/-/blob/cmd/frontend/graphqlbackend/search_results.go#L1591:26-1591:35","name":"Results","containerName":"SearchResultsResolver","kind":"FIELD"}]} {"type":"commit","icon":"","label":"[sourcegraph/sourcegraph-atom](/github.com/sourcegraph/sourcegraph-atom) › [Stephen Gutekanst](/github.com/sourcegraph/sourcegraph-atom/-/commit/5b098d7fed963d88e23057ed99d73d3c7a33ad89): [all: release v1.0.5](/github.com/sourcegraph/sourcegraph-atom/-/commit/5b098d7fed963d88e23057ed99d73d3c7a33ad89)^","url":"","detail":"","content":"```COMMIT_EDITMSG\nfoo bar\n```","ranges":[[1,3,3]]} diff --git a/cmd/src/testdata/streaming_search_want.txt b/cmd/src/testdata/streaming_search_want.txt index 422c14b4ab..9045928b92 100644 --- a/cmd/src/testdata/streaming_search_want.txt +++ b/cmd/src/testdata/streaming_search_want.txt @@ -1,6 +1,6 @@ -org/repo › path/to/file (1 match) +org/repo › path/to/file (2 matches) -------------------------------------------------------------------------------- -  5 | foo bar +  5 | foo bar foo sourcegraph/sourcegraph (http://127.0.0.1:55128/sourcegraph/sourcegraph) (1 match) org/repo › path/to/file (2 matches)