MCPcopy Index your code
hub / github.com/ChrisFeldmeier/OpenCodeRust / search_file

Function search_file

crates/opencode-grep/src/search.rs:212–254  ·  view source on GitHub ↗
(
    path: &Path,
    regex: &regex::Regex,
    matches: &mut Vec<MatchResult>,
    limit: usize,
)

Source from the content-addressed store, hash-verified

210}
211
212fn search_file(
213 path: &Path,
214 regex: &regex::Regex,
215 matches: &mut Vec<MatchResult>,
216 limit: usize,
217) -> Result<(), io::Error> {
218 let file = File::open(path)?;
219 let reader = BufReader::new(file);
220 let path_str = path.to_string_lossy().to_string();
221 let mut offset = 0;
222
223 for (line_num, line_result) in reader.lines().enumerate() {
224 if matches.len() >= limit {
225 break;
226 }
227
228 let line = line_result?;
229 let line_len = line.len() + 1;
230
231 if regex.is_match(&line) {
232 let mut submatches = Vec::new();
233 for cap in regex.find_iter(&line) {
234 submatches.push(SubMatch {
235 text: cap.as_str().to_string(),
236 start: cap.start(),
237 end: cap.end(),
238 });
239 }
240
241 matches.push(MatchResult {
242 path: path_str.clone(),
243 line_number: line_num + 1,
244 lines: line,
245 absolute_offset: offset,
246 submatches,
247 });
248 }
249
250 offset += line_len;
251 }
252
253 Ok(())
254}
255
256#[derive(Debug, Clone, Serialize, Deserialize)]
257struct TreeNode {

Callers 1

search_with_limitMethod · 0.85

Calls 4

newFunction · 0.85
as_strMethod · 0.45
startMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected