Skip to main content

vtcode_core/instructions/
render.rs

1use std::fmt::Write as _;
2use std::path::Path;
3
4use super::labels::{format_instruction_path, instruction_source_label};
5use super::types::InstructionSegment;
6
7pub fn extract_instruction_highlights(segments: &[InstructionSegment], limit: usize) -> Vec<String> {
8    if limit == 0 {
9        return Vec::new();
10    }
11
12    let mut highlights = Vec::with_capacity(limit);
13    for segment in segments {
14        let mut found_bullet = false;
15        for line in segment.contents.lines() {
16            if highlights.len() >= limit {
17                break;
18            }
19
20            let trimmed = line.trim();
21            if trimmed.starts_with('-') {
22                let highlight = trimmed.trim_start_matches('-').trim();
23                if !highlight.is_empty() {
24                    highlights.push(highlight.to_string());
25                    found_bullet = true;
26                }
27            }
28        }
29
30        if highlights.len() >= limit {
31            break;
32        }
33
34        if found_bullet {
35            continue;
36        }
37
38        for line in segment.contents.lines() {
39            if highlights.len() >= limit {
40                break;
41            }
42
43            let trimmed = line.trim();
44            if trimmed.is_empty() {
45                continue;
46            }
47
48            let fallback = trimmed.trim_start_matches('#').trim();
49            if !fallback.is_empty() {
50                highlights.push(fallback.to_string());
51                break;
52            }
53        }
54
55        if highlights.len() >= limit {
56            break;
57        }
58    }
59
60    highlights
61}
62
63pub fn render_instruction_markdown(
64    title: &str,
65    segments: &[InstructionSegment],
66    truncated: bool,
67    project_root: &Path,
68    home_dir: Option<&Path>,
69    truncation_note: &str,
70) -> String {
71    let combined_len = segments.iter().map(|segment| segment.contents.len()).sum::<usize>();
72    let mut section = String::with_capacity(combined_len.saturating_add(512));
73    let _ = writeln!(section, "## {title}\n");
74
75    // Full segment bodies are inlined below, so a "Key points" highlight list
76    // is a pure echo of content the model already receives (AGENTS.md alone
77    // re-printed ~1.2 KB of the first bullets). Skip it. The map and
78    // precedence sentence only pay for themselves with multiple sources.
79    let show_map = segments.len() > 1;
80    if show_map {
81        section.push_str(
82            "Instructions are listed from lowest to highest precedence. When conflicts exist, defer to the later entries.\n\n",
83        );
84        section.push_str("### Instruction map\n");
85        for (index, segment) in segments.iter().enumerate() {
86            let _ = writeln!(
87                section,
88                "- {}. {} ({})",
89                index + 1,
90                format_instruction_path(&segment.source.path, project_root, home_dir),
91                instruction_source_label(&segment.source),
92            );
93        }
94    }
95
96    if !segments.is_empty() {
97        for (index, segment) in segments.iter().enumerate() {
98            let _ = writeln!(
99                section,
100                "\n### {}. {} ({})\n",
101                index + 1,
102                format_instruction_path(&segment.source.path, project_root, home_dir),
103                instruction_source_label(&segment.source),
104            );
105            section.push_str(segment.contents.trim());
106            section.push('\n');
107        }
108    }
109
110    if truncated && !truncation_note.is_empty() {
111        let _ = writeln!(section, "\n_{truncation_note}_");
112    }
113
114    section.push('\n');
115    section
116}