diff --git a/crates/ide-agent/src/lib.rs b/crates/ide-agent/src/lib.rs index 936ac1055..89b88ed82 100644 --- a/crates/ide-agent/src/lib.rs +++ b/crates/ide-agent/src/lib.rs @@ -4245,11 +4245,24 @@ fn file_uri(path: &Path) -> String { out } -/// The title of a task: the first line of the prompt that says something. A pasted Markdown -/// block opens with a code fence (```` ```text ````), which names no task. +/// The title of a task: the first line of the prompt that says something. Text inside a code +/// fence (```` ```text ````) is pasted material, so a line outside the fences wins; a prompt +/// that is all one pasted block still names its task from inside it. fn title_of(prompt: &AgentPrompt) -> String { let is_fence = |l: &str| l.starts_with("```") || l.starts_with("~~~"); - let line = prompt.text.lines().map(str::trim).find(|l| !l.is_empty() && !is_fence(l)); + let (mut outside, mut inside, mut fenced) = (None, None, false); + for l in prompt.text.lines().map(str::trim) { + if is_fence(l) { + fenced = !fenced; + } else if !l.is_empty() { + let slot = if fenced { &mut inside } else { &mut outside }; + slot.get_or_insert(l); + if outside.is_some() { + break; + } + } + } + let line = outside.or(inside); let line = match line { Some(l) => l.to_owned(), None => prompt.files.first().map(|f| file_name(&f.0)).unwrap_or_else(|| "Image".into()), diff --git a/crates/ide-agent/src/tests.rs b/crates/ide-agent/src/tests.rs index 0d4051c80..fe3bd5caf 100644 --- a/crates/ide-agent/src/tests.rs +++ b/crates/ide-agent/src/tests.rs @@ -1736,6 +1736,9 @@ fn prompts_become_acp_content_blocks() { assert_eq!(title_of(&AgentPrompt { files: p.files.clone(), ..AgentPrompt::default() }), "my file.rs"); let pasted = AgentPrompt { text: "```text\n\n ## Fix the tab title\n```".into(), ..AgentPrompt::default() }; assert_eq!(title_of(&pasted), "## Fix the tab title"); + let fenced_first = + AgentPrompt { text: "```txt\nlog line\n```\nWhy does this fail?".into(), ..AgentPrompt::default() }; + assert_eq!(title_of(&fenced_first), "Why does this fail?"); assert!(clean(text(" ")).is_none()); }