Make path optional when parsing file
Co-Authored-By: Kyle Caverly <kyle@zed.dev>
This commit is contained in:
parent
24698b61fd
commit
6a271617b4
2 changed files with 22 additions and 8 deletions
|
@ -7,6 +7,7 @@ use rusqlite::{
|
||||||
};
|
};
|
||||||
use sha1::{Digest, Sha1};
|
use sha1::{Digest, Sha1};
|
||||||
use std::{
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
cmp::{self, Reverse},
|
cmp::{self, Reverse},
|
||||||
collections::HashSet,
|
collections::HashSet,
|
||||||
ops::Range,
|
ops::Range,
|
||||||
|
@ -94,12 +95,15 @@ impl CodeContextRetriever {
|
||||||
|
|
||||||
fn parse_entire_file(
|
fn parse_entire_file(
|
||||||
&self,
|
&self,
|
||||||
relative_path: &Path,
|
relative_path: Option<&Path>,
|
||||||
language_name: Arc<str>,
|
language_name: Arc<str>,
|
||||||
content: &str,
|
content: &str,
|
||||||
) -> Result<Vec<Span>> {
|
) -> Result<Vec<Span>> {
|
||||||
let document_span = ENTIRE_FILE_TEMPLATE
|
let document_span = ENTIRE_FILE_TEMPLATE
|
||||||
.replace("<path>", relative_path.to_string_lossy().as_ref())
|
.replace(
|
||||||
|
"<path>",
|
||||||
|
&relative_path.map_or(Cow::Borrowed("untitled"), |path| path.to_string_lossy()),
|
||||||
|
)
|
||||||
.replace("<language>", language_name.as_ref())
|
.replace("<language>", language_name.as_ref())
|
||||||
.replace("<item>", &content);
|
.replace("<item>", &content);
|
||||||
let digest = SpanDigest::from(document_span.as_str());
|
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<Vec<Span>> {
|
fn parse_markdown_file(
|
||||||
|
&self,
|
||||||
|
relative_path: Option<&Path>,
|
||||||
|
content: &str,
|
||||||
|
) -> Result<Vec<Span>> {
|
||||||
let document_span = MARKDOWN_CONTEXT_TEMPLATE
|
let document_span = MARKDOWN_CONTEXT_TEMPLATE
|
||||||
.replace("<path>", relative_path.to_string_lossy().as_ref())
|
.replace(
|
||||||
|
"<path>",
|
||||||
|
&relative_path.map_or(Cow::Borrowed("untitled"), |path| path.to_string_lossy()),
|
||||||
|
)
|
||||||
.replace("<item>", &content);
|
.replace("<item>", &content);
|
||||||
let digest = SpanDigest::from(document_span.as_str());
|
let digest = SpanDigest::from(document_span.as_str());
|
||||||
let (document_span, token_count) = self.embedding_provider.truncate(&document_span);
|
let (document_span, token_count) = self.embedding_provider.truncate(&document_span);
|
||||||
|
@ -188,7 +199,7 @@ impl CodeContextRetriever {
|
||||||
|
|
||||||
pub fn parse_file_with_template(
|
pub fn parse_file_with_template(
|
||||||
&mut self,
|
&mut self,
|
||||||
relative_path: &Path,
|
relative_path: Option<&Path>,
|
||||||
content: &str,
|
content: &str,
|
||||||
language: Arc<Language>,
|
language: Arc<Language>,
|
||||||
) -> Result<Vec<Span>> {
|
) -> Result<Vec<Span>> {
|
||||||
|
@ -203,7 +214,10 @@ impl CodeContextRetriever {
|
||||||
let mut spans = self.parse_file(content, language)?;
|
let mut spans = self.parse_file(content, language)?;
|
||||||
for span in &mut spans {
|
for span in &mut spans {
|
||||||
let document_content = CODE_CONTEXT_TEMPLATE
|
let document_content = CODE_CONTEXT_TEMPLATE
|
||||||
.replace("<path>", relative_path.to_string_lossy().as_ref())
|
.replace(
|
||||||
|
"<path>",
|
||||||
|
&relative_path.map_or(Cow::Borrowed("untitled"), |path| path.to_string_lossy()),
|
||||||
|
)
|
||||||
.replace("<language>", language_name.as_ref())
|
.replace("<language>", language_name.as_ref())
|
||||||
.replace("item", &span.content);
|
.replace("item", &span.content);
|
||||||
|
|
||||||
|
|
|
@ -402,7 +402,7 @@ impl SemanticIndex {
|
||||||
|
|
||||||
if let Some(content) = fs.load(&pending_file.absolute_path).await.log_err() {
|
if let Some(content) = fs.load(&pending_file.absolute_path).await.log_err() {
|
||||||
if let Some(mut spans) = retriever
|
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_err()
|
||||||
{
|
{
|
||||||
log::trace!(
|
log::trace!(
|
||||||
|
@ -422,7 +422,7 @@ impl SemanticIndex {
|
||||||
path: pending_file.relative_path,
|
path: pending_file.relative_path,
|
||||||
mtime: pending_file.modified_time,
|
mtime: pending_file.modified_time,
|
||||||
job_handle: pending_file.job_handle,
|
job_handle: pending_file.job_handle,
|
||||||
spans: spans,
|
spans,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue