(absPath string, match func(string) bool, maxPerFile, maxTextRunes int)
| 129 | } |
| 130 | |
| 131 | func scanFileForMatch(absPath string, match func(string) bool, maxPerFile, maxTextRunes int) ([]FileAIContentHit, error) { |
| 132 | f, err := os.Open(absPath) |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | defer f.Close() |
| 137 | |
| 138 | scanner := bufio.NewScanner(f) |
| 139 | buf := make([]byte, 0, 64*1024) |
| 140 | scanner.Buffer(buf, 1024*1024) |
| 141 | |
| 142 | var out []FileAIContentHit |
| 143 | lineNo := 0 |
| 144 | for scanner.Scan() { |
| 145 | lineNo++ |
| 146 | if len(out) >= maxPerFile { |
| 147 | break |
| 148 | } |
| 149 | line := scanner.Text() |
| 150 | if !match(line) { |
| 151 | continue |
| 152 | } |
| 153 | out = append(out, FileAIContentHit{ |
| 154 | Path: absPath, |
| 155 | Line: lineNo, |
| 156 | Text: truncateHitLine(line, maxTextRunes), |
| 157 | }) |
| 158 | } |
| 159 | return out, scanner.Err() |
| 160 | } |
| 161 | |
| 162 | func truncateHitLine(s string, maxRunes int) string { |
| 163 | if maxRunes <= 0 { |
no test coverage detected