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

Function read_file_content

crates/opencode-tool/src/read.rs:351–457  ·  view source on GitHub ↗
(
    path: &Path,
    path_str: &str,
    content: &[u8],
    offset: usize,
    limit: usize,
    title: String,
    project_root: &str,
)

Source from the content-addressed store, hash-verified

349}
350
351async fn read_file_content(
352 path: &Path,
353 path_str: &str,
354 content: &[u8],
355 offset: usize,
356 limit: usize,
357 title: String,
358 project_root: &str,
359) -> Result<ToolResult, ToolError> {
360 let text = String::from_utf8_lossy(content);
361 let lines: Vec<&str> = text.lines().collect();
362
363 if offset > lines.len() {
364 return Err(ToolError::InvalidArguments(format!(
365 "Offset {} is out of range (file has {} lines)",
366 offset,
367 lines.len()
368 )));
369 }
370
371 let start = offset.saturating_sub(1);
372 let mut result_lines: Vec<String> = Vec::new();
373 let mut bytes = 0;
374 let mut truncated_by_bytes = false;
375
376 for i in start..std::cmp::min(lines.len(), start + limit) {
377 let line = if lines[i].len() > MAX_LINE_LENGTH {
378 format!("{}...", &lines[i][..MAX_LINE_LENGTH])
379 } else {
380 lines[i].to_string()
381 };
382
383 let size = line.len() + if result_lines.is_empty() { 0 } else { 1 };
384 if bytes + size > MAX_BYTES {
385 truncated_by_bytes = true;
386 break;
387 }
388
389 result_lines.push(format!("{}: {}", i + 1, line));
390 bytes += size;
391 }
392
393 let preview = result_lines
394 .iter()
395 .take(20)
396 .cloned()
397 .collect::<Vec<_>>()
398 .join("\n");
399 let total_lines = lines.len();
400 let last_read_line = offset + result_lines.len() - 1;
401 let has_more_lines = total_lines > last_read_line;
402 let truncated = has_more_lines || truncated_by_bytes;
403
404 let truncation_msg = if truncated_by_bytes {
405 format!(
406 "\n\n(Output truncated at {} bytes. Use 'offset' parameter to read beyond line {})",
407 MAX_BYTES, last_read_line
408 )

Callers 1

executeMethod · 0.85

Calls 4

newFunction · 0.85
is_emptyMethod · 0.80
cloneMethod · 0.45

Tested by

no test coverage detected