in codex-rs/tui/src/ide_context/prompt.rs [96:185]
fn render_prompt_context(context: &IdeContext) -> Option<String> {
let mut ide_context_section = String::new();
if let Some(active_file) = &context.active_file {
ide_context_section.push_str(&format!(
"\n## Active file: {}\n",
active_file.descriptor.path
));
}
if let Some(active_file) = &context.active_file {
let selected_ranges = if active_file.selections.is_empty() {
std::slice::from_ref(&active_file.selection)
} else {
active_file.selections.as_slice()
}
.iter()
.filter(|range| range.start != range.end)
.collect::<Vec<_>>();
if !selected_ranges.is_empty()
&& (active_file.active_selection_content.is_empty() || selected_ranges.len() > 1)
{
if selected_ranges.len() == 1 {
ide_context_section.push_str("\n## Active selection range:\n");
} else {
ide_context_section.push_str("\n## Active selection ranges:\n");
}
for range in selected_ranges {
// Render ranges as 1-based positions for the prompt.
let start_line = range.start.line + 1;
let start_column = range.start.character + 1;
let end_line = range.end.line + 1;
let end_column = range.end.character + 1;
ide_context_section.push_str(&format!(
"- {}: line {start_line}, column {start_column} to line {end_line}, column {end_column}\n",
active_file.descriptor.path
));
}
}
}
if let Some(active_file) = &context.active_file
&& !active_file.active_selection_content.is_empty()
{
ide_context_section.push_str("\n## Active selection of the file:\n");
let selection = active_file.active_selection_content.as_str();
if let Some((truncate_at, _)) = selection.char_indices().nth(MAX_ACTIVE_SELECTION_CHARS) {
ide_context_section.push_str(&selection[..truncate_at]);
ide_context_section.push_str(&format!(
"\n[Selection truncated to {MAX_ACTIVE_SELECTION_CHARS} characters.]\n"
));
} else {
ide_context_section.push_str(selection);
}
}
if !context.open_tabs.is_empty() {
ide_context_section.push_str("\n## Open tabs:\n");
let mut rendered_tabs = 0;
let mut rendered_tab_chars = 0;
for tab in &context.open_tabs {
if rendered_tabs >= MAX_OPEN_TABS {
break;
}
let tab_line = format!("- {}: {}\n", tab.label, tab.path);
if rendered_tab_chars + tab_line.len() > MAX_OPEN_TABS_CHARS {
break;
}
ide_context_section.push_str(&tab_line);
rendered_tabs += 1;
rendered_tab_chars += tab_line.len();
}
let omitted_tabs = context.open_tabs.len() - rendered_tabs;
if omitted_tabs > 0 {
ide_context_section.push_str(&format!("[{omitted_tabs} open tabs omitted.]\n"));
}
}
if ide_context_section.is_empty() {
None
} else {
Some(format!(
"# Context from my IDE setup:\n{ide_context_section}"
))
}
}