Allow example name to be specified in markdown

Max Brunsfeld created

Change summary

crates/edit_prediction/src/example_spec.rs     |  7 +++++--
crates/edit_prediction_cli/src/example.rs      | 10 +++++++---
crates/edit_prediction_cli/src/load_project.rs |  3 +--
3 files changed, 13 insertions(+), 7 deletions(-)

Detailed changes

crates/edit_prediction/src/example_spec.rs 🔗

@@ -104,11 +104,11 @@ impl ExampleSpec {
     }
 
     /// Parse an example spec from markdown.
-    pub fn from_markdown(name: String, mut input: &str) -> anyhow::Result<Self> {
+    pub fn from_markdown(mut input: &str) -> anyhow::Result<Self> {
         use pulldown_cmark::{CodeBlockKind, CowStr, Event, HeadingLevel, Parser, Tag, TagEnd};
 
         let mut spec = ExampleSpec {
-            name,
+            name: String::new(),
             repository_url: String::new(),
             revision: String::new(),
             uncommitted_diff: String::new(),
@@ -150,6 +150,9 @@ impl ExampleSpec {
                 Event::Text(line) => {
                     text.push_str(&line);
                 }
+                Event::End(TagEnd::Heading(HeadingLevel::H1)) => {
+                    spec.name = mem::take(&mut text);
+                }
                 Event::End(TagEnd::Heading(HeadingLevel::H2)) => {
                     let title = mem::take(&mut text);
                     current_section = if title.eq_ignore_ascii_case(UNCOMMITTED_DIFF_HEADING) {

crates/edit_prediction_cli/src/example.rs 🔗

@@ -190,7 +190,11 @@ pub fn read_examples(inputs: &[PathBuf]) -> Vec<Example> {
                     .collect::<Vec<Example>>(),
             ),
             "md" => {
-                examples.push(parse_markdown_example(filename, &content).unwrap());
+                let mut example = parse_markdown_example(&content).unwrap();
+                if example.spec.name.is_empty() {
+                    example.spec.name = filename;
+                }
+                examples.push(example);
             }
             ext => {
                 panic!("{} has invalid example extension `{ext}`", path.display())
@@ -236,8 +240,8 @@ pub fn group_examples_by_repo(examples: &mut [Example]) -> Vec<Vec<&mut Example>
     examples_by_repo.into_values().collect()
 }
 
-fn parse_markdown_example(name: String, input: &str) -> Result<Example> {
-    let spec = ExampleSpec::from_markdown(name, input)?;
+fn parse_markdown_example(input: &str) -> Result<Example> {
+    let spec = ExampleSpec::from_markdown(input)?;
     Ok(Example {
         spec,
         buffer: None,

crates/edit_prediction_cli/src/load_project.rs 🔗

@@ -14,15 +14,14 @@ use futures::{
 };
 use gpui::{AsyncApp, Entity};
 use language::{Anchor, Buffer, LanguageNotFound, ToOffset, ToPoint};
+use project::Project;
 use project::buffer_store::BufferStoreEvent;
-use project::{Project, ProjectPath};
 use std::{
     cell::RefCell,
     fs,
     path::{Path, PathBuf},
     sync::Arc,
 };
-use util::{paths::PathStyle, rel_path::RelPath};
 use zeta_prompt::CURSOR_MARKER;
 
 pub async fn run_load_project(