diff --git a/crates/semantic_index/src/parsing.rs b/crates/semantic_index/src/parsing.rs index b6fc000e1d..281b683853 100644 --- a/crates/semantic_index/src/parsing.rs +++ b/crates/semantic_index/src/parsing.rs @@ -7,6 +7,7 @@ use rusqlite::{ }; use sha1::{Digest, Sha1}; use std::{ + borrow::Cow, cmp::{self, Reverse}, collections::HashSet, ops::Range, @@ -94,12 +95,15 @@ impl CodeContextRetriever { fn parse_entire_file( &self, - relative_path: &Path, + relative_path: Option<&Path>, language_name: Arc, content: &str, ) -> Result> { let document_span = ENTIRE_FILE_TEMPLATE - .replace("", relative_path.to_string_lossy().as_ref()) + .replace( + "", + &relative_path.map_or(Cow::Borrowed("untitled"), |path| path.to_string_lossy()), + ) .replace("", language_name.as_ref()) .replace("", &content); let digest = SpanDigest::from(document_span.as_str()); @@ -114,9 +118,16 @@ impl CodeContextRetriever { }]) } - fn parse_markdown_file(&self, relative_path: &Path, content: &str) -> Result> { + fn parse_markdown_file( + &self, + relative_path: Option<&Path>, + content: &str, + ) -> Result> { let document_span = MARKDOWN_CONTEXT_TEMPLATE - .replace("", relative_path.to_string_lossy().as_ref()) + .replace( + "", + &relative_path.map_or(Cow::Borrowed("untitled"), |path| path.to_string_lossy()), + ) .replace("", &content); let digest = SpanDigest::from(document_span.as_str()); let (document_span, token_count) = self.embedding_provider.truncate(&document_span); @@ -188,7 +199,7 @@ impl CodeContextRetriever { pub fn parse_file_with_template( &mut self, - relative_path: &Path, + relative_path: Option<&Path>, content: &str, language: Arc, ) -> Result> { @@ -203,7 +214,10 @@ impl CodeContextRetriever { let mut spans = self.parse_file(content, language)?; for span in &mut spans { let document_content = CODE_CONTEXT_TEMPLATE - .replace("", relative_path.to_string_lossy().as_ref()) + .replace( + "", + &relative_path.map_or(Cow::Borrowed("untitled"), |path| path.to_string_lossy()), + ) .replace("", language_name.as_ref()) .replace("item", &span.content); diff --git a/crates/semantic_index/src/semantic_index.rs b/crates/semantic_index/src/semantic_index.rs index 115bf5d7a8..53df3476d3 100644 --- a/crates/semantic_index/src/semantic_index.rs +++ b/crates/semantic_index/src/semantic_index.rs @@ -402,7 +402,7 @@ impl SemanticIndex { if let Some(content) = fs.load(&pending_file.absolute_path).await.log_err() { if let Some(mut spans) = retriever - .parse_file_with_template(&pending_file.relative_path, &content, language) + .parse_file_with_template(Some(&pending_file.relative_path), &content, language) .log_err() { log::trace!( @@ -422,7 +422,7 @@ impl SemanticIndex { path: pending_file.relative_path, mtime: pending_file.modified_time, job_handle: pending_file.job_handle, - spans: spans, + spans, }); } }