MCPcopy Index your code
hub / github.com/cli/cli / formatMatch

Function formatMatch

pkg/cmd/search/code/code.go:192–229  ·  view source on GitHub ↗
(t string, matches []search.Match, io *iostreams.IOStreams)

Source from the content-addressed store, hash-verified

190}
191
192func formatMatch(t string, matches []search.Match, io *iostreams.IOStreams) []string {
193 cs := io.ColorScheme()
194
195 startIndices := map[int]struct{}{}
196 endIndices := map[int]struct{}{}
197 for _, m := range matches {
198 if len(m.Indices) < 2 {
199 continue
200 }
201 startIndices[m.Indices[0]] = struct{}{}
202 endIndices[m.Indices[1]] = struct{}{}
203 }
204
205 var lines []string
206 var b strings.Builder
207 var found bool
208 for i, c := range t {
209 if c == '\n' {
210 if found {
211 lines = append(lines, b.String())
212 }
213 found = false
214 b.Reset()
215 continue
216 }
217 if _, ok := startIndices[i]; ok {
218 b.WriteString(cs.HighlightStart())
219 found = true
220 } else if _, ok := endIndices[i]; ok {
221 b.WriteString(cs.Reset())
222 }
223 b.WriteRune(c)
224 }
225 if found {
226 lines = append(lines, b.String())
227 }
228 return lines
229}

Callers 1

displayResultsFunction · 0.85

Calls 4

ColorSchemeMethod · 0.80
HighlightStartMethod · 0.80
StringMethod · 0.45
ResetMethod · 0.45

Tested by

no test coverage detected